Thursday, July 09, 2009

ImportError: No module named django.core (fun with Windows)

I just spent 45 minutes tracking this down, hopefully if I post a trace here, others will be able to resolve the issue more quickly in the future.

So I am building a little django app at work, but my latest installed version of django is pre-1.0, so why not upgrade, right?

After getting everything downloaded and running setup.py, I did the standard thing to start a new project:

C:\Users\jtb\Documents\Clickable\src>django-admin.py startproject myproject
Traceback (most recent call last):
File "C:\Python25\Scripts\django-admin.py", line 2, in
from django.core import management
ImportError: No module named django.core

All the search results on that error pointed to issues with my PYTHONPATH, but no amount of tinkering with it would make django actually run.

The real problem was that, due to a recent installation of Python 2.6, my PATH and my PYTHONPATH had fallen out of sync. My PATH was pointing to the Python 2.5 directories, while my PYTHONPATH was pointing to the 2.6 directories. It seems the Python installer I used knows how to update PYTHONPATH but not PATH. Which makes sense.

If you found this post through a web search and it helped you debug a similar issue, please post a comment.

2 comments:

Hello3171 said...

Thank you so much!

You pointed out the problem for me, which before now was beyond me and all the Google searches I did!

I had installed python 2.6, 3.0 and 3.1.
My "python" command (in command prompt) pointed to python 2.6 (in "c:\python26\") whereas when double-clicking python files it loaded up python 3.1!
First, this was always confusing me (as I prefer to work in python 3000 but some libraries such as Django and Pyglet only work in older versions).
I finally fixed it manually by adding "c:\Python26" and "c:\Python31" to PATH and renaming "python.exe" to "python26.exe" and "python31.exe" (I deleted python 3.0). (I also renamed the "pythonw.exe" files)
Furthermore, I removed the double-clicking .py files explorer shortcut to further reduce my own confusion!

However, this means I cannot set a path to the django-admin.py file and start it like, in cmd: "django-admin.py startproject mysite".
But this is okay because now I can be explicit about the python version and save myself a lot of problems!

Now that problem is fixed and I can actually use the django-admin.py script, although... now I must write out the full path to django-admin.py!
Like so: "python26 'c:\python26\scripts\django-admin.py' startproject mysite"

<40 minutes later>
I tried editing site.py to automatically include /Scripts folder (where "django-admin.py" is) but this didn't work!

So unless you've got any ideas on how I can just "python26 django-admin.py", I will have to bear with it as I *finally* get started with the Django tutorial!

Once again, thank you for posting this, I really appreciate it!

Hello3171 said...

Ah, I worked it out.
By using the command line switch "-m" like so:

"python26 -m django-admin startproject mysite"

I also "unmodified" the site.py file and instead added a "scripts.pth" file to the site-packages directory, which was simply:

"../../Scripts/"

Well... this was a lot of hassle... *NOW* I can finally get started! Yay!