What's the best practice for incorporating third-party libraries in a python program?

时光总嘲笑我的痴心妄想 提交于 2019-12-07 18:02:04

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/5862566/whats-the-best-practice-for-incorporating-third-party-libraries-in-a-python-pro

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!