No Module named django.core

Question:

I have updated to latest Django version 1.0.2 after uninstalling my old Django version.But now when I run django-admin.py I get the following error. How can I resolve this?

Traceback (most recent call last):
  File "C:Python25Libsite-packagesdjangobindjango-admin.py", line 2, in <module>
    from django.core import management
ImportError: No module named django.core
Asked By: Sirish

||

Answers:

You must make sure that django is in your PYTHONPATH.

To test, just do a import django from a python shell. There should be no output:

ActivePython 2.5.1.1 (ActiveState Software Inc.) based on
Python 2.5.1 (r251:54863, May  1 2007, 17:47:05) [MSC v.1310 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import django
>>>

If you installed django via setuptools (easy_install, or with the setup.py included with django), then check in your site-packages if the .pth file (easy-install.pth, django.pth, …) point to the correct folder.

HIH.

Answered By: Fil

I’m sure it’s related to something incorrect in my setup, but it works properly if I do this:

python c:Python26scriptsdjango-admin.py startproject mysite
Answered By: cmsjr

As usual, an install script failed to set world read/execute permissions 🙂
Do this:

sudo find  /usr/lib/python2.5/site-packages/django -type d -exec chmod go+rx {} ;
sudo find  /usr/lib/python2.5/site-packages/django -type f -exec chmod go+r {} ;
Answered By: greg

You can get around this problem by providing the full path to your django-admin.py file

python c:python25scriptsdjango-admin.py startproject mysite
Answered By: Niyaz

I encountered this problem today, it turned out that I had C:Python26 in my path and .py files were associated to Python 3.1. Repairing the proper version of Python, either through Programs and Features or by running the .msi, will fix the associations.

Answered By: Steve McKay

This worked for me with bitnami djangostack:

python appsdjangodjangobindjango-admin.py startproject mysite
Answered By: Tod

If your path variables are correct and from the python shell you can do: from django.core import management , make sure you’re including “python” before “django-admin.py” as such: python django-admin.py startproject thelittlethings

Answered By: Mindspam

Thanks for posting the question and answers. I have two versions of Python installed, but root was pointing to the /usr/bin version, and I wanted to use the 2.7 version of Python in /usr/local/bin. After rebuilding/reinstalling Django and mysqldb, all is well and I’m not getting the error.

Answered By: octopusgrabbus

Small quick fix is just to create symlink ln -s $SOMEWHERE/lib/python2.6/site-packages/django/ ./django

Answered By: sultan

I had the same problem, it was clear that I had a PYTHONPATH configuration issue. The solution is quite simple, just create a file with this name django.pth in your PYTHONHOMELibsite-packages directory where PYTHONHOME is the directory where Python is installed (mine is: C:Python27). Add the following line to the django.pth file:

PYTHONHOMELibsite-packagesdjango

Of course you have to change PYTHONHOME to your Python’s installation directory as I explained.

Note that you can edit the django.pth to include any directory that you want to be included in the PYTHONPATH. Actually, you can name that file as you wish, path.pth for example if you want it to be more general and to include several directory paths.

Answered By: wassimans

I had the same problem in windows xp. The reason was i installed multiple python versions(2.6,3.2).windows’s PATH is set correctly to python26, but the .py file is associated with python32. I want the .py file is associated with python26.To solve it, the easit way is to right click the *.py(such as django-admin.py),choose “open with”->”choose program…”->”Browse…” (select c:python26python.ext)->”Ok”. Then we can run django-admin.py in the cmd without the need for the expatiatory prefix “c:python26libsite-packagesdjangobin”.

Answered By: af999

The simplest solution though not the most elegant is to copy the django-admin.py from the Scripts folder. The Scripts folder will be found under your Python installation . On my computer it is in C:Python26Scripts. Then paste the django-admin.py into the folder you are trying to run the file from.
It is true that the use of the System path will give flexibility. This is a particular solution if you are in a hurry. Then type for instance python django-admin.py startproject fluffyteaspoons and you will create the project fluffyteaspoons

Answered By: Kieran

It was a PYTHONPATH environment variable issue for me, as others mentioned above, but noone has really shown how to set it for people that could use the extra instruction.

Linux (bash)

I set this variable in my bashrc file in my home folder (.bashrc is the file for me since my shell is /bin/bash).

vim ~/.bashrc
export PYTHONPATH=/usr/local/python-2.7.2/lib/python2.7/site-packages:$PYTHONPATH
source ~/.bashrc

The path should be wherever your django source is. Mine is located at /usr/local/python-2.7.2/lib/python2.7/site-packages/django, so I just specified /usr/local/python-2.7.2/lib/python2.7/site-packages without the django portion.

OSX

On OSX the path is /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages and you can add it to /etc/profile:

sudo echo "PYTHONPATH=/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages:$PYTHONPATH" >> /etc/profile
source /etc/profile
Answered By: Banjer

In my case, I’m on OS X Lion and I’ve installed Python with homebrew
I was getting the same error, but none of the solutions posted here helped me. In my case I just had to edit the script:

vim /usr/local/share/python/django-admin.py

And I noticed that the first line was wrong, as it was pointing to the system’s python installation:

#!/usr/bin/python

I just modified it to point to homebrew’s installation:

#!/usr/local/bin/python

And it worked 🙂

Answered By: David Morales

I know that this is an old question, but I just had the same problem,
in my case, it was because the I am using virtualenv with django, but .py file extensions in Windows are associated with the main Python installation, so running the django-admin.py are directly from the command prompt causes it run with the main Python installation without django installed.

So, since i dont know if there is any hash pound equivalent in Windows, I worked around this by running python followed by the full path of the django-admin.py, or you can also modify the virtualenv batch script to change the file associations and change it back when you deactivate it (although I am not sure how to do it, as I am not really familiar with batch script).

Hope this helps,

Answered By: hndr

I have the same problem on Windows and it seems I’ve found the problem. I have both 2.7 and 3.x installed. It seems it has something to do with the associate program of .py:

In commandline type:

assoc .py

and the result is:

.py=Python.File

which means .py is associated with Python.File

then I tried this:

ftype Python.File

I got:

Python.File=”C:Python32python.exe” “%1” %*

which means in commandline .py is associated with my Python 3.2 installation — and that’s why I can’t just type “django-admin.py blah blah” to use django.

ALL you need to do is change the association:

ftype Python.File=”C:Python27python.exe” “%1” %*

then everythong’s okay!

Answered By: Noah

Well.. I do something radical. I unistall python and I delete from Environment Variables/PATH this: ;C:Python26Scripts;C:Python26.
And Its work…
I had your problem before.

Answered By: kry

This worked on Mac OS X

In the terminal run python
In python:
import sys
print sys.path

Look for the site packages path. I found this in the output of sys.path:
‘/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages’

exit python. Find where your current site-packages are. Mine were at /Library/Python/2.6/site-packages

Now be careful: Check the content of site-packages to be sure it is empty. That is, directory /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages should be empty, or just contain a readme file. If it is, delete that directory, because you are now about to make a symlink.

ln -s /Library/Python/2.6/site-packages /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages

If you don’t delete the folder you will put the symlink in the folder.

Other options are to add the path to the sys.path. I elected the symlink route because I have a couple of versions of python, I don’t want several versions of Django, and just wanted to point to the known working copy.

Answered By: kd4ttc

I had the same problem and recalled that I had installed iPython the previous afternoon. I uninstalled iPython and the problem went away. Incidentally, I am working with virtualenv. I had installed iPython in the system-wide site-packages directory, so I just re-installed iPython inside each virtualenv that I am using. After installing iPython yesterday I had noticed a warning message (from version 0.13 of iPython) that it had detected that I was using virtualenv and that if I ran into any trouble I should install iPyton inside each virtualenv.

Answered By: tadasajon

I just got the same ImportError in Windows Vista having Python 2.7 and 3.2 installed and using virualenv with Python 2.7 and Django 1.4.3.

I changed the file association of .py files in Explorer from 3.2 to 2.7. Rightclicking a .py file and changing settings. I still got the ImportError.

I ran cmd.exe as an administrator and copypasted the earlier ftype stuff. After an error, noted that double quotes don’t get copied correctly from browser to cmd. Rewrote the command in cmd, but I still got the ImportError.

In root of the actived virtual environment, I explicitly gave the python command and the path to django-admin.py from there.
(env_p27) C:UsersGemmuenv_p27>python .Scriptsdjango-admin.py startproject mysite

That worked.

Thanks for all the help for everyone.

Answered By: Gemmu

If you are using Windows, then Don’t run with ‘django-admin.py’, since this will call your outer python.exe to execute and it cannot read into the django inside the virtual environemnt. Try ‘python django-admin.py’ to use your python.exe inside your environment.

Answered By: lichenbo

This happened to me because I ran pip as sudo while my virtualenv is setup to not import outside site packages so Django was installed for the root user but not in the virtualenv, even though I had virtualenv activated when I ran sudo.

Solution switch to root, activate venv then do pip install.

Answered By: Emil Davtyan

I realized this happen because I didn’t run python setup.py install. That setup the environment.

Answered By: LynxLee

To tack on to what lichenbo said… If you are using Windows, then Don’t run with ‘django-admin.py’…

I’m running inside a virtual environment, so the path and command to create a new project looks like:

(DjangoEnv)  C:userstimreillyEnvsdjangoScriptsdjango-admin.exe startproject myproject

DjangoEnv is the name of my virtual environment.

Answered By: Tim Reilly

After reading a lot I found a solution that works for me.

I have

 django version 1.9.2
  1. Set all system variable in “path”.No duplicate copy in user variable nor in the PYTHONPATH .
    enter image description here

  2. Then set Value in regedit

enter image description here

  1. made a virtualenv in desired folder by command

    virtualenv VENV

  2. And finally in cmd

used command

django-admin.py startproject MySite

***and not this***

python django-admin.py startproject MySite 

at the root folder of the “VENV”

And It worked

Answered By: hemant c
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.