Python如何在多个项目之间共享包 [英] Python how to share package between multiple projects
问题描述
我有一个包,其中包含来自多个供应商的特定硬件的协议.这基本上是具有相似接口的多个不同协议的集合.它是由我移植到 python 的.但是现在我有两个项目使用不同的传输协议处理与该设备的通信,一个是通过 TCP 另一个是通过 RS-485/232.如何在这两个项目之间使用协议共享包,以便在添加新功能支持或错误修复后不需要复制它.
I have a package that contains protocols for specific hardware from multiple vendors. This is basically a collection of multiple different protocols that have similar interface. It is ported to python by me. But now I have two projects that handle communication with this equipment using different transport protocols one is over TCP another is over RS-485/232. How to share the package with protocols between this two projects so that I don't need to copy it after I add new functionality support or bug fixes.
推荐答案
你可以构建一个 python 包(更多信息在这里:https://python-packaging.readthedocs.io/en/latest/)
You can build a python package (more info here: https://python-packaging.readthedocs.io/en/latest/)
创建包含 setup.py 文件的包后,您可以使用以下命令将其安装在您的机器上:
Once you have created your package containing a setup.py file, you can install it on your machine using the:
pip install -e myPackage
pip install -e myPackage
命令.(其中myPacakge"是包含 setup.py 文件的文件夹).使用 -e 标志的优点是对您的包所做的更改直接包含在已安装的版本中.
command. (Where "myPacakge" is the folder containing the setup.py file). The advantage of using the -e flag is that changes made to your package are directly included in the installed version.
安装包后,您可以在多个项目中共享它,只需在 Python 中导入您的包,例如
Once your package is installed, you can share it across multiple projects, simply by importing your package in Python, e.g.
import myPackage as myPackge
这篇关于Python如何在多个项目之间共享包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!