Django Shell没有模块命名设置

时间:2021-07-06 19:20:32

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:

我已经通过mod_wsgi将Django部署到了Apache。从Apache托管时,Django运行正常。但是,我正在尝试通过manage.py进行一些维护,但是当我尝试运行它时,我收到错误:

Error: Could not import settings 'myproject.settings' (Is it on sys.path?): No module named settings

错误:无法导入设置'myproject.settings'(是否在sys.path上?):没有名为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 seems to be ignoring the DJANGO_SETTINGS_MODULE environment variable.

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
>>> 

Just to confirm I wasn't going crazy, I commented out everything inside manage.py except the import settings line, and it ran correctly.

为了确认我没有发疯,我在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.

我也尝试直接在manage.py的顶部设置os.environ ['DJANGO_SETTINGS_MODULE'] ='settings'和sys.path.append('/ usr / local / myproject'),但无济于事。

What's going on here? Why is Django using the wrong settings module name? This is driving me crazy.

这里发生了什么?为什么Django使用错误的设置模块名称?这真让我抓狂。

9 个解决方案

#1


29  

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:

如果您的根目录名称与您的某个应用程序的名称相同,则会发生这种情况。例如,这里有一个名为bar的目录,其中包含一个带有应用程序的Django项目,也称为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

Changing the root directory's name to foo (or anything else other than bar) solves the problem:

将根目录的名称更改为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)
>>> 

#2


7  

It seems the path to your project isn't being recognized by wsgi. This has happened to me, and to solve it I added this to the top of my .wsgi file:

Wsgi似乎无法识别您项目的路径。这发生在我身上,为了解决这个问题,我将其添加到我的.wsgi文件的顶部:

import os
import sys

root_path = os.path.abspath(os.path.split(__file__)[0])
sys.path.insert(0, os.path.join(root_path, 'project_name'))
sys.path.insert(0, root_path)

#3


7  

I had a similar problem, where the same error was being returned when I tried to run django-admin.py startproject myapp. A previous answer here helped me figure it out. The problem was that I had previously pointed DJANGO_SETTINGS_MODULE to a certain file, which I had later deleted. To fix it, I just removed the pointer with this command:

我遇到了类似的问题,当我尝试运行django-admin.py startproject myapp时,返回了相同的错误。之前的回答帮助我弄清楚了。问题是我之前已将DJANGO_SETTINGS_MODULE指向某个文件,我后来删除了该文件。要修复它,我只是用这个命令删除了指针:

export DJANGO_SETTINGS_MODULE=

导出DJANGO_SETTINGS_MODULE =

#4


2  

Though Simon Whitaker's answer (that a same-name dir is confusing things) is certainly on point, rather than suggesting you change your entire extant dir structure, might I suggest:

虽然西蒙·惠特克的答案(一个同名的目录令人困惑的事情)当然是关键,而不是建议你改变现有的整个目录结构,我可以建议:

Instead of using the "malfunctioning" / ambiguous...

而不是使用“故障”/模糊......

import settings

...use the more specific...

...使用更具体的......

from django.conf import settings

#5


2  

Somehow, if your project folder is the same name as the app that has the settings file in it, and if you have __init__.py in the project root folder, it will mess wsgi. I really dont understand why but removing this file solved this for me.

不知何故,如果您的项目文件夹与其中包含设置文件的应用程序的名称相同,并且如果项目根文件夹中有__init__.py,则会使wsgi混乱。我真的不明白为什么但删除这个文件解决了这个问题。

#6


1  

