【问题标题】:Django manage.py syncdb can't find my settings file on CentOSDjango manage.py syncdb 在 CentOS 上找不到我的设置文件
【发布时间】:2012-01-23 16:57:42
【问题描述】:

我正在将我的 django 应用程序部署到 CentOS 5.5 服务器上,使用 django-1.1.4 而不是 python-2.6.5

myapp/settings/ 文件夹中有多个设置文件。

我想运行同步数据库;这就是我所做的(myappmyproject 文件夹中):

$> cd /var/www/apps/myproject
$> export PYTHONPATH=/var/www/apps/myproject
$> export DJANGO_SETTINGS_MODULE=myapp.settings.my_serverconfig
$> python26 myapp/manage.py syncdb

Django 然后发出这样的错误:

Error: Can't find the file 'settings.py' in the directory containing 'myapp/manage.py'. It appears you've customized things.
You'll have to run django-admin.py, passing it your settings module.
(If the file settings.py does indeed exist, it's causing an ImportError somehow.)

Traceback (most recent call last):
  File "emon/manage.py", line 17, in <module>
    execute_manager(settings)
  File "/usr/lib/python2.6/site-packages/django/core/management/__init__.py", line 360, in execute_manager
    setup_environ(settings_mod)
File "/usr/lib/python2.6/site-packages/django/core/management/__init__.py", line 343, in setup_environ
    project_module = import_module(project_name)
File "/usr/lib/python2.6/site-packages/django/utils/importlib.py", line 35, in import_module
    __import__(name)
ImportError: No module named my_serverconfig

myapp.wsgi 文件中,os.path 附加了myproject 路径,并且还设置了os.environ['DJANGO_SETTINGS_MODULE']。 Apache(通过 mod_wsgi)可以启动应用程序而不会出现此类错误。

最后,这可以在 Windows 下运行,我使用 django-1.1.1 运行 python-2.6.6

$> d:
$> cd d:\Code\myproject
$> export PYTHONPATH=d:\Code\myproject
$> export DJANGO_SETTINGS_MODULE=myapp.settings.dev_settings
$> python.exe myapp/manage.py syncdb

我知道版本不一样,但我不确定细微的差异是否会导致我的所有痛苦。此外,我似乎没有为 Windows 找到完全相同的 python 版本。

有什么想法吗?非常感谢阅读。

哦。

编辑: 添加了manage.py 内容

#!/usr/bin/env python
from django.core.management import execute_manager
import os


    if __name__ == "__main__":
    settings = None
    try:
        if os.environ.has_key('LOCAL_SERVER_SETTINGS'):
            os.environ['DJANGO_SETTINGS_MODULE'] = 'myapp.settings.%s' % os.environ['LOCAL_SERVER_SETTINGS']

        if os.environ.has_key('DJANGO_SETTINGS_MODULE'):
            settings = __import__(os.environ['DJANGO_SETTINGS_MODULE'])

        if settings is None:
            import settings
        execute_manager(settings)

    except ImportError:
        import sys
        sys.stderr.write("Error: Can't find the file 'settings.py' in the directory containing %r. It appears you've customized things.\nYou'll have to run django-admin.py, passing it your settings module.\n(If the file settings.py does indeed exist, it's causing an ImportError somehow.)\n" % __file__)

        import traceback
        traceback.print_exc()

        sys.exit(1)

编辑:更多关于myapppackage 中发生的事情

我从 myapp.__init__ 模块中修补了一些 django 函数/类。我在想这个模块中的import django 部分导致了循环引用。该代码在我加载 myapp.settings.[any_config] 时执行,并且可能导致崩溃。但是,为什么 WSGI 加载了正确的设置模块而没有错误,并且它在 Windows 上也能正常工作?更多:注释掉上述代码部分后, ImportError 仍然存在。

【问题讨论】:

  • 您是否将您的__init__.py 添加到设置文件夹中?
  • 你在运行什么shell?你的环境变量被保存了吗?检查env 命令或echo $DJANGO_SETTINGS_MODULE 的输出
  • 设置包中存在__init__模块,$DJANGO_SETTINGS_MODULE确实保存正确。我什至可以在 python 中打印os.environ['DJANGO_SETTINGS_MODULE']
  • 添加了一些关于我在 myapp.__init__ 模块中所做工作的详细信息。

标签: django centos manage.py


【解决方案1】:

如果你移动你的设置文件,你需要修改manage.py 告诉它在哪里可以找到它。默认情况下它与manage.py 在同一目录中,但是如果您移动到另一个python 模块(带有__init__.py 的文件夹),那么您需要更新manage.py 中的引用。

在 manage.py 中查看它导入您的设置模块的位置,并在您的情况下导入 settings.dev_settings。寻找两条重要的线

imp.find_module('settings') # Assumed to be in the same directory.
...
import settings

更改这些以引用您将设置文件移动到的位置(假设您移动到 ​​settings.dev_settings):

imp.find_module('settings.dev_settings') # Assumed to be in the same directory.
...
from settings import dev_settings as settings

您还可以使用命令行选项--settings 指定要使用的设置文件。

【讨论】:

  • 我忘了说有人修改了 manage.py 以导入 DJANGO_SETTINGS_MODULE 环境变量指定的模块,但作为 settings = __import__(os.environ['DJANGO_SETTINGS_MODULE'])。也许不是最好的方法?
  • 另外,我看到您正在使用来自应用根目录 (settings.module) 的相对导入。它可以与myapp.settings.module 一起使用吗?
  • 您可以在 Python 中以多种方式导入(django 也有一些不错的实用程序),所以我不关心导入是如何完成的,只是它针对的是正确的模块和那个模块存在。如果不知道对 manage.py 进行了哪些自定义,就很难看出问题所在。
  • 您是否尝试过 cd 进入 django 文件夹并执行 ./manage.py 而不是从更高级别执行?可能是您的路径设置不正确?
  • 另外,请确保您的 django 基本文件夹中有一个 __init__.py
猜你喜欢
  • 1970-01-01
  • 2015-07-31
  • 2011-02-13
  • 2014-09-07
  • 2014-10-13
  • 1970-01-01
  • 1970-01-01
  • 2015-04-11
  • 2010-11-25
相关资源
最近更新 更多