如何为我的 Django 项目正确设置 DJANGO_SETTINGS_MODULE (我正在使用 virtualenv)?

发布于 2024-12-26 04:03:44 字数 3234 浏览 4 评论 0原文

我在为 Django 项目设置 DJANGO_SETTINGS_MODULE 时遇到一些问题。

我在 ~/dev/django-project 有一个目录。在这个目录中,我有一个使用 virtualenv 设置的虚拟环境,还有一个名为“blossom”的 django 项目,其中有一个名为“onora”的应用程序。从 ~/dev/django-project/ 运行 tree -L 3 显示以下内容:

.
├── Procfile
├── blossom
│   ├── __init__.py
│   ├── __init__.pyc
│   ├── fixtures
│   │   └── initial_data_test.yaml
│   ├── manage.py
│   ├── onora
│   │   ├── __init__.py
│   │   ├── __init__.pyc
│   │   ├── admin.py
│   │   ├── admin.pyc
│   │   ├── models.py
│   │   ├── models.pyc
│   │   ├── tests.py
│   │   └── views.py
│   ├── settings.py
│   ├── settings.pyc
│   ├── sqlite3-database
│   ├── urls.py
│   └── urls.pyc
├── blossom-sqlite3-db2
├── requirements.txt
└── virtual_environment
    ├── bin
    │   ├── activate
    │   ├── activate.csh
    │   ├── activate.fish
    │   ├── activate_this.py
    │   ├── django-admin.py
    │   ├── easy_install
    │   ├── easy_install-2.7
    │   ├── gunicorn
    │   ├── gunicorn_django
    │   ├── gunicorn_paster
    │   ├── pip
    │   ├── pip-2.7
    │   ├── python
    │   └── python2.7 -> python
    ├── include
    │   └── python2.7 -> /System/Library/Frameworks/Python.framework/Versions/2.7/include/python2.7
    └── lib
        └── python2.7

我正在尝试使用以下命令从数据库转储我的数据

django-admin.py dumpdata

我的方法是运行 cd ~/dev/django-project ,然后运行 ​​source virtual_environment/bin/activate ,然后运行 ​​django-admin.py dumpdata

但是,我收到以下错误:

ImportError: Settings cannot be imported, because environment variable DJANGO_SETTINGS_MODULE is undefined.

我做了一些谷歌搜索并找到此页面:https://docs.djangoproject。 com/en/dev/topics/settings/#designating-the-settings

这告诉我

当你使用 Django 时,你必须告诉它你正在使用哪些设置。 通过使用环境变量 DJANGO_SETTINGS_MODULE 来执行此操作。这 DJANGO_SETTINGS_MODULE 的值应该采用 Python 路径语法,例如 我的网站.设置。注意设置模块应该在Python上 导入搜索路径。

按照 Setting DJANGO_SETTINGS_MODULE under virtualenv? 的建议,我将这些行附加

export DJANGO_SETTINGS_MODULE="blossom.settings"
echo $DJANGO_SETTINGS_MODULE

到 virtual_environment/垃圾箱/激活。现在,当我运行 activate 命令以激活虚拟环境时,我得到输出:

DJANGO_SETTINGS_MODULE set to blossom.settings

这对我来说看起来不错,但现在我遇到的问题是运行

django-admin.py dumpdata

返回以下错误:

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

我做错了什么?如何检查sys.path?这应该如何运作?

谢谢。

I am having some trouble setting the DJANGO_SETTINGS_MODULE for my Django project.

I have a directory at ~/dev/django-project. In this directory I have a virtual environment which I have set up with virtualenv, and also a django project called "blossom" with an app within it called "onora". Running tree -L 3 from ~/dev/django-project/ shows me the following:

