2π
I ran into this error when trying to run a test module residing outside my django project (a practice django doesnβt seem to support):
$ tree
.
βββ example_project
βΒ Β βββ example_app
βΒ Β βΒ Β βββ __init__.py
βΒ Β βΒ Β βββ models.py
βΒ Β βββ __init__.py
βΒ Β βββ manage.py
βΒ Β βββ project
βΒ Β Β Β βββ __init__.py
βΒ Β Β Β βββ settings.py
βΒ Β Β Β βββ urls.py
βΒ Β Β Β βββ wsgi.py
βββ test
βββ test_example_app.py
The error was caused by django assuming a different import path at different points in the process so I resolved it by appending to sys.path
. Iβd be hesitant to do this in a production environment but I can accept some hackiness in tests.
Hereβs what I ended up with in test_example_app.py
:
import os
import sys
import django
from django.conf import settings
from django.test import LiveServerTestCase
from django.test.utils import get_runner
class TestExampleApp(LiveServerTestCase):
...
if __name__ == '__main__':
sys.path.append(os.path.realpath('./example_project'))
os.environ['DJANGO_SETTINGS_MODULE'] = 'example_project.project.settings'
django.setup()
sys.path.append(os.path.realpath('./example_project/project'))
testrunner = get_runner(settings)()
failures = testrunner.run_tests(['test_example_app'])
sys.exit(bool(failures))
1π
The solution, as strange as it seems to me, was I had specified the root folder as the start of the import for books.models in the views.py file. This seemed not just to blow up the admin start, but the error that it gave didnβt give much, if any, indication of the root issue. It was only when I rolled back to python 2.7 that the error message gave me some indication of the root of the problem.
Iβm guessing that this is classic neophyte stuff, since it now seems obvious that the βrootβ of the site should be the folder with manage.py in it, and therefore shouldnβt be in any path specification. Although the book doesnβt seem to state thatβ¦