{"id":938,"hash":"8167369fc3e32c3f1e7fe02fbf2bbe79aa48efe16de0f8cc9c42661957b60796","pattern":"pytest cannot import module while python can","full_message":"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):\n\n(VEnvTestRc) zz@zz:~/Desktop/GitFolders/rc$ python\nPython 2.7.12 (default, Jul  1 2016, 15:12:24) \n[GCC 5.4.0 20160609] on linux2\nType \"help\", \"copyright\", \"credits\" or \"license\" for more information.\n>>> from rc import ns\n>>> exit()\n(VEnvTestRc) zz@zz:~/Desktop/GitFolders/rc$ python tests/test_ns.py \nissued command: echo hello\ncommand output: hello\n\nHowever, if I try to use PyTest, I get some import error messages:\n\n(VEnvTestRc) zz@zz:~/Desktop/GitFolders/rc$ pytest\n=========================================== test session starts ============================================\nplatform linux2 -- Python 2.7.12, pytest-3.0.5, py-1.4.31, pluggy-0.4.0\nrootdir: /home/zz/Desktop/GitFolders/rc, inifile: \ncollected 0 items / 1 errors \n\n================================================== ERRORS ==================================================\n________________________________ ERROR collecting tests/test_ns.py ________________________________\nImportError while importing test module '/home/zz/Desktop/GitFolders/rc/tests/test_ns.py'.\nHint: make sure your test modules/packages have valid Python names.\nTraceback:\ntests/test_ns.py:2: in <module>\n    from rc import ns\nE   ImportError: cannot import name ns\n!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Interrupted: 1 errors during collection !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n========================================= 1 error in 0.09 seconds ==========================================\n(VEnvTestRc) zz@zz:~/Desktop/GitFolders/rc$ which pytest\n/home/zz/Desktop/VirtualEnvs/VEnvTestRc/bin/pytest\n\nI 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?","ecosystem":"pypi","package_name":"import","package_version":null,"solution":"Found the answer:\n\nDO 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.\n\nThis 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.","confidence":0.95,"source":"stackoverflow","source_url":"https://stackoverflow.com/questions/41748464/pytest-cannot-import-module-while-python-can","votes":344,"created_at":"2026-04-19T04:52:04.236111+00:00","updated_at":"2026-04-19T04:52:04.236111+00:00"}