.
├── Procfile
├── blossom
│   ├── __init__.py
│   ├── __init__.pyc
│   ├── fixtures
│   │   └── initial_data_test.yaml
│   ├── manage.py
│   ├── onora
│   │   ├── __init__.py
│   │   ├── __init__.pyc
│   │   ├── admin.py
│   │   ├── admin.pyc
│   │   ├── models.py
│   │   ├── models.pyc
│   │   ├── tests.py
│   │   └── views.py
│   ├── settings.py
│   ├── settings.pyc
│   ├── sqlite3-database
│   ├── urls.py
│   └── urls.pyc
├── blossom-sqlite3-db2
├── requirements.txt
└── virtual_environment
    ├── bin
    │   ├── activate
    │   ├── activate.csh
    │   ├── activate.fish
    │   ├── activate_this.py
    │   ├── django-admin.py
    │   ├── easy_install
    │   ├── easy_install-2.7
    │   ├── gunicorn
    │   ├── gunicorn_django
    │   ├── gunicorn_paster
    │   ├── pip
    │   ├── pip-2.7
    │   ├── python
    │   └── python2.7 -> python
    ├── include
    │   └── python2.7 -> /System/Library/Frameworks/Python.framework/Versions/2.7/include/python2.7
    └── lib
        └── python2.7

I am trying to dump my data from the database with the command

django-admin.py dumpdata

My approach is to run cd ~/dev/django-project and then run source virtual_environment/bin/activate and then run django-admin.py dumpdata

However, I am getting the following error:

ImportError: Settings cannot be imported, because environment variable DJANGO_SETTINGS_MODULE is undefined.

I did some googling and found this page: https://docs.djangoproject.com/en/dev/topics/settings/#designating-the-settings

which tell me that

When you use Django, you have to tell it which settings you're using.
Do this by using an environment variable, DJANGO_SETTINGS_MODULE. The
value of DJANGO_SETTINGS_MODULE should be in Python path syntax, e.g.
mysite.settings. Note that the settings module should be on the Python
import search path.

Following a suggestion at Setting DJANGO_SETTINGS_MODULE under virtualenv? I appended the lines

export DJANGO_SETTINGS_MODULE="blossom.settings"
echo $DJANGO_SETTINGS_MODULE

to virtual_environment/bin/activate. Now, when I run the activate command in order to activate the virtual environment, I get output reading:

DJANGO_SETTINGS_MODULE set to blossom.settings

This looks good to me, but now the problem I have is that running

django-admin.py dumpdata

returns the following error:

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

What am I doing wrong? How can I check thesys.path? How is this supposed to work?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

遗失的美好 2025-01-02 04:03:44

除了初始项目创建之外,不要运行 django-admin.py 进行任何其他操作。对于此后的所有内容,请使用manage.py,它负责查找设置。

Don't run django-admin.py for anything other than the initial project creation. For everything after that, use manage.py, which takes care of the finding the settings.

冬天旳寂寞 2025-01-02 04:03:44

我刚刚遇到了同样的错误,并最终设法弄清楚发生了什么(最大的线索是 ImportError 中的 (Is it on sys.path?) )。

您需要将项目目录添加到 PYTHONPATH - 这就是文档的含义

请注意,设置模块应位于 Python 导入搜索路径上。

为此,请

$ export PYTHONPATH=$PYTHONPATH:$PWD

在运行 django-admin.py 之前从 ~/dev/django-project 目录运行。

您可以将此命令(将 $PWD 替换为项目的实际路径,ie ~/dev/django-project)添加到您的 virtualenv 中脚本。如果您选择在某个时候前进到 virtualenvwrapper (这是为这种情况设计的),您可以添加 < code>export PY... 行到自动生成的 postactivate 挂钩脚本。

mkdjangovirtualenv 可以进一步自动化此操作,为您添加适当的条目到 Python 路径中,但我自己还没有测试过。

I just encountered the same error, and eventually managed to work out what was going on (the big clue was (Is it on sys.path?) in the ImportError).

You need add your project directory to PYTHONPATH — this is what the documentation means by

Note that the settings module should be on the Python import search path.

To do so, run

$ export PYTHONPATH=$PYTHONPATH:$PWD

from the ~/dev/django-project directory before you run django-admin.py.

