Import error while running in PyCharm but not from CLI
In most of my Django projects I use django-gitrevision. It's a middleware that displays the SHA of the git repo the code is running from.
When I launch a project that uses the 'django-gitrevision' middleware from within PyCharm, Django crashes when I request a page in the browser. If I launch the server from the CLI, it runs flawlessly. (Both are running in the virtualenv I created using mkvirtualenv)
To duplicate:
mkdir code
cd code
mkvirtualenv myproject
pip install django
pip install django-gitrevision
django-admin startproject myproject
cd myproject
git init
git add .
git commit -m 'Initial commit'
edit settings.py and
add 'gitrevision.middleware.GitRevision' to the MIDDLEWARE_CLASSES.
add 'gitrevision' to INSTALLED_APPS
add 'gitrevision.context_processors.gitrevision' to TEMPLATE_CONTEXT_PROCESSORS
add GIT_PATH = '/home/whatever/code/myproject'
From the 'myproject' directory run 'python manage.py runserver'
Open your browser to localhost:8000 and you should see the normal page telling you that URLS haven't been configured.
Stop the manage.py runserver command.
Open PyCharm, and open your 'myproject'.
Edit the configuration and point your python interpreter to /home/whatever/.virtualenvs/myproject/bin/python.
Run the server inside PyCharm.
Refresh your browser and you should get an error similar to:
[19/Apr/2012 19:41:58] "GET / HTTP/1.1" 500 59
Traceback (most recent call last):
File "/usr/lib/python2.7/wsgiref/handlers.py", line 85, in run
self.result = application(self.environ, self.start_response)
File "/home/aaron/.virtualenvs/myproject/local/lib/python2.7/site-packages/django/contrib/staticfiles/handlers.py", line 67, in __call__
return self.application(environ, start_response)
File "/home/aaron/.virtualenvs/myproject/local/lib/python2.7/site-packages/django/core/handlers/wsgi.py", line 219, in __call__
self.load_middleware()
File "/home/aaron/.virtualenvs/myproject/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 47, in load_middleware
raise exceptions.ImproperlyConfigured('Error importing middleware %s: "%s"' % (mw_module, e))
ImproperlyConfigured: Error importing middleware gitrevision.middleware: "cannot import name isfile"
The error is being caught and re-raised by Django. It's actually hiding an import error from the python library 'gitdb' which is used by django-gitrevision.
There are several places where gitdb has statements like 'from gitdb.util import isfile'.
When you look at gitdb/util.py it has a line that says 'isfile = os.path.isfile'.
I'm not sure why the developer would import isfile and assign it to a variable inside his own library, then import gitdb.util.isfile everywhere when he/she could just import os.path.isfile directly everywhere.
But why would Django running under PyCharm crash there while running it from the command-line does not?
As I understand it, since I am using the python interpreter from my virtualenv in PyCharm AND when running from the CLI, it should behave identically.
Thanks for a great IDE!
-A
Please sign in to leave a comment.