No Module named django.core

2020-01-24 11:35发布

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:\Python25\Lib\site-packages\django\bin\django-admin.py", line 2, in <module>
    from django.core import management
ImportError: No module named django.core

标签: python django
25条回答
Animai°情兽
2楼-- · 2020-01-24 11:40

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
查看更多
狗以群分
3楼-- · 2020-01-24 11:42

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

查看更多
家丑人穷心不美
4楼-- · 2020-01-24 11:44

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:\Python32\python.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:\Python27\python.exe" "%1" %*

then everythong's okay!

查看更多
【Aperson】
5楼-- · 2020-01-24 11:44

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.

查看更多
做自己的国王
6楼-- · 2020-01-24 11:44

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

查看更多
爷的心禁止访问
7楼-- · 2020-01-24 11:45

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.

查看更多
登录 后发表回答