我如何告诉 django-admin 使用哪个设置模块?
Posted
技术标签:
【中文标题】我如何告诉 django-admin 使用哪个设置模块?【英文标题】:How do I tell django-admin which settings module to use? 【发布时间】:2017-03-23 22:41:20 【问题描述】:当我使用--settings
开关运行django-admin
时,它失败了:
(env) $ django-admin help --settings=myapp.settings
Traceback (most recent call last):
File "/srv/http/myapp/env/bin/django-admin", line 11, in <module>
sys.exit(execute_from_command_line())
File "/srv/http/myapp/env/lib/python3.5/site-packages/django/core/management/__init__.py", line 367, in execute_from_command_line
utility.execute()
File "/srv/http/myapp/env/lib/python3.5/site-packages/django/core/management/__init__.py", line 316, in execute
settings.INSTALLED_APPS
File "/srv/http/myapp/env/lib/python3.5/site-packages/django/conf/__init__.py", line 53, in __getattr__
self._setup(name)
File "/srv/http/myapp/env/lib/python3.5/site-packages/django/conf/__init__.py", line 41, in _setup
self._wrapped = Settings(settings_module)
File "/srv/http/myapp/env/lib/python3.5/site-packages/django/conf/__init__.py", line 97, in __init__
mod = importlib.import_module(self.SETTINGS_MODULE)
File "/usr/lib/python3.5/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 986, in _gcd_import
File "<frozen importlib._bootstrap>", line 969, in _find_and_load
File "<frozen importlib._bootstrap>", line 944, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
File "<frozen importlib._bootstrap>", line 986, in _gcd_import
File "<frozen importlib._bootstrap>", line 969, in _find_and_load
File "<frozen importlib._bootstrap>", line 956, in _find_and_load_unlocked
ImportError: No module named 'myapp'
(env) $ ls myapp/settings.py
myapp/settings.py
但是manage.py
工作得很好。为了清楚起见,我想有额外的settings.py
用于开发环境。
另外,从这个脚本导入设置:
#!/usr/bin/env python
import myapp.settings
也可以。
【问题讨论】:
【参考方案1】:嗯,the doc 确实说:
manage.py
与django-admin
做同样的事情,但会为您处理一些事情:它将您的项目包放在
sys.path
。它设置
DJANGO_SETTINGS_MODULE
环境变量,使其指向您项目的settings.py
文件。
但它也说:
如果您需要在多个 Django 设置文件之间切换,请使用
django-admin
和DJANGO_SETTINGS_MODULE
或--settings
命令行选项。
所有的例子都是这样的:
$ django-admin migrate --settings=mysite.settings
问题是它可以工作your project dir must be in sys.path
。实现这一目标的一种方法是:
PYTHONPATH=/path/to/project django-admin help --settings=myapp.settings
或者你可以使用--pythonpath开关,或者提前设置PYTHONPATH
。
【讨论】:
以上是关于我如何告诉 django-admin 使用哪个设置模块?的主要内容,如果未能解决你的问题,请参考以下文章
如何告诉 Java 使用哪个 StringEscapeUtils.escapeXML()?