2013-07-31 66 views
2

我一直在爲重命名Python項目的項目文件夾而苦苦掙扎。它被稱爲Foo,我希望它將它重命名爲Bar。重命名項目文件夾後Pytests導入失敗

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 

當項目文件夾命名爲美孚我所有的測試通過,但將其重命名爲BAR我的測試後不工作了。所有進口將增加ImportError: no module src.x

可以導入時,我使用Python控制檯模塊:

$ python 
>>> import src.x 

當我然後重命名酒吧回到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並繼續進行測試?

回答

4

刪除python緩存(__pycache__)和所有「編譯」文件(即:* .pyc,* .pyo)。

確保您的__init.py__文件不是指任何文件夾或路徑。

+2

從pycmd包(https://pypi.python.org/pypi/pycmd)使用'py.cleanup -p'命令對此很方便。 – flub

+0

我不得不同意用python緩存這個問題很容易通過上面的評論來解決。安裝pycmd包並使用'py.cleanup -p'和重命名目錄不會拋出錯誤。 –