我已通过将Django部署到Django mod_wsgi。从Apache托管时,Django运行良好。但是,我试图通过进行一些维护manage.py,但是当我尝试运行它时,出现错误消息:
Django mod_wsgi
manage.py
错误:无法导入设置“ myproject.settings”(在sys.path上吗?):没有名为设置的模块
user@localhost:~$ cd /usr/local/myproject user@localhost:/usr/local/myproject$ ls drwxr-xr-x 2 apache apache 4096 2011-09-07 19:38 apache -rw-r--r-- 1 apache apache 0 2011-05-25 14:52 __init__.py -rw-r--r-- 1 apache apache 813 2011-09-09 16:56 manage.py drwxr-xr-x 6 apache apache 4096 2011-09-09 16:43 myapp -rw-r--r-- 1 apache apache 4992 2011-09-07 19:31 settings.py drwxr-xr-x 4 apache apache 4096 2011-09-08 20:32 templates -rw-r--r-- 1 apache apache 1210 2011-09-08 14:49 urls.py
Django似乎忽略了DJANGO_SETTINGS_MODULE环境变量。
user@localhost:~$ cd /usr/local/myproject user@localhost:/usr/local/myproject$ export DJANGO_SETTINGS_MODULE=settings user@localhost:/usr/local/myproject$ python manage.py shell Error: Could not import settings 'myproject.settings' (Is it on sys.path?): No module named settings user@localhost:/usr/local/myproject$ python Python 2.6.5 (r265:79063, Apr 16 2010, 13:57:41) [GCC 4.4.3] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import settings >>>
为了确认我没有发疯,我注释掉了manage.py中除import settings行外的所有内容,并正确运行了该行。
我也试着设置os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'和sys.path.append('/usr/local/myproject')直接manage.py的顶部,都无济于事。
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
sys.path.append('/usr/local/myproject'
这里发生了什么?Django为什么使用错误的设置模块名称?这真让我抓狂。
如果你的根目录名称与其中一个应用程序的名称相同,则可能发生这种情况。例如,在这里,我有一个名为的目录,bar其中包含一个Django项目以及一个应用程序bar:
bar
Simons-MacBook-Pro ~/temp $ cd bar Simons-MacBook-Pro ~/temp/bar $ ./manage.py shell Error: Could not import settings 'bar.settings' (Is it on sys.path?): No module named settings Simons-MacBook-Pro ~/temp/bar $ ls -l total 48 -rw-r--r-- 1 simon staff 0 25 Oct 10:46 __init__.py -rw-r--r-- 1 simon staff 130 25 Oct 10:46 __init__.pyc drwxr-xr-x 7 simon staff 238 25 Oct 10:46 bar -rwxr-xr-x 1 simon staff 503 25 Oct 10:46 manage.py -rw-r--r-- 1 simon staff 5025 25 Oct 10:46 settings.py -rw-r--r-- 1 simon staff 2658 25 Oct 10:46 settings.pyc -rw-r--r-- 1 simon staff 556 25 Oct 10:46 urls.py
将根目录的名称更改为foo(或以外的其他名称bar)可以解决此问题:
Simons-MacBook-Pro ~/temp/bar $ cd .. Simons-MacBook-Pro ~/temp $ mv bar foo Simons-MacBook-Pro ~/temp $ cd foo Simons-MacBook-Pro ~/temp/foo $ ./manage.py shell Python 2.7.1 (r271:86832, Jun 16 2011, 16:59:05) [GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2335.15.00)] on darwin Type "help", "copyright", "credits" or "license" for more information. (InteractiveConsole) >>>