错误:在为Django设置virtualenv后无法导入设置'mysite.settings'

Cpp*_*ner 5 django fedora

我在Fedora上这样做

问题:

(sandbox)[root@localhost mysite]# django-admin.py runserver
Error: Could not import settings 'mysite.settings' (Is it on sys.path?): No module named mysite.settings
Run Code Online (Sandbox Code Playgroud)

设置virtualenv

mkdir pythonenv           # that's the /home/yeukhon/pythonenv/*.*
cd pythonenv
virtualenv --no-site-packages --distribute sandbox
Run Code Online (Sandbox Code Playgroud)

安装Django

pip install -E sandbox django

#    changing mode of /home/yeukhon/pythonenv/sandbox/bin/django-admin.py to 755
#    Successfully installed django
Run Code Online (Sandbox Code Playgroud)

在/ home/yeukhon/pythonenv/sandbox下

bin   include  lib  mysite
Run Code Online (Sandbox Code Playgroud)

在lib下

You have /lib/python2.7/site-packages/django/*.*
Run Code Online (Sandbox Code Playgroud)

创建项目很好

(sandbox)[root@localhost sandbox]# django-admin.py startproject mysite
# the path is now /home/yeukhon/pythonenv/sandbox/mysite/*.*
Run Code Online (Sandbox Code Playgroud)

无法运行服务器

django-admin.py runserver
Error: Could not import settings 'mysite.settings' (Is it on sys.path?): No module named mysite.settings
Run Code Online (Sandbox Code Playgroud)

沙箱下的Python Shell (遵循本指南:如何排除故障 - ImportError:在部署django时无法导入设置'mysite.settings'?)

(sandbox)[root@localhost mysite]# python
Python 2.7.2 (default, Oct 27 2011, 01:36:46) 
[GCC 4.6.1 20111003 (Red Hat 4.6.1-10)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import django
>>> import os
>>> os.environ['DJANGO_SETTINGS_MODULE'] 
'mysite.settings'

>>> os.path.exists('/home')
True
>>> os.path.exists('/home/yeukhon/pythonenv/sandbox/mysite')
True
>>> os.path.exists('/home/yeukhon/pythonenv/sandbox/mysite/settings.py')
True
>>> from django.core.management import setup_environ
>>> import mysite.settings
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named mysite.settings

>>> setup_environ(mysite.settings)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'mysite' is not defined


>>> print sys.path
['', 
/home/yeukhon/pythonenv/sandbox/lib/python2.7/site-packages/distribute-0.6.14-py2.7.egg', 
'/home/yeukhon/pythonenv/sandbox/lib/python2.7/site-packages/pip-0.8.1-py2.7.egg', 
'/home/yeukhon/pythonenv/sandbox/lib/python27.zip',
'/home/yeukhon/pythonenv/sandbox/lib/python2.7',
'/home/yeukhon/pythonenv/sandbox/lib/python2.7/plat-linux2',
'/home/yeukhon/pythonenv/sandbox/lib/python2.7/lib-tk',
'/home/yeukhon/pythonenv/sandbox/lib/python2.7/lib-old',
'/home/yeukhon/pythonenv/sandbox/lib/python2.7/lib-dynload',
'/usr/lib/python2.7',
'/usr/lib/python2.7/plat-linux2',
'/home/yeukhon/pythonenv/sandbox/lib/python2.7/site-packages'
Run Code Online (Sandbox Code Playgroud)

我需要做些什么来纠正这个问题?感谢您的时间.


编辑

谢谢你的回复.

我尝试了以下方法:

(sandbox)[root@localhost mysite]# export PYTHONPATH="/home/yeukhon/pythonenv/sandbox/"
(sandbox)[root@localhost mysite]# export PYTHONPATH="/home/yeukhon/pythonenv/"
(sandbox)[root@localhost mysite]# deactivate
[root@localhost mysite]# source ../bin/activate
(sandbox)[root@localhost mysite]# django-admin.py runserver
Error: Could not import settings 'mysite.settings' (Is it on sys.path?): No module named mysite.settings

>>> sys.path
['',.... '/home/yeukhon/pythonenv'.....]
Run Code Online (Sandbox Code Playgroud)

它现在在python路径上.但我仍然无法运行服务器.

集中的Django项目

是.这是一个很好的建议.所以我想我需要做的就是"创建一个名为mydjango的目录,然后在mydjango中创建项目".但是需要更改/添加哪些命令?我愿意学习好的做法.

非常感谢你.


解决方案(添加到环境变量)

PYTHONPATH=$PYTHONPATH:path-to-your-directory

# PYTHONPATH=$PYTHONPATH:/home/yeukhon/pythonenv/sandbox/
Run Code Online (Sandbox Code Playgroud)

Chr*_*att 5

最后一行告诉您需要知道的一切.要导入mysite.settings,父目录mysite必须在PYTHONPATH上.目前不是.

FWIW,将项目实际存储 virtualenv目录中并不典型.通常,您将所有项目放在PYTHONPATH上的目录中.然后,只需加载你需要的所有功能,一切都很好.事实上,virtualenv最好的部分是它们是可以互换的; 也就是说,您可以轻松地在多个不同的virtualenv环境中运行相同的项目(例如在不改变您的正常virtualenv的情况下测试Django的新版本),但同样,您希望将项目放在一个集中的位置,而不是在特定的virtualenv目录中.