为什么Fabric fabfile不能看到Django设置?

时间:2016-02-05 22:37:15

标签: django fabric

我有一个最小的Django项目,其中包含一个简单地尝试显示Django设置的fabfile:

# fabfile.py
from fabric.api import *
from fabric.contrib import django

django.settings_module('conf.settings')
from django.conf import settings

def set():
    print settings.DEBUG

当我在我的本地主机上运行激活我的虚拟环境'venv'时,转到项目目录'/ www / django / testfab /'并运行命令'fab set',我收到此错误:

Traceback (most recent call last):
  File "/Users/smith/venv/django18/lib/python2.7/site-packages/fabric/main.py", line 743, in main
    *args, **kwargs
  File "/Users/smith/venv/django18/lib/python2.7/site-packages/fabric/tasks.py", line 427, in execute
    results['<local-only>'] = task.run(*args, **new_kwargs)
  File "/Users/smith/venv/django18/lib/python2.7/site-packages/fabric/tasks.py", line 174, in run
    return self.wrapped(*args, **kwargs)
  File "/www/django/testfab/fabfile.py", line 8, in set
    print settings.DEBUG
  File "/Users/smith/venv/django18/lib/python2.7/site-packages/django/conf/__init__.py", line 48, in __getattr__
    self._setup(name)
  File "/Users/smith/venv/django18/lib/python2.7/site-packages/django/conf/__init__.py", line 44, in _setup
    self._wrapped = Settings(settings_module)
  File "/Users/smith/venv/django18/lib/python2.7/site-packages/django/conf/__init__.py", line 92, in __init__
    mod = importlib.import_module(self.SETTINGS_MODULE)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
ImportError: No module named conf.settings

我的settings.py文件位于我的/ www / django / testfab / conf目录中:

.
├── conf
│   ├── __init__.py
│   ├── __init__.pyc
│   ├── settings.py
│   ├── settings.pyc
│   ├── urls.py
│   ├── urls.pyc
│   ├── wsgi.py
│   └── wsgi.pyc
├── fabfile.py
├── fabfile.pyc
└── manage.py

我的DJANGO_SETTINGS_MODULE环境变量设置为'conf.settings'。

为什么Fabric没有看到我的settings.py文件?我正在遵循Fabric文档中显示的Django Integration说明。

谢谢!

正如Sean Hayes在下面指出的,这是一个结构issue。以下是解决问题的方法:

django.settings_module('conf.settings')
from django.conf import settings
_ = settings.INSTALLED_APPS

1 个答案:

答案 0 :(得分:0)

您从哪个目录运行fab?除非conf位于Python路径上,否则/www/django/testfab/不会被识别为Python模块。从该目录运行fabpython./manage.py会在该命令的执行过程中将其添加到Python路径中。

conf.settings也有可能提升自己ImportError