1

因此,首先,直到今天,这一切都运行良好。

当我今天跑的时候python manage.py runserver,它吐了一个错误,说

Traceback (most recent call last):
  File "manage.py", line 8, in <module>
    from django.core.management import execute_from_command_line
ImportError: No module named django.core.management

经过大量挖掘,我发现我的 django 安装在/usr/local/lib/python2.7/site-packages/(就是pip install django放它的地方),但是 python 现在正在寻找它/Library/Python/2.7/site-packages

我如何告诉 python django 安装在上面的那个目录中?

仅供参考,这是我的 sys.path

['', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip', 
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7', 
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin', 
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac', 
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-
scriptpackages', '/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python', 
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk', 
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old', 
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload', 
'/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/PyObjC', 
'/Library/Python/2.7/site-packages']
4

2 回答 2

2

您可以通过在终端中执行以下操作将路径添加到 python 路径

sudo vi ~/.bash_profile

然后将此行添加到 .bash_profile 文件中

export PYTHONPATH=/usr/local/lib/python2.7/site-packages/

退出终端并再次打开它。现在你应该很好

于 2013-08-14T04:33:59.800 回答
1

你可以尝试这样的事情。

sudo vi ~/.bash_profile
export PYTHONSTARTUP=/usr/local/lib/python2.7/site-packages/

and modify sys.path at runtime with this

import sys
sys.path.insert(0, '/usr/local/lib/python2.7/site-packages')
that could be necessary later for your apache and .wsgi files.
于 2013-08-14T05:00:41.307 回答