简体   繁体   中英

Python/Django Test Runner Import Error

So in the joyous tradition of starting a project and not setting up my source control properly before the initial commit, I've gone and broken my Django Test Runner. It appears that the PATH is messed up somewhere, so this probably isn't a Django specific issue.

I can successfully run the django dev server and all project functionality works. I have maybe 70 tests in the project that used to work, until at some point I introduced something that made them not.

Error:

======================================================================
ERROR: app.group.tests (unittest.loader.ModuleImportFailure)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Users/Jamie/.pyenv/versions/3.4.1/lib/python3.4/unittest/case.py", line 58, in testPartExecutor
    yield
  File "/Users/Jamie/.pyenv/versions/3.4.1/lib/python3.4/unittest/case.py", line 577, in run
    testMethod()
  File "/Users/Jamie/.pyenv/versions/3.4.1/lib/python3.4/unittest/loader.py", line 32, in testFailure
    raise exception
ImportError: Failed to import test module: app.group.tests
Traceback (most recent call last):
  File "/Users/Jamie/.pyenv/versions/3.4.1/lib/python3.4/unittest/loader.py", line 312, in _find_tests
    module = self._get_module_from_name(name)
  File "/Users/Jamie/.pyenv/versions/3.4.1/lib/python3.4/unittest/loader.py", line 290, in _get_module_from_name
    __import__(name)
ImportError: No module named 'app.group'

Checking the standards:

(venv) Jamies-MacBook-Pro:app Jamie$ python3
Python 3.4.1 (default, Jun 30 2015, 16:47:23) 
[GCC 4.2.1 Compatible Apple LLVM 6.1.0 (clang-602.0.53)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path
['', '/Users/Jamie/.pyenv/versions/3.4.1/lib/python34.zip', '/Users/Jamie/.pyenv/versions/3.4.1/lib/python3.4', '/Users/Jamie/.pyenv/versions/3.4.1/lib/python3.4/plat-darwin', '/Users/Jamie/.pyenv/versions/3.4.1/lib/python3.4/lib-dynload', '/Users/Jamie/.pyenv/versions/venv/lib/python3.4/site-packages']

There is nothing obvious that could be messing with PATH, and I've hit the stage where I have lots of untested functionality that I can't deploy (and an impending release date). Any ideas?

So after digging around, it turns out that there is a known caveat with the Django Test Runner. If you have a dunder-init in the root project folder, this issue will occur. The offending culprit is the first file below:

jamiestrauss@jamies-air-2:~/projects/app$ tree
.
├── __init__.py
├── app
│   ├── __init__.py
│   ├── celery.py
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
├── client
│   ├── __init__.py
│   ├── admin.py
│   ├── api.py
│   ├── migrations

Of course, the existence of the Shebang! in manage.py , coupled with the dunder-init means that the entire application is imported as a module, which messes up the internal reference.

tl;dr - if this happens to you delete the root __init__.py in the /project/ directory.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM