pytest cannot import module while python can

匿名 (未验证) 提交于 2019-12-03 03:04:01

问题:

I am working on a package in Python. I use virtualenv. I set the path to the root of the module in a .pth path in my virtualenv, so that I can import modules of the package while developing the code and do testing (Question 1: is it a good way to do?). This works fine (here is an example, this is the behavior I want):

(VEnvTestRc) zz@zz:~/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) zz@zz:~/Desktop/GitFolders/rc$ python tests/test_ns.py  issued command: echo hello command output: hello

However, if I try to use PyTest, I get some import error messages:

(VEnvTestRc) zz@zz:~/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) zz@zz:~/Desktop/GitFolders/rc$ which pytest /home/zz/Desktop/VirtualEnvs/VEnvTestRc/bin/pytest

I am a bit puzzled, it looks like this indicates an import error, but Python does it fine so why is there a problem specifically with PyTest? Any suggestion to the reason / remedy (Question 2)? I googled and stack-overflowed the 'ImportError: cannot import' error for PyTest, but the hits I got were related to missing python path and remedy to this, which does not seem to be the problem here. Any suggestions?

回答1:

Found the answer:

DO NOT put a __init__.py file in a folder containing TESTS if you plan on using pytest. I had one such file, deleting it solved the problem.

This was actually buried in the comments to the second answer of PATH issue with pytest 'ImportError: No module named YadaYadaYada' so I did not see it, hope it gets more visibility here.



回答2:

I had the same problem but for another reason than the ones mentioned:

I had py.test installed globally, while the packages were installed in a virtual environment.

The solution was to install pytest in the virtual environment. (In case your shell hashes executables, as Bash does, use hash -r, or use the full path to py.test)



回答3:

I can't say I understand why this works, but I had the same problem and the tests work fine if I run python -m pytest.

I'm in a virtualenv, with pytest also available globally:

(proj)tom@neon ~/dev/proj$ type -a python python is /home/tom/.virtualenvs/proj/bin/python python is /usr/bin/python  (proj)tom@neon ~/dev/proj$ python -V Python 3.5.2  (proj)tom@neon ~/dev/proj$ type -a pytest pytest is /home/tom/.virtualenvs/proj/bin/pytest pytest is /usr/bin/pytest  (proj)tom@neon ~/dev/proj$ pytest --version This is pytest version 3.5.0, imported from /home/tom/.virtualenvs/proj/lib/python3.5/site-packages/pytest.py


回答4:

It could be that Pytest is not reading the package as a Python module while Python is (likely due to path issues). Try changing the directory of the pytest script or adding the module explicitly to your PYTHONPATH.

Or it could be that you have two versions of Python installed on your machine. Check your Python source for pytest and for the python shell that you run. If they are different (i.e. Python 2 vs 3), use source activate to make sure that you are running the pytest installed for the same python that the module is installed in.



回答5:

I had a similar issue, exact same error, but different cause. I was running the test code just fine, but against an old version of the module. In the previous version of my code one class existed, while the other did not. After updating my code, I should have run the following to install it.

sudo pip install ./ --upgrade

Upon installing the updated module running pytest produced the correct results (because i was using the correct code base).



回答6:

Install the packages into Your virtual environment.
Then start a new shell and source Your virtual environment again.



回答7:

In my case, the import error occurred because the package is pointing to another package/directory with the same name and its path is one level above the folder I actually wanted. I think this also explains why some people need to remove _ init _.py while others need to add back.

I just put print(the_root_package.__path__) (after import the_root_package) in both python console and pytest scripts to compare the difference

BOTTOM LINE: When you do python, the package you import may be different from the package when you run pytest.



回答8:

I just solved this by removing the __init__.py in my project root:

. ├―― __init__.py <--- removed ├―― models    ├―― __init__.py    ├―― address.py    ├―― appointment.py    └―― client.py ├―― requirements.txt ├―― setup.cfg ├―― tests    ├―― __init__.py    ├―― models       ├―― __init__.py       ├―― appointment_test.py       └―― client_test.py    └―― other_test.py └―― script.py


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!