Skip to content Skip to sidebar Skip to footer

Python Modulenotfounderror Testsuite (import Error)

When I run a file with some tests (ex. python test_website_loads.py) the test runs perfectly and there are no issues, but when I try the testsuite (ex. python test_suite.py), the n

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_loadsimport some_function, some_classresult = 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)"