简体   繁体   English

多个 Django 项目之间的共享库?

[英]Shared library between multiple Django projects?

If I have 2 Django apps, how could I share a common library between both?如果我有 2 个 Django 应用程序,我如何在两者之间共享一个公共库? I was thinking about a third library project, that I shortcut to in the other 2 projects locally, & on production replace the shortcut with the whole Library folder.我正在考虑第三个库项目,我在本地的其他 2 个项目中使用快捷方式,并在生产中将快捷方式替换为整个库文件夹。 would something like this work?像这样的东西会起作用吗? and if so, how would I switch out the shortcut for the actual folder on release?如果是这样,我将如何在发布时切换实际文件夹的快捷方式?

You can use third library as your utility folder and refer code from that.您可以使用第三个库作为您的实用程序文件夹并从中引用代码。
Generally, in in project with multiple apps, we create app directory which will be used by other 2 apps a utility.通常,在具有多个应用程序的项目中,我们创建应用程序目录,其他 2 个应用程序将使用该目录作为实用程序。

├── app_a
├── app_b
└── common_app
    └── common_utility.py

# and can be imported in app_a and app_b as 
from common_app.common_utility import CommonUtilty

I reckon you want to share code among apps on different servers.我认为您想在不同服务器上的应用程序之间共享代码。

There are 2 methods you can use to achieve that:您可以使用 2 种方法来实现这一目标:

  1. Publish it to PyPI (Public)将其发布到 PyPI(公共)
  2. Use it from your Github (Private)从您的 Github 使用它(私人)

Publishing to PyPI:发布到 PyPI:

Read and follow this article 阅读并关注这篇文章

Use it from your github:从您的 github 中使用它:

Follow the above( Publishing to PyPI ) article till the publishing part.按照上面的( 发布到 PyPI )文章直到发布部分。 After that follow this article .之后按照这篇文章

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

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