Since your web app is working, check that you're running manage.py with the same python interpreter that's defined in your .wsgi file (and if you append other directories to sys.path in your .wsgi file, make sure they're in the pythonpath here too).

由于您的Web应用程序正在运行,请检查您运行的manage.py是否使用.wsgi文件中定义的相同python解释器(如果您将其他目录附加到.wsgi文件中的sys.path,请确保它们是在这里的pythonpath)。

If you try to import something in your settings file that throws an ImportError, Django tells you settings cannot be imported. Newer versions of django will mention (If the file settings.py does indeed exist, it's causing an ImportError somehow.) and I've run into this a few times.

如果您尝试在设置文件中导入抛出ImportError的内容,Django会告诉您无法导入设置。较新版本的django会提到(如果文件settings.py确实存在,它会以某种方式导致ImportError。)我已经碰到了几次。

If it's not that, maybe try using django-admin.py instead, just in case something has gone wrong in your manage.py file. AFAIK there is no good reason to modify manage.py directly.

如果不是这样,也许可以尝试使用django-admin.py,以防万一在manage.py文件中出现问题。 AFAIK没有充分的理由直接修改manage.py.

#7


1  

I had accidentally changed my DJANGO_SETTINGS_MODULE variable using the echo command: echo DJANGO_SETTINGS_MODULE=mysite.settings

我使用echo命令意外更改了我的DJANGO_SETTINGS_MODULE变量:echo DJANGO_SETTINGS_MODULE = mysite.settings

I simply quit virtualenv and activated it again, which restored my settings.

我只是退出virtualenv并再次激活它,这恢复了我的设置。

#8


1  

If you are using wsgi/uwsgi in production...

如果你在生产中使用wsgi / uwsgi ......

I was having the same error:

我遇到了同样的错误:

If you renamed the folder that django startproject created that has setting.py files and wsgi.py , check in the wsgi.py file the line: os.environ.setdefault("DJANGO_SETTINGS_MODULE", "<your_folder_name>.settings")

如果您重命名django startproject创建的具有setting.py文件和wsgi.py的文件夹,请在wsgi.py文件中检查以下行:os.environ.setdefault(“DJANGO_SETTINGS_MODULE”,“ .settings”)

In my case i had to rename < your_folder_name> also.

在我的情况下,我也必须重命名

#9


0  

Please check for compatibility between the virtualenv version and the django version. when it matches, it works like a gem.

请检查virtualenv版本和django版本之间的兼容性。当它匹配时,它就像一个宝石。

#1


29  

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:

如果您的根目录名称与您的某个应用程序的名称相同,则会发生这种情况。例如,这里有一个名为bar的目录,其中包含一个带有应用程序的Django项目,也称为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

Changing the root directory's name to foo (or anything else other than bar) solves the problem:

将根目录的名称更改为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)
>>> 

#2


7  

It seems the path to your project isn't being recognized by wsgi. This has happened to me, and to solve it I added this to the top of my .wsgi file:

Wsgi似乎无法识别您项目的路径。这发生在我身上,为了解决这个问题,我将其添加到我的.wsgi文件的顶部:

import os
import sys

root_path = os.path.abspath(os.path.split(__file__)[0])
sys.path.insert(0, os.path.join(root_path, 'project_name'))
sys.path.insert(0, root_path)

#3


7  

I had a similar problem, where the same error was being returned when I tried to run django-admin.py startproject myapp. A previous answer here helped me figure it out. The problem was that I had previously pointed DJANGO_SETTINGS_MODULE to a certain file, which I had later deleted. To fix it, I just removed the pointer with this command:

我遇到了类似的问题,当我尝试运行django-admin.py startproject myapp时,返回了相同的错误。之前的回答帮助我弄清楚了。问题是我之前已将DJANGO_SETTINGS_MODULE指向某个文件,我后来删除了该文件。要修复它,我只是用这个命令删除了指针:

export DJANGO_SETTINGS_MODULE=

导出DJANGO_SETTINGS_MODULE =

#4


2  

Though Simon Whitaker's answer (that a same-name dir is confusing things) is certainly on point, rather than suggesting you change your entire extant dir structure, might I suggest:

虽然西蒙·惠特克的答案(一个同名的目录令人困惑的事情)当然是关键,而不是建议你改变现有的整个目录结构,我可以建议:

Instead of using the "malfunctioning" / ambiguous...

而不是使用“故障”/模糊......

import settings

...use the more specific...

...使用更具体的......

from django.conf import settings

#5


2  

Somehow, if your project folder is the same name as the app that has the settings file in it, and if you have __init__.py in the project root folder, it will mess wsgi. I really dont understand why but removing this file solved this for me.

不知何故,如果您的项目文件夹与其中包含设置文件的应用程序的名称相同,并且如果项目根文件夹中有__init__.py,则会使wsgi混乱。我真的不明白为什么但删除这个文件解决了这个问题。

#6


1  

Since your web app is working, check that you're running manage.py with the same python interpreter that's defined in your .wsgi file (and if you append other directories to sys.path in your .wsgi file, make sure they're in the pythonpath here too).

由于您的Web应用程序正在运行,请检查您运行的manage.py是否使用.wsgi文件中定义的相同python解释器(如果您将其他目录附加到.wsgi文件中的sys.path,请确保它们是在这里的pythonpath)。

If you try to import something in your settings file that throws an ImportError, Django tells you settings cannot be imported. Newer versions of django will mention (If the file settings.py does indeed exist, it's causing an ImportError somehow.) and I've run into this a few times.

如果您尝试在设置文件中导入抛出ImportError的内容,Django会告诉您无法导入设置。较新版本的django会提到(如果文件settings.py确实存在,它会以某种方式导致ImportError。)我已经碰到了几次。

If it's not that, maybe try using django-admin.py instead, just in case something has gone wrong in your manage.py file. AFAIK there is no good reason to modify manage.py directly.

如果不是这样,也许可以尝试使用django-admin.py,以防万一在manage.py文件中出现问题。 AFAIK没有充分的理由直接修改manage.py.

#7


1  

I had accidentally changed my DJANGO_SETTINGS_MODULE variable using the echo command: echo DJANGO_SETTINGS_MODULE=mysite.settings

我使用echo命令意外更改了我的DJANGO_SETTINGS_MODULE变量:echo DJANGO_SETTINGS_MODULE = mysite.settings

I simply quit virtualenv and activated it again, which restored my settings.

我只是退出virtualenv并再次激活它,这恢复了我的设置。

#8


1  

If you are using wsgi/uwsgi in production...

如果你在生产中使用wsgi / uwsgi ......

I was having the same error:

我遇到了同样的错误:

If you renamed the folder that django startproject created that has setting.py files and wsgi.py , check in the wsgi.py file the line: os.environ.setdefault("DJANGO_SETTINGS_MODULE", "<your_folder_name>.settings")

如果您重命名django startproject创建的具有setting.py文件和wsgi.py的文件夹,请在wsgi.py文件中检查以下行:os.environ.setdefault(“DJANGO_SETTINGS_MODULE”,“ .settings”)

In my case i had to rename < your_folder_name> also.

在我的情况下,我也必须重命名

#9


0  

Please check for compatibility between the virtualenv version and the django version. when it matches, it works like a gem.

请检查virtualenv版本和django版本之间的兼容性。当它匹配时,它就像一个宝石。