我与 pycharm 和 unittests 一起遇到了这个问题,并想添加一个额外的答案。我已经用 python 3 对其进行了测试,但假设如果你使用from __future__ import absolute_import
早期版本也会发生同样的情况。
pycharm 错误报告中描述了根本原因https://youtrack.jetbrains.com/issue/PY-15889
总结:如果你在 pycharm 中运行 unittests,它总是将包含测试脚本的文件夹添加到sys.path
. 如果您celery.py
在同一路径中,python 将首先尝试加载它。
我没有更改文件名,而是通过在sys.path
导入测试脚本之前删除此文件夹来解决此问题。celery.py
..._Test.py
# If running in pycharm, pycharm always adds the current path to the beginning (!) of sys.path
# I have not found a setting to fix this - so I just remove the path again
# This block can be removed once https://youtrack.jetbrains.com/issue/PY-15889 if fixed
import sys
import os
sys.path.remove(os.path.dirname(__file__))
import celery