You can add this command (replacing $PWD with the actual path to your project, i.e. ~/dev/django-project) to your virtualenv's source script. If you choose to advance to virtualenvwrapper at some point (which is designed for this kind of situation), you can add the export PY... line to the auto-generated postactivate hook script.

mkdjangovirtualenv automates this even further, adding the appropriate entry to the Python path for you, but I have not tested it myself.

几味少女 2025-01-02 04:03:44

在类似 UNIX 的机器上,您可以像这样简单地为 virtualenv 加上别名,并使用别名而不是每次都键入:

.bashrc

alias Cool='source /path_to_ve/bin/activate;导出 DJANGO_SETTINGS_MODULE=django_settings_folder.settings; cd django_project 路径;导出 PYTHONPATH=$PYTHONPATH:$PWD'

On unix-like machine you can simply alias virtualenv like this and use alias instead of typing everytime:

.bashrc

alias cool='source /path_to_ve/bin/activate; export DJANGO_SETTINGS_MODULE=django_settings_folder.settings; cd path_to_django_project; export PYTHONPATH=$PYTHONPATH:$PWD'

羁〃客ぐ 2025-01-02 04:03:44

我最喜欢的替代方案是以 python 包语法将设置文件作为运行时参数传递给 manage.py,例如:

python manage.py runserver --settings folder.filename

更多信息 django 文档

My favourite alternative is passing settings file as runtime parameter to manage.py in a python package syntax, e.g:

python manage.py runserver --settings folder.filename

more info django docs

泪是无色的血 2025-01-02 04:03:44

我知道有很多答案,但这个答案对我有用,只是为了记录。

  1. 导航到所有虚拟环境所在的 .virtual_env 文件夹。
  2. 转到特定于您的项目的环境文件夹
  3. 附加 export DJANGO_SETTINGS_MODULE=.settings
    export DJANGO_SETTINGS_MODULE=.settings.local(如果您使用的是存储在设置文件夹中的单独设置文件)。

I know there are plenty answers, but this one worked for me just for the record.

  1. Navigate to your .virtual_env folder where all the virtual environments are.
  2. Go to the environment folder specific to your project
  3. Append export DJANGO_SETTINGS_MODULE=<django_project>.settings
    or export DJANGO_SETTINGS_MODULE=<django_project>.settings.local if you are using a separate settings file stored in a settings folder.
逆蝶 2025-01-02 04:03:44

处理此问题的另一种方法是使用 python dotenv 包,并将 PYTHONPATH 和 DJANGO_SETTINGS_MODULE 与其他环境变量一起包含在 .env 文件中。然后修改你的manage.py和wsgi.py以按照说明中的说明加载它们。

from dotenv import load_dotenv
load_dotenv()

Yet another way to do deal with this issue is to use the python dotenv package and include PYTHONPATH and DJANGO_SETTINGS_MODULE in the .env file along with your other environment variables. Then modify your manage.py and wsgi.py to load them as stated in the instructions.

from dotenv import load_dotenv
load_dotenv()
与之呼应 2025-01-02 04:03:44

我在 Windows 机器上工作时遇到类似的错误。我的问题是使用了错误的调试配置。使用 Python:django 作为调试配置选项。
首先确保您已在此处正确导出/设置 django_settings_module。

在此处输入图像描述

I had similar error while working on windows machine. My problem was using wrong debug configuration. Use Python:django as your debug config option.
First ensure you've exported/set django_settings_module correctly here.

enter image description here

却一份温柔 2025-01-02 04:03:44

我在更改 virtualenv 环境时遇到了这个问题(愚蠢的想法,顺便说一句)。我尝试设置 DJANGO_SETTINGS_MODULE 但没有成功。我的修复,而不是启动我的服务器:

django-admin runserver

我现在使用此命令运行我的服务器:

python manage.py runserver

不需要其他更改!

I ran into this issue when changing out my virtualenv environment (stupid idea, btw). I tried setting DJANGO_SETTINGS_MODULE and it didn't work. My fix, instead of starting my server with:

django-admin runserver

I now run my server with this command:

python manage.py runserver

No other changes needed!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文