Python ModuleNotFoundError Testsuite (Import Error)
Solution 1:
This is because it's located in the tests
directory, but you're running test_suite.py
from the parent directory of tests
. The reason why it worked when you ran test_website_loads.py
is because you were running it from within the tests
directory. When resolving imports, the Python interpreter checks a series of locations for the module, starting with the current directory and then moving to other locations such as those in your PYTHONPATH environment variable, and the site-packages
directory in your Python install location.
I duplicated this on my system but changed the import statement to tests.special_module.special_module_file
and it worked. If you don't like this solution, you will need to change the location of the special_module
directory or add it to your PYTHONPATH or something similar.
Edit: In response to your comment below, I assume your test_suite.py
file looks something like this:
from tests.test_website_loads import some_function, some_class
result = some_function()
obj = some_class()
This still runs into the problem described above, because the Python interpreter is still being run in the top-level directory. When it searches for modules, it searches the current directory where it only finds test_suite.py
and tests/
. Then it checks your PYTHONPATH environment variable. If it still finds nothing, it will check then installation-dependent default location (such as the site-packages
directory), and if no such module is found, it throws an exception. I think the best solution would be to add special_module
to the PYTHONPATH environment variable as described in the link I included above. Otherwise, you could create a symbolic link to the module in the top-level directory with ln -s tests/special_module special_module
, assuming that you're running on a UNIX-like system. However, this would not be considered best practice; the first method is preferred.
Post a Comment for "Python ModuleNotFoundError Testsuite (Import Error)"