Django Shell没有模块命名设置 [英] Django Shell No module named settings
问题描述
我已经通过 mod_wsgi
将Django部署到Apache。从Apache托管时,Django正常运行。但是,我试图通过 manage.py
进行一些维护,但是当我尝试运行它时,我会收到错误:
I've deployed Django to Apache via mod_wsgi
. Django is running fine when hosted from Apache. However, I'm trying to do some maintenance via manage.py
, but when I try and run it, I get the error:
错误:无法导入设置myproject.settings(是否在sys.path?):没有模块命名设置
Error: Could not import settings 'myproject.settings' (Is it on sys.path?): No module named settings
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环境变量。
Django seems to be ignoring the DJANGO_SETTINGS_MODULE environment variable.
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中的所有内容,除了导入设置
行,并正常运行。
Just to confirm I wasn't going crazy, I commented out everything inside manage.py except the import settings
line, and it ran correctly.
我还尝试设置 os。 environ ['DJANGO_SETTINGS_MODULE'] ='settings'
和 sys.path.append('/ usr / local / myproject')
的manage.py,无济于事。
I've also tried setting os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
and sys.path.append('/usr/local/myproject')
directly at the top of manage.py, to no avail.
这里发生了什么?为什么Django使用错误的设置模块名称?这是令我疯狂的。
What's going on here? Why is Django using the wrong settings module name? This is driving me crazy.
推荐答案
如果您的根目录名称与您的一个应用程序的名称相同,则可能会发生这种情况。例如这里,我有一个名为 bar
的目录,其中包含一个名为code> bar的应用程序的Django项目:
This can happen if your root directory name is the same as the name of one of your apps. For example here I have a directory called bar
containing a Django project with an app also called 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
(或anyt除了 bar
之外的其他选项)解决了这个问题:
Changing the root directory's name to foo
(or anything else other than bar
) solves the problem:
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)
>>>
这篇关于Django Shell没有模块命名设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!