Got an import error all of a sudden

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.

  • Creating a submodule in a git project
  • Using Git For Community-Oriented Website Content Revision System
  • Add a subdirectory of another project as a subtree
  • django migrations - workflow with multiple dev branches
  • Tips on using Git with svn in presence of a large number of svn externals
  • Retrieve private repository commits from github
  • 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 🙂

  • How to git push to remote if remote does not exist at all
  • PHPStorm keypair setup to use gitlab
  • Cygwin and gitolite 3. How to set it up on windows?
  • Why is git staging files for my checkin which were picked up in a merge, but untouched by me?
  • error with p4merge merging tool in git
  • Visualizing branch topology in git
  • One Solution collect form web for “Got an import error all of a sudden”

    Sometimes you get ImportErrors because the module you try to import encounters some problem. For example maybe frontpage.view is trying to import a module that has a SyntaxError. The best way to figure out exactly what is going on is to put an import pdb;pdb.set_trace() just before the failing import and try to step inside the import.

    Also make sure that when you import frontpage that you get the module back you expect, check specifically the modules __file__ attribute, and that it is the __init__.pyc you expect.

    Git Baby is a git and github fan, let's start git clone.