简体   繁体   English

Django,Python模块和Git子模块

[英]Django, Python Modules, and Git Submodules

I am working on a django project that has uses multiple applications (python modules). 我正在研究一个使用多个应用程序(python模块)的django项目。 Most of those python modules are maintained by other people in their own git repositories. 大多数python模块都由其他人在自己的git存储库中维护。 I use the git-submodules command to import them into my project under the 'apps' directory like so: 我使用git-submodules命令将它们导入到'apps'目录下的项目中,如下所示:

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 大多数这些子模块(例如django-extensions)都有一个包含实际python模块的子文件夹: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. 这意味着我不能简单地将我的python路径设置为包含mysite / apps - 我必须将其设置为包含mysite / apps / django-extensions,以便它可以导入django_extensions子文件夹。

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. 只是为了好玩,我尝试了一个mysite / apps / *的PYTHONPATH,但这不起作用。

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 . 相反,创建一个virtualenv,并使用pip直接安装代码。

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. 更新:使用virtualenv和/或类似dokku的东西来部署应用程序要容易得多。 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. 虽然查看需要“调整”并在项目中使用它们的第三方应用程序仍然很痛苦。

You could tuck those paths in a dependencies.pth file, and only have the .pth in your path. 您可以将这些路径放在dependencies.pth文件中,并且只在路径中包含.pth There are examples in your site-packages / dist-packages. 您的site-packages / dist-packages中有一些示例。

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. 因此,不要获取django-extensions获取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). 另外,我相信你可以在第一个django-extensions目录中添加一个__init__.py,但是你还需要为你的导入添加额外的django-extensions(__ init__.py告诉python它是一袋)。 While I think this might work, I would recommend shooting for my first example. 虽然我认为这可行,但我建议拍摄我的第一个例子。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM