Python 使用pytest xdist会导致AttributeError:module'__主&';没有属性'__文件';

Python 使用pytest xdist会导致AttributeError:module'__主&';没有属性'__文件';,python,pytest,python-import,attributeerror,xdist,Python,Pytest,Python Import,Attributeerror,Xdist,我正在尝试使用pytest xdist在多个CPU上运行测试,以更快地运行测试。但是,当我切换到使用多个CPU时,测试不会运行,因为收集测试时出错 更具体地说,错误是 AttributeError: module '__main__' has no attribute '__file__' 这是因为正在测试的脚本导入了我编写的日志模块。日志模块使用 __main__.__file__ 确定主应用程序的名称,以便将其添加到日志输出文件名 我似乎不知道pytest在多CPU模式下与在单CPU模式

我正在尝试使用pytest xdist在多个CPU上运行测试,以更快地运行测试。但是,当我切换到使用多个CPU时,测试不会运行,因为收集测试时出错

更具体地说,错误是

AttributeError: module '__main__' has no attribute '__file__'
这是因为正在测试的脚本导入了我编写的日志模块。日志模块使用

__main__.__file__
确定主应用程序的名称,以便将其添加到日志输出文件名

我似乎不知道pytest在多CPU模式下与在单CPU模式下做的有什么不同

所以当我执行测试时

python3 -m pytest
一切正常,但一旦我切换到

python3 -m pytest -n 4
我发现以下错误:

____________________ ERROR collecting ***.py
***/logging/log_setup.py:21: in <module>
    app_name = os.path.basename(__main__.__file__)[:-3]
E   AttributeError: module '__main__' has no attribute '__file__'
编辑: 我现在在日志模块中使用以下代码来回避这个问题:

if hasattr(__main__, '__file__'):
    app_name = os.path.basename(__main__.__file__)[:-3]
else:
    app_name = 'unknown_app_name'

现在,使用多个CPU的pytest对我来说很好。

在这两种情况下,
dir(\uuu main\uuuu)
的输出是什么?使用多个CPU时,['annotations'、'builtins'、'doc'、'loader'、'name'、'package'、'spec'、'execmodel'、'get\u execmodel'、'init\u popen\u io'、'serve'、'sys']使用一个CPU,它是:['Class'、'Collector'、'File'、'Function'、'Instance'、'Item'、'Module'、'Package'、'PytestAssertRewriteWarning'、'PytestCacheWarning'、'PytestCollectionWarning'、'PytestConfigWarning'、'PytestExperimentalApiWarning'、'PytestUnhandledCoroutineWarning'、'PytestUnknownMarkWarning'、'removedinbytest4warning'“,”Session“,”UsageError“,”all“,”annotations“,”builtins“,”cached“,”doc“,”file“,”loader“,”name“,”package“,”pytestPDB“,”proversion“,”version“,”pyfuncargs“,”近似“,”cmdline“,”弃用的调用“,”exit“,”fail“,”fixture“,”freeze“,”freeze“,”hookimport“,”main“,”hookimportorskip“ssert_rewrite'、“set_trace”、“skip”、“warns”、“xfail”、“YOUD_fixture”]您能提供一个解决方案吗?
if hasattr(__main__, '__file__'):
    app_name = os.path.basename(__main__.__file__)[:-3]
else:
    app_name = 'unknown_app_name'