我一直在努力重命名Python项目的项目文件夹。它被称为Foo,我希望它将其重命名为Bar。
Foo/
/src
__init__.py
x.py
/test
__init__.py
x_test.py
__init__.py
成了
Bar/
/src
__init__.py
x.py
/test
__init__.py
x_test.py
__init__.py
当项目文件夹命名为Foo时,我的所有测试都通过了,但在将其重命名为Bar后,我的测试不再起作用了。所有导入都会引发ImportError: no module src.x
。
我可以在我使用python控制台时导入模块:
$ python
>>> import src.x
当我将Bar重命名为Foo并运行测试时,我会收到此错误:
import file mismatch:
imported module 'test.x' has this __file__ attribute:
/home/orangetux/projects/Foo/test/x_test.py
which is not the same as the test file we want to collect:
/home/orangetux/projects/Bar/test/x_test.py
HINT: remove __pycache__ / .pyc files and/or use a unique basename for your test file modules
我可以通过删除所有__pycache__
个文件夹来解决此问题。但现在我回到了开始。一个名为Foo的文件夹,带有工作测试。如何将项目文件夹重命名为Bar并继续进行工作测试?
答案 0 :(得分:4)
删除python缓存(__pycache__
)和所有“已编译”文件(即:* .pyc,* .pyo)。
确保 __init.py__
文件不是指任何文件夹或路径。
答案 1 :(得分:0)
这是由于您的根目录中存在不必要的 __init__.py
文件造成的。
如果你只是删除它,你应该能够正确运行测试,即使你重命名了项目文件夹。
Bar/
/src
__init__.py
x.py
/test
__init__.py
x_test.py
__init__.py -------------> REMOVE THIS FILE