Django manage.pysyncdb 在 CentOS 上找不到我的设置文件

发布于 2024-12-28 12:47:10 字数 3327 浏览 0 评论 0原文

我正在将 django 应用程序部署到 CentOS 5.5 服务器上,并使用 django-1.1.4python-2.6.5 进行部署。

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

我想运行syncdb;这就是我所做的(在 myproject 文件夹中使用 myapp):

$> 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 下运行,我在 Windows 上运行 python-2.6.6django-1.1.1

$> 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 版本。

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

O.

编辑: 添加了 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)

编辑:更多关于 myapp 中发生的情况package

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

I'm deploying my django application onto a CentOS 5.5 server, with django-1.1.4 over python-2.6.5.

I have multiple settings files inside the myapp/settings/ folder.

I would like to run the syncdb; here's what I do (with myapp inside myproject folder):

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

Django then issues an error like this :

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

In the myapp.wsgi file, os.path is appended with myproject path, and the os.environ['DJANGO_SETTINGS_MODULE'] is set also. Apache (through mod_wsgi) can start the app with no such error.

Finally, this works under Windows, where I run python-2.6.6 with django-1.1.1.

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

I know the versions are not the same, but I'm not sure that the minor differences may cause all my woe. Moreover I don't seem to find the exact same python version for Windows.

Any thoughts? Thanks a lot for reading.

O.

EDIT: added the manage.py content

#!/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)

EDIT : more on what happens in the myapppackage

I patch some django functions/classes from within the myapp.__init__ module. I was thinking the import django part in this module was causing a circular reference. The code is executed when I load myapp.settings.[any_config] and could have caused the crash. But then, how come the correct settings module is loaded with no error by WSGI, and that it works fine also on Windows? More : after commenting out the said code parts, the ImportError is still there.

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

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

发布评论

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

评论(1

夜巴黎 2025-01-04 12:47:10

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

查看 Manage.py 中导入设置模块的位置,然后根据您的情况导入 settings.dev_settings 。查找 2 行重要的行,

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 指定要使用的设置文件。

If you move your settings file, you need to modify manage.py to tell it where to find it. The default is for it to be in the same directory as manage.py, but if you move into another python module(folder with __init__.py) then you need to update the reference in manage.py.

Look in manage.py where it imports your settings module, and import settings.dev_settings in your case instead. Look for 2 important lines

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

Change these to reference where you moved the settings file to(assuming you moved to settings.dev_settings):

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

You can also use the command line option --settings to specify which settings file to use.

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