Sys.path修改或更复杂的问题?

发布于 2024-09-16 05:22:54 字数 957 浏览 0 评论 0原文

我在正确导入 appengine 上的模块时遇到问题。我的应用程序通常使用 django 和 app-engine-patch,但这部分是仅使用 webapp 框架的任务队列。

我需要导入 django 设置才能使应用程序正常工作。

我的脚本开头为:

import os
import sys
sys.path.append('common/')
# 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' 

我总是收到此错误,或相关的内容:

<type 'exceptions.ImportError'>: No module named ragendja.settings_pre

因为 settings.py 文件开头为

from ragendja.settings_pre import *

我认为我需要再次将 ragendja 添加到 sys.path 但我进行了几次尝试,但没有成功。

这是我的目录:

project/
    app.yaml
    setting.py
    common/
        appenginepatch/
            ragendja/
                setting_pre.py
    myapp/
        script.py 

这只是 sys.path 问题吗?我需要如何使用正确的语法修改它?

谢谢

I have problems with importing correctly a module on appengine. My app generally uses django with app-engine-patch, but this part is task queues using only the webapp framework.

I need to import django settings for the app to work properly.

My script starts with:

import os
import sys
sys.path.append('common/')
# 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' 

I always get this error, or something related:

<type 'exceptions.ImportError'>: No module named ragendja.settings_pre

because the settings.py file starts with

from ragendja.settings_pre import *

I think I need to add ragendja to sys.path again but I had several tries that didn't work.

Here is my directory:

project/
    app.yaml
    setting.py
    common/
        appenginepatch/
            ragendja/
                setting_pre.py
    myapp/
        script.py 

Is it only a sys.path problem and how do I need to modify it with the correct syntax?

Thanks

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

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

发布评论

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

评论(2

四叶草在未来唯美盛开 2024-09-23 05:22:54

应用程序引擎补丁在内部操作 sys.path。后台任务会绕过该代码,因此您的路径将无法为 Django 调用做好准备。您有两个选择:

  • 手动修复路径。 应用引擎文档(请参阅“处理导入路径操作”小节") 建议将路径操作代码分解到可以由任务脚本导入的模块中。

  • 如果可能的话,消除对 django 代码的依赖。如果您可以将任务编写为纯 python 和/或 google api 调用,那么您就可以开始了。对于您的情况,这可能意味着重构您的设置代码。

App engine patch manipulates sys.path internally. Background tasks bypass that code, so your path will not be ready for Django calls. You have two choices:

  • Fix the paths manually. The app engine documentation (see the sub-section called "Handling import path manipulation") suggests factoring the path manipulation code into a module that can be imported by your task script.

  • Eliminate dependencies on django code, if possible. If you can write your task to be pure python and/or google api calls, you're good to go. In your case, this might mean refactoring your settings code.

生生不灭 2024-09-23 05:22:54

为什么不呢:

sys.path.append('common/appenginepatch')

因为 ragendja 就在这个目录中?

Why not:

sys.path.append('common/appenginepatch')

since the ragendja is in this directory?

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