在Python开发中使用共享模块的正确方法是什么? [英] What is the proper way to work with shared modules in Python development?

查看:85
本文介绍了在Python开发中使用共享模块的正确方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力将Python用作团队开发工具套件的一部分。使用我们使用的其他语言/工具,我们开发了许多可重用的函数和类,这些函数和类特定于我们所做的工作。这样可以使我们的工作方式标准化,并节省了大量的重复劳动。

I'm working toward adopting Python as part of my team's development tool suite. With the other languages/tools we use, we develop many reusable functions and classes that are specific to the work we do. This standardizes the way we do things and saves a lot of wheel re-inventing.

我似乎找不到任何通常用Python处理的示例。现在,我在本地驱动器上有一个development文件夹,下面有多个项目文件夹,另外还有一个 common文件夹,其中包含具有可重复使用的类和功能的程序包和模块。这些通用模块由多个项目中的模块导入。

I can't seem to find any examples of how this is usually handled with Python. Right now I have a development folder on a local drive, with multiple project folders below that, and an additional "common" folder containing packages and modules with re-usable classes and functions. These "common" modules are imported by modules within multiple projects.

Development/
    Common/
        Package_a/
        Package_b/
    Project1/
        Package1_1/
        Package1_2/
    Project2/
        Package2_1/
        Package2_2/

在尝试学习如何分发Python应用程序时,似乎假设所有引用的软件包都在顶层以下项目文件夹,而不是附带的。我也想到,也许正确的方法是在一个单独的项目中开发通用/框架模块,并在对其进行测试之后,通过安装到site-packages文件夹将其部署到每个开发人员的环境中。但是,这也会引起重新分配问题。

In trying to learn how to distribute a Python application, it seems that there is an assumption that all referenced packages are below the top-level project folder, not collateral to it. The thought also occurred to me that perhaps the correct approach is to develop common/framework modules in a separate project, and once tested, deploy those to each developer's environment by installing to the site-packages folder. However, that also raises questions re distribution.

有人可以阐明这一点,还是可以将我指向讨论该问题的资源?

Can anyone shed light on this, or point me to a resource that discusses this issue?

推荐答案

如果您要在多个项目中共享通用代码,则可能需要考虑将此代码存储在物理上独立的项目中,然后将其导入作为对其他项目的依赖。如果将通用代码项目托管在github或bitbucket中,则可以轻松实现此目的,您可以在其中使用pip将其安装在任何其他项目中。这种方法不仅可以帮助您轻松地在多个项目中共享通用代码,而且还可以保护您避免无意间创建不良的依赖关系(即,从您的通用代码指向非通用代码的依赖)。

If you have common code that you want to share across multiple projects, it may be worth thinking about storing this code in a physically separate project, which is then imported as a dependency into your other projects. This is easily achieved if you host your common code project in github or bitbucket, where you can use pip to install it in any other project. This approach not only helps you to easily share common code across multiple projects, but it also helps protect you from inadvertently creating bad dependencies (i.e. those directed from your common code to your non common code).

下面的链接很好地介绍了如何使用pip和virtualenv管理依赖项,如果您和您的团队对使用python相当陌生,那么绝对值得一读,因为这是用于此类的非常常见的工具链问题:

The link below provides a good introduction to using pip and virtualenv to manage dependencies, definitely worth a read if you and your team are fairly new to working with python as this is a very common toolchain used for just this kind of problem:

http://dabapps.com/blog/introduction-to-pip-and-virtualenv-python/

下面的链接向您展示了使用pip从github中获取依赖项:

And the link below shows you how to pull in dependencies from github using pip:

如何使用Python Pip安装软件,从Github中提取软件包?

这篇关于在Python开发中使用共享模块的正确方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