WSGI 下所需的项目*和*应用程序的 Django sys.path.append

发布于 2024-08-26 11:43:39 字数 642 浏览 6 评论 0原文

有人可以告诉我为什么我需要将项目根路径添加到 python 路径以及 WSGI 文件中的应用程序本身吗?

项目库称为“djapp”,应用程序称为“myapp”。

sys.path.append(os.path.dirname(os.path.abspath(__file__)) + '/..')
sys.path.append(os.path.dirname(os.path.abspath(__file__)) + '/../djapp')

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

如果我省略带有“/../djapp/”的行,日志会告诉我无法导入“myapp”,即使“djapp.settings”可以导入。 (验证“djapp”已导入)

它可以使用 ./manage.py 命令正常运行。项目文件夹中有一个__init__

为了进行测试,我使用 addsitedir 看到了同样的问题:

site.addsitedir('/home/user/web/project/')
site.addsitedir('/home/user/web/project/djapp')

Could somebody give me a pointer on why I need to add my project root path to the python path as well as the application itself in my WSGI file?

Project base is called 'djapp', the application is called 'myapp'.

sys.path.append(os.path.dirname(os.path.abspath(__file__)) + '/..')
sys.path.append(os.path.dirname(os.path.abspath(__file__)) + '/../djapp')

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

If I omit the line with "/../djapp/" the log tells my that 'myapp' can not be imported, even though 'djapp.settings' is. (validating 'djapp' was imported)

It al runs properly with the ./manage.py command. there's a __init__ in the project folder.

For testings sake, I see the same issue using addsitedir:

site.addsitedir('/home/user/web/project/')
site.addsitedir('/home/user/web/project/djapp')

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

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

发布评论

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

评论(2

少女情怀诗 2024-09-02 11:43:39

由于djapp(django 项目文件夹)位于也属于部署的父文件夹中,因此我将djapp 文件夹简单地重命名为project
那么这段代码总是正确:

sys.path.append(os.path.dirname(os.path.abspath(__file__)) + '/..' )
sys.path.append(os.path.dirname(os.path.abspath(__file__)) + '/../project')

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

完整的文件夹布局是:

host.example.com\
    etc\
    bin\
    project\
    logs\

你有什么。这样项目就可以始终被称为项目:)

希望有帮助。

格茨G

Since djapp (the django project folder) is in a parent folder that also belongs to the deployment I renamed the djapp folder simply to project.
Then this code is always correct:

sys.path.append(os.path.dirname(os.path.abspath(__file__)) + '/..' )
sys.path.append(os.path.dirname(os.path.abspath(__file__)) + '/../project')

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

The complete folder layout being:

host.example.com\
    etc\
    bin\
    project\
    logs\

And what have you. This way project can always be called project :)

Hope that helps.

GrtzG

酒解孤独 2024-09-02 11:43:39

想必您的项目中有执行 from myapp import foo 的代码。

两个选项:

  • 将其更改为 from djapp.myapp import foo,不建议这样做,因为它会妨碍可移植性;
  • 只需在 WSGI 中添加 djapp,并将 DJANGO_SETTINGS_MODULE 设置为 'settings'

Presumably you've got code within your project which is doing from myapp import foo.

Two options:

  • change that to from djapp.myapp import foo, which is not recommended as it prevents portability;
  • only add djapp in your WSGI, and set the DJANGO_SETTINGS_MODULE to just 'settings'.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文