10

I am working on unittest and when I type that :

python manage.py test

I got that :

ImportError: 'tests' module incorrectly imported from '/home/user/Documents/myproject/dev/Project/tests'. Expected '/home/user/Documents/myproject/dev/Project/Project/Project/Project/Project/Project/Project/Project/Project/Project'. Is this module globally installed?

Clearly, the expected path is wrong... Do you know where I can fix that ?

Thank you very much !

Peter
  • 123
  • 5
  • 1
    Take a look at this; https://stackoverflow.com/questions/37525075/what-does-tests-module-incorrectly-imported-mean – markwalker_ Feb 08 '21 at 22:12

2 Answers2

24

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

For more information, see this website.

Alex Waygood
  • 6,304
  • 3
  • 24
  • 46
Mark
  • 644
  • 1
  • 4
  • 11
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-ask). – Community Sep 15 '21 at 21:22
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-ask). – Community Sep 16 '21 at 06:11
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.

GrandOpener
  • 1,943
  • 1
  • 17
  • 25