我正在使用Python的包。我使用virtualenv。我在virtualenv的.pth路徑中設置了模塊根目錄的路徑,這樣我可以在開發代碼的同時導入包的模塊並進行測試(問題1:是否是一種好的方法?)。這工作正常(這裏有一個例子,這是我想要的行爲):pytest無法導入模塊,而python可以
(VEnvTestRc) [email protected]:~/Desktop/GitFolders/rc$ python
Python 2.7.12 (default, Jul 1 2016, 15:12:24)
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from rc import ns
>>> exit()
(VEnvTestRc) [email protected]:~/Desktop/GitFolders/rc$ python tests/test_ns.py
issued command: echo hello
command output: hello
但是,如果我嘗試使用PyTest,我得到一些進口的錯誤信息:
(VEnvTestRc) [email protected]:~/Desktop/GitFolders/rc$ pytest
=========================================== test session starts ============================================
platform linux2 -- Python 2.7.12, pytest-3.0.5, py-1.4.31, pluggy-0.4.0
rootdir: /home/zz/Desktop/GitFolders/rc, inifile:
collected 0 items/1 errors
================================================== ERRORS ==================================================
________________________________ ERROR collecting tests/test_ns.py ________________________________
ImportError while importing test module '/home/zz/Desktop/GitFolders/rc/tests/test_ns.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
tests/test_ns.py:2: in <module>
from rc import ns
E ImportError: cannot import name ns
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Interrupted: 1 errors during collection !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
========================================= 1 error in 0.09 seconds ==========================================
(VEnvTestRc) [email protected]:~/Desktop/GitFolders/rc$ which pytest
/home/zz/Desktop/VirtualEnvs/VEnvTestRc/bin/pytest
我是有點困惑,它看起來像這樣表示一個導入錯誤,但是Python很好,所以爲什麼PyTest特別存在問題?對於原因/補救措施的任何建議(問題2)?我用Google搜索並堆棧溢出PyTest的'ImportError:can not import'錯誤,但是我得到的匹配與缺少python路徑和補救措施有關,這似乎不是問題。有什麼建議麼?
有趣。我有同樣的問題,我不得不將__init__.py添加到我的測試文件夾。 –