在 app-engine-patch 中导入 django 设置

发布于 2024-09-16 01:03:47 字数 1138 浏览 5 评论 0原文

我的 Django 设置有问题。 我的应用程序使用 app-engine-patch 运行。 我添加了一个无需 django 即可运行的脚本,并且可以通过 app.yaml 处理程序直接访问。 然后我得到这个错误:

File "/base/python_runtime/python_lib/versions/third_party/django-0.96/django/conf/__init__.py", line 53, in _import_settings
raise EnvironmentError, "Environment variable %s is undefined." % ENVIRONMENT_VARIABLE
<type 'exceptions.EnvironmentError'>: Environment variable DJANGO_SETTINGS_MODULE is undefined.

我在谷歌中找到了这个提示:

# Force Django to reload its settings.
from django.conf import settings
settings._target = None

# Must set this env var before importing any part of Django
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings' 

但是后来我得到了这个错误:

raise EnvironmentError, "Could not import settings '%s' (Is it on sys.path? Does it have syntax errors?): %s" % (self.SETTINGS_MODULE, e)
<type 'exceptions.EnvironmentError'>: Could not import settings 'settings.py' (Is it on sys.path? Does it have syntax errors?): No module named ragendja.settings_pre

我认为这是app-engine-patch路径修改的问题,如何正确导入settings_pre?

谢谢!

I have a problem with Django settings.
My app runs with app-engine-patch.
I added a script that runs without django, and is reached directly via the app.yaml handlers.
I then get this error:

File "/base/python_runtime/python_lib/versions/third_party/django-0.96/django/conf/__init__.py", line 53, in _import_settings
raise EnvironmentError, "Environment variable %s is undefined." % ENVIRONMENT_VARIABLE
<type 'exceptions.EnvironmentError'>: Environment variable DJANGO_SETTINGS_MODULE is undefined.

I found this tip in google:

# Force Django to reload its settings.
from django.conf import settings
settings._target = None

# Must set this env var before importing any part of Django
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings' 

But then I got this error:

raise EnvironmentError, "Could not import settings '%s' (Is it on sys.path? Does it have syntax errors?): %s" % (self.SETTINGS_MODULE, e)
<type 'exceptions.EnvironmentError'>: Could not import settings 'settings.py' (Is it on sys.path? Does it have syntax errors?): No module named ragendja.settings_pre

I think it is a problem with app-engine-patch paths modification, how can I import settings_pre correctly?

Thanks!

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

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

发布评论

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

评论(2

看透却不说透 2024-09-23 01:03:47

更改

os.environ['DJANGO_SETTINGS_MODULE'] = 'settings.py' 

os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'

DJANGO_SETTINGS_MODULE 的值是模块的名称(即,正如您在 Python 脚本的 import 语句中编写的那样),而不是模块的路径模块。

Change

os.environ['DJANGO_SETTINGS_MODULE'] = 'settings.py' 

to

os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'

The value of the DJANGO_SETTINGS_MODULE is the name of the module (ie, as you would write it in an import statement in a Python script), not the path to the module.

七色彩虹 2024-09-23 01:03:47

感谢另一个问题,我将 beinning 替换为:

from common.appenginepatch.aecmd import setup_env 
setup_env(manage_py_env=True)

This imports all the settings and my task can run withoutreference to Django

Thanks to another question, I replaced the beinning with:

from common.appenginepatch.aecmd import setup_env 
setup_env(manage_py_env=True)

This imports all the settings and my task can run without reference to Django

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