Join GitHub today
GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together.
Sign upStructuring your project guide leads to a ModuleNotFoundError in the tests module #1013
Comments
|
Relative imports only work in a package that is being imported due to using the __name__ variable that is set to "__main__" if the file is ran directly. If you import test_basic.py, it should work to run test_basic.sample. If you take out the period, you lose the ability to import the tests from elsewhere so what you want depends on how you're using the tests. |
|
Python tests are usually coded to be executed through a testing framework. In this case
|
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment


Following this section, adding
from .context import samplein my tests module gives me the following error:ModuleNotFoundError: No module named '__main__.context'; '__main__' is not a packageI was able to fix it by removing the
.fromfrom .context import sample, but I suspect that's not the ideal solution. I know that thefrom .context import sampleis a relative import statement as described in PEP 328, but I just do not understand modules, packages, absolute and relative imports, etc. enough to say what's going on here.I tried creating
./__init__.pyand./tests/__init__.pyfiles, but that did not help.Finally, I tried the following:
I'm not alone in hitting this issue. @rileyweber13 originally reported this problem a year ago.