Django、Python 模块和 Git 子模块

发布于 2024-10-19 11:53:22 字数 736 浏览 2 评论 0原文

我正在开发一个使用多个应用程序(python 模块)的 django 项目。大多数 python 模块都是由其他人在他们自己的 git 存储库中维护的。我使用 git-submodules 命令将它们导入到“apps”目录下的项目中,如下所示:

mysite/
mysite/apps
mysite/apps/django-extensions
mysite/apps/django-celery
mysite/apps/django-comments
mysite/apps/myapp
...etc

大多数子模块(例如 django-extensions)都有一个包含实际 python 模块的子文件夹: mysite/apps/django-extensions /django_extensions

这意味着我不能简单地将 python 路径设置为包含 mysite/apps——我必须将其设置为包含 mysite/apps/django-extensions,以便它可以导入 django_extensions 子文件夹。

打字很烦人:

PYTHONPATH=mysite/apps/django-extensions:mysite/apps/django-celery... python manage.py runserver

有没有更简单的方法来布置我的存储库?一个更简单的过程? 只是为了好玩,我尝试了 mysite/apps/* 的 PYTHONPATH,但这不起作用。

I am working on a django project that has uses multiple applications (python modules). Most of those python modules are maintained by other people in their own git repositories. I use the git-submodules command to import them into my project under the 'apps' directory like so:

mysite/
mysite/apps
mysite/apps/django-extensions
mysite/apps/django-celery
mysite/apps/django-comments
mysite/apps/myapp
...etc

Most of those submodules (take django-extensions for example) have a subfolder containing the actual python module: mysite/apps/django-extensions/django_extensions

This means I can't simply set my python path to include mysite/apps--I have to set it to include mysite/apps/django-extensions so it can import the django_extensions subfolder.

It gets annoying typing:

PYTHONPATH=mysite/apps/django-extensions:mysite/apps/django-celery... python manage.py runserver

Is there an easier way I should be laying out my repo? An easier process?
Just for fun, I tried a PYTHONPATH of mysite/apps/*, but that didn't work.

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

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

发布评论

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

评论(4

栀子花开つ 2024-10-26 11:53:22

这是错误的做法。不要在自己的项目文件中安装别人的第三方代码。相反,创建一个 virtualenv,并直接使用 pip 安装代码。

This is the wrong way to do it. Don't install other people's third-party code in your own project file. Instead, create a virtualenv, and install the code directly using pip.

此刻的回忆 2024-10-26 11:53:22

在互联网上一片空白后,我一起破解了这个解决方案。它很简单并且运行良好:

#At the top of settings.py
import sys, os
git_sub_modules = '/path/to/dir/containing/submodules' #Relative paths ok too
for dir in os.listdir(git_sub_modules):
    path = os.path.join(git_sub_modules, dir)
    if not path in sys.path:
        sys.path.append(path)

时间流逝

更新:使用 virtualenv 和/或 dokku 之类的东西来部署应用程序要容易得多。我不再用这个了。尽管检查需要“调整”的第三方应用程序并在项目中使用它们仍然很痛苦。

After coming up blank on the internet, I hacked this solution together. It's straight forward and works well enough:

#At the top of settings.py
import sys, os
git_sub_modules = '/path/to/dir/containing/submodules' #Relative paths ok too
for dir in os.listdir(git_sub_modules):
    path = os.path.join(git_sub_modules, dir)
    if not path in sys.path:
        sys.path.append(path)

time passes

UPDATE: It's much easier to use a virtualenv and/or something like dokku for deploying apps. I no longer use this. Although it is still a pain to checkout 3rd party apps that need 'tweaks' and use them in the project.

好久不见√ 2024-10-26 11:53:22

您可以将这些路径放入 dependency.pth 文件中,并且路径中仅包含 .pth。您的 site-packages / dist-packages 中有示例。

You could tuck those paths in a dependencies.pth file, and only have the .pth in your path. There are examples in your site-packages / dist-packages.

花开雨落又逢春i 2024-10-26 11:53:22

您可以尝试只检查存储库中想要的部分吗?因此,如果他们在您要检查的内容中​​包含实际代码,请不要检查额外的部分。

因此,不要获取 django-extensions,而是获取 django-extensions/django-extensions。

编辑:我相信就是你上面可以做的。

另外,我相信你可以在第一个 django-extensions 目录中添加一个 __init__.py ,但是你还必须在你的导入中添加一个额外的 django-extensions ( __init__.py 告诉 python 它是一个包)。虽然我认为这可能有效,但我建议拍摄我的第一个示例。

Could you try checking out just the wanted part of the repository? So if they have the actual code inside of what you're checking out, don't check out the extra part.

So instead of getting django-extensions get django-extensions/django-extensions.

Edit: I believe this is what you could do above.

Also, I believe you could get away with adding an __init__.py in the first django-extensions directory, but then you're going to have to add an extra django-extensions to your imports as well (__init__.py tells python it is a package). While I think this might work, I would recommend shooting for my first example.

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