我有大约15个不同的python脚本用于应用程序开发,其中10个脚本包括用于调试的日志记录。现在有一个主剧本说hutdextract.py“以文件名作为参数运行。以前日志文件名已修复“测试日志". 现在我想创建与输入文件名同名的日志文件(除了扩展名)。我的“hutdextract.py“代码,其中“randpage”和“constraint”是其他python脚本:
from randpage import genrand
from mandatebase import formext
...# importing other scripts, functions
import logging
file_name=sys.argv[1]
def return_log_name():
return ".".join(file_name.split("/")[-1].split(".")[:-1]) + ".log"
log_file_name = return_log_name()
logging.basicConfig(filename=log_file_name, level=logging.DEBUG, format="%(asctime)s:%(levelname)s:%(message)s")
在随机页.py, 命令库.py和其他脚本,日志也包括在那里:
^{pr2}$这会产生一个错误,当我们试图运行hutdextract.py使用参数调用其他脚本(及其函数),这些脚本将再次从hutdextract.py出于记录目的:
Traceback (most recent call last):
File "hutextract.py", line 3, in <module>
from randpage import genrand
File "/home/src/randpage.py", line 3, in <module>
from mandate_final import sigext
File "/home/src/mandate_final.py", line 5, in <module>
from hutextract import return_log_name
File "/home/src/hutextract.py", line 3, in <module>
from randpage import genrand
ImportError: cannot import name 'genrand'
如何在所有模块脚本中进行日志记录,以保存与作为参数的输入文件名同名的日志文件?在
您提供的错误是由于循环导入。您可以在回溯
hutextract.py
-randpage.py
-mandate_final.py
-hutextract.py
中看到这个圆圈现在到伐木场。您应该在多个脚本中(在起始脚本中)只使用一次
logging.basicConfig(...)
,因为这一行代码修改了日志模块的所谓根日志记录器。此根记录器是在您第一次导入日志模块时创建的,它位于全局范围内。所以根记录程序总是可用的-只需在需要的时候和地点使用它。在相关问题 更多 >
编程相关推荐