2016-11-10 76 views
0

當我--settings開關運行django-admin,它失敗:如何告訴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 

工作過。

回答

0

好,the doc確實說:

manage.py做同樣的事情django-admin但需要照顧的幾件事情你:

  • 把你的項目的包裝上sys.path

  • 它設置DJANGO_SETTINGS_MODULE環境變量,以便它指向您的項目的settings.py文件。

但它也說:

如果需要多個Django之間開關的設置文件,請使用django-adminDJANGO_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

相關問題