145

I have copied a working test line by line and just changed a few names (at least so I thought) and now I get this very cryptic error: (I have replaced some stuff with FOO, BAR)

ImportError: 'tests' module incorrectly imported from 'FOO/exports/tests'. Expected 'FOO/exports'. Is this module globally installed?

The problem is that I do not understand the error at all. What does this error message mean?

Complete stacktrace:

Traceback (most recent call last):
  File "BAR/modeling/manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/Users/jonathan/anaconda/lib/python2.7/site-packages/django/core/management/__init__.py", line 353, in execute_from_command_line
    utility.execute()
  File "/Users/jonathan/anaconda/lib/python2.7/site-packages/django/core/management/__init__.py", line 345, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/Users/jonathan/anaconda/lib/python2.7/site-packages/django/core/management/commands/test.py", line 30, in run_from_argv
    super(Command, self).run_from_argv(argv)
  File "/Users/jonathan/anaconda/lib/python2.7/site-packages/django/core/management/base.py", line 348, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/Users/jonathan/anaconda/lib/python2.7/site-packages/django/core/management/commands/test.py", line 74, in execute
    super(Command, self).execute(*args, **options)
  File "/Users/jonathan/anaconda/lib/python2.7/site-packages/django/core/management/base.py", line 399, in execute
    output = self.handle(*args, **options)
  File "/Users/jonathan/anaconda/lib/python2.7/site-packages/django/core/management/commands/test.py", line 90, in handle
    failures = test_runner.run_tests(test_labels)
  File "/Users/jonathan/anaconda/lib/python2.7/site-packages/django/test/runner.py", line 531, in run_tests
    suite = self.build_suite(test_labels, extra_tests)
  File "/Users/jonathan/anaconda/lib/python2.7/site-packages/django/test/runner.py", line 451, in build_suite
    tests = self.test_loader.discover(start_dir=label, **kwargs)
  File "/Users/jonathan/anaconda/lib/python2.7/unittest/loader.py", line 206, in discover
    tests = list(self._find_tests(start_dir, pattern))
  File "/Users/jonathan/anaconda/lib/python2.7/unittest/loader.py", line 267, in _find_tests
    raise ImportError(msg % (mod_name, module_dir, expected_dir))
ImportError: 'tests' module incorrectly imported from 'FOO/exports/tests'. Expected 'FOO/exports'. Is this module globally installed?
2
  • Can you post the complete stack trace? Commented May 30, 2016 at 11:55
  • Please add the involved file tree, to check the structure and the file where you are trying to import tests
    – trinchet
    Commented May 30, 2016 at 12:03

9 Answers 9

334

In my experience, weird ImportErrors when running tests are caused by an ImportError in the tests module itself.

Ensure that your tests module can be imported:

$ python manage.py shell
...
>>> import foo.exports.tests

Edit:

If that causes an error, make sure you do not have both a directory foo/exports/tests and a file foo/exports/tests.py

9
  • Yes it says that there is no such module. But the file is there. What else can I do?
    – jonalv
    Commented May 30, 2016 at 12:14
  • 121
    Do you maybe have both a directory foo/exports/tests and a file foo/exports/tests.py? Commented May 30, 2016 at 13:05
  • 66
    Oh, there was a Django automagic tests.py file there. Nice catch. And what a cryptic error message... :(
    – jonalv
    Commented May 30, 2016 at 13:08
  • 14
    the django automatic tests.py caught me as well Commented Aug 15, 2016 at 21:05
  • 4
    Your edit is what saved me! When running startapp django created a tests.py file. I was able to import tests.py in the shell Commented Sep 12, 2018 at 0:59
124

As Daniel Hepper said in a comment above, try checking whether you have both a app/tests folder and a app/tests.py file in your app.

Django startapp creates a tests.py file automatically so there might be a file that you haven't noticed.

If you simply delete the automatically generated tests.py file, it should work. (Obviously you should check the contents of the file before deleting anything!)

0
37

In case you have created a directory named tests and have written test files inside it, for example test_views.py, test_models.py, etc., make sure you remove the file test.py created automatically by the command python manage.py startapp.

1
  • It worked for me. In this case, the names are clashing. Django doesn't know if it needs to load tests.py or something inside tests/, Thank you Vipin and dKen. Commented Jun 14 at 15:17
8

In one word: delete test.py or tests folder I had the same issue when I copied some tests I wrote before in one of my projects into my new projects where I had more than 5 same APIs there. Usually I do create a new folder called tests and write all my tests in a folder for each app so everything looks better The mistake I made which led me through this problem was not deleting the test.py file from app folder when I created tests folder in the same app Because you cant have both tests folder and test.py in the same app.

2
  • 3
    That was way more than one word.
    – ron_g
    Commented Mar 24, 2022 at 9:59
  • How does this add anything vs what the top answers say? Commented Apr 11, 2023 at 11:29
5

Try checking whether you have both a app/tests folder and a app/tests.py

file in your app.

By default a file is automatically called tests.py delete this file it the error will be resolved

1
  • How does this add anything to the top answers? Commented Apr 11, 2023 at 11:28
2

Just to add to the list of possible cases.

This can also happen inside a virtual env when the package you're on was locally installed.

In that case you just need to uninstall the version that was installed an "re-link" it (I don't known the correct term) by using the develop command

~/dev/stufflib% pip uninstall stufflib
~/dev/stufflib% python setup.py develop
~/dev/stufflib% python setup.py test
2

make sure you don't have 2 files named test.py in your tree files that way Python should pick the one you wanted to.

1
  • 3
    This has already been suggested in a different answer, consider up voting that answer instead of posting another.
    – User1010
    Commented Feb 21, 2021 at 11:55
1

In my case problem was because I tried to start django test task from symlink to folder with project, not from "real" path. When I run django test task from project folder not using symlink I don't get this error.

0

There are a lot of answers here suggesting different things. None of them quite worked for me, and some seemed not to be true, so I explored the issue and have found a few rules that in part summarise what other have said, but hopefully add a bit to that.

In my case I wanted to test some utilities that were in their own folder, and not a proper app, so the folder I created was missing somethings. When I say "app folder" below, I mean any folder in your project root; in my case it was not a proper app.

I was using Pthon 3.11 by the way.

  1. The file containing the tests must have a name that starts "test" (if not, it will not be found).

  2. By convention, the tests should be in the app folder itself or in a subfolder called "tests", but you can actually call the subfolder anything you like and I suspect put it anywhere you like.

  3. The app folder must have a file called __init__.py in it, even if the file is empty (if not present, it will not be recognised as a Python module and the test not found).

  4. If your test is in a subfolder, that subfolder too must have a file called __init__.py in it (as above).

  5. Ensure you do not have a file and sub-folder with the same name (i.e., a folder "tests" and a file "tests.py") inside the app folder (if you have, you will see "ImportError: 'tests' module incorrectly imported from...").

You do not need a file called "models.py" in the app folder or a file called "apps.py" in it. The app does not need to be listed in INSTALLED_APPs in settings.py (but if it is, then you will need that "apps.py" file).

1
  • The underscore is rendered in markdown as bold text. You can use backticks to write __init__.py
    – jaksco
    Commented Jul 13 at 15:14

Not the answer you're looking for? Browse other questions tagged or ask your own question.