[Solved]-ImportError: 'tests' module incorrectly

25👍

Delete the tests.py file automatically generated by Django. That helped me make it work.

For more information, see this website.

👤Mark

0👍

To expand on Mark’s answer, especially in case the link bit-rots, the most common reason this happens is a conflict between two potential definitions of the "test" module. You probably have a directory structure something like:

myproject/mymodule/tests.py
myproject/mymodule/tests/__init__.py
myproject/mymodule/tests/test_feature.py

This is not a problem with tests specifically, but rather with (mis)use of the Python module/import system. There are two potential definitions there for the mymodule.tests module, and Python doesn’t know which to use. Instead, it simply errors out and forces you to fix the problem. This error surfaces in a somewhat surprising way for tests because (1) you likely never explicitly imported this module and (2) Django may have created the tests.py file automatically without telling you. But if you created this file structure for a non-test module and tried to import it, you’d get a very similar error.

Deleting tests.py fixes the problem because then there is only one definition for the module. Deleting the tests folder would also work if your module has so few tests that making a separate folder doesn’t make sense. If both locations have meaningful tests, make sure you copy the ones you care about to the location you’re going to keep before deleting the one you don’t want.

Leave a comment