Python:登录模块脚本,以另一个scrip调用

2024-10-02 00:38:23 发布

您现在位置:Python中文网/ 问答频道 /正文

我有大约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'

如何在所有模块脚本中进行日志记录,以保存与作为参数的输入文件名同名的日志文件?在


Tags: nameinfrompyimport脚本logreturn
1条回答
网友
1楼 · 发布于 2024-10-02 00:38:23

您提供的错误是由于循环导入。您可以在回溯hutextract.py-randpage.py-mandate_final.py-hutextract.py中看到这个圆圈

现在到伐木场。您应该在多个脚本中(在起始脚本中)只使用一次logging.basicConfig(...),因为这一行代码修改了日志模块的所谓根日志记录器。此根记录器是在您第一次导入日志模块时创建的,它位于全局范围内。所以根记录程序总是可用的-只需在需要的时候和地点使用它。在

相关问题 更多 >

    热门问题