在 python 程序中合并第三方库的最佳实践是什么?
下午好。
我正在为我的工作编写一个中小型Python程序。该任务要求我使用 Excel 库 xlwt 和 xlrd,以及用于查询 Oracle 数据库的库,称为 cx_Oracle。我正在通过版本控制系统(即CVS)开发该项目。
我想知道围绕 python 项目组织第三方库的标准方法是什么。库 xlwt、xlrd 和 cx_Oracle 是否应该存储在 /usr/local/lib/python 等目录中,该目录可能在 PYTHONPATH 中占有一席之地?或者应该将第三方库包含在与项目源代码相同的目录中,与项目一起有效地“发货”,这样 python 脚本就更加独立于平台。
我只是在寻找最佳实践,因为我来自 Java,并且是 Python 的新手。
预先感谢,
克特姆
Good afternoon.
I am writing a small to medium-sized python program for my job. The task requires me to use the Excel libraries xlwt and xlrd, as well as a library for querying Oracle databases, called cx_Oracle. I am developing the project through a version control system, namely CVS.
I was wondering what is the standard way to organize third-party libraries around a python project. Should the libraries xlwt, xlrd, and cx_Oracle be stored in a directory such as /usr/local/lib/python, which presumably has its place in the PYTHONPATH? Or should the third-party libraries instead be included in the same directory as the source code of the project, effectively "shipped out" with the project, that way the python script is more platform-independent.
I am just looking for best practices since I am coming from Java and am new to Python.
Thanks in advance,
ktm
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
基本上您有两个选择(就像您在 Java 中可能认识到的那样)。
一种是将外部依赖项打包到应用程序中,这意味着您将所有依赖项复制到应用程序目录结构中。通常不建议这样做,除非您必须能够发布单个安装或二进制文件。在这种情况下,您必须能够处理所有支持的平台。
处理依赖关系的更好方法是记录应用程序的依赖关系。一种方法是定义 pip 格式的 requests.txt 文件,然后由
pip install -rrequirements.txt
运行该文件,该文件将继续安装所有依赖项。 Buildout 是您可以选择的另一个选项。You basically have two options (just like you might recognize from Java).
One is to packaged the external dependencies in your app, meaning you copy all the dependencies into your apps directory structure. This is usually not recommended unless you must be able to ship a single installation or binary. In this case, you must be able to handle all supported platforms.
The better way to handle dependencies is to document what your apps dependencies are. One way to do this is to define a requirements.txt file in the pip format which can then be run by
pip install -r requirements.txt
, which will proceed to install all dependencies. Buildout is another option you can go with.