So I set up a virtualenv on Ubuntu, installed everything, and got the development site running with runserver. All my own unit tests passed, but many of the Django packages’ unit tests were not passing.
So I tried doing git bisect to figure this out, checking out the very earliest commits I had made. I went too early in history, back when the development environment was using different database engines, packages, etc., and the site of course couldn’t run. I then decided to work on something else more important than failing Django unit tests (since my own were working), and so I went back to the latest commit in the development branch.
I fired up runserver again, but this time when I reloaded the homepage, I was met with the error:
Traceback:
File "...venv.../local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
101. request.path_info)
File "...venv.../local/lib/python2.7/site-packages/django/core/urlresolvers.py" in resolve
298. for pattern in self.url_patterns:
File "...venv.../local/lib/python2.7/site-packages/django/core/urlresolvers.py" in url_patterns
328. patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "...venv.../local/lib/python2.7/site-packages/django/core/urlresolvers.py" in urlconf_module
323. self._urlconf_module = import_module(self.urlconf_name)
File "...venv.../local/lib/python2.7/site-packages/django/utils/importlib.py" in import_module
35. __import__(name)
File "...project dir.../myproject/urls.py" in <module>
2. import frontpage.views
Exception Type: ImportError at /
Exception Value: No module named views
Everything was working just fine before I tried the git bisect. But not any more, at least on my local machine.
So I googled around, and changed my ROOT_URLCONF from 'myproject.urls' to just 'urls'. Now I get this error message:
Traceback:
File "...venv.../local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
101. request.path_info)
File "...venv.../local/lib/python2.7/site-packages/django/core/urlresolvers.py" in resolve
298. for pattern in self.url_patterns:
File "...venv.../local/lib/python2.7/site-packages/django/core/urlresolvers.py" in url_patterns
328. patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "...venv.../local/lib/python2.7/site-packages/django/core/urlresolvers.py" in urlconf_module
323. self._urlconf_module = import_module(self.urlconf_name)
File "...venv.../local/lib/python2.7/site-packages/django/utils/importlib.py" in import_module
35. __import__(name)
Exception Type: ImportError at /
Exception Value: No module named urls
I googled some more and thought maybe my Python path didn’t include my project directory… but no, it totally does when I included a print sys.path in my settings.py file for the project!
So what’s the problem here? I am using Django 1.4.3, and starting the server using python manage.py runserver. I swear my website was working just a few hours ago, and I’ve since done git checkout development and git reset many times… I’ve tried deleting the virtualenv, recreating it, and reinstalling all the Python packages. I’m still getting the same error.
(Also, I did activate the virtualenv, and I’ve even tried restarting, just in case… no luck so far)
UPDATE
Following Lennart’s suggestion, I started the debugger right before the failing import, and found that import frontpage works, but not import frontpage.views, frontpage.tests, or anything else. I do have an __init__.py file in the frontpage directory as well. At this point, sys.path contains my project folder all right…
I have already been including frontpage in my INSTALLED_APPS as well.
FIXED
Thanks to Lennart, I checked that the frontpage module was indeed getting imported correctly. The correct .pyc file was shown, but this made me wonder if the pyc files were somehow out of sync with my actual code.
So I deleted all the pyc files, restarted the server, and everything is working fine once more 🙂
Sometimes you get ImportErrors because the module you try to import encounters some problem. For example maybe
frontpage.viewis trying to import a module that has a SyntaxError. The best way to figure out exactly what is going on is to put animport pdb;pdb.set_trace()just before the failing import and try tostep inside the import.Also make sure that when you import
frontpagethat you get the module back you expect, check specifically the modules__file__attribute, and that it is the__init__.pycyou expect.