简体   繁体   English

我应该用我的Python应用程序捆绑C库吗?

[英]Should I bundle C libraries with my Python application?

If I have a Python package that depends on some C libraries (like say the Gnu Scientific Library (GSL) for numerical computations), is it a good idea to bundle the library with my code? 如果我有一个依赖于某些C库的Python包(比如用于数值计算的Gnu Scientific Library(GSL)),那么将库与我的代码捆绑在一起是一个好主意吗?

I'd like to make my package as easy to install as possible for users and I don't want them to have to download C libraries by hand and supply include-paths. 我想让我的软件包尽可能易于安装,并且我不希望他们必须手动下载C库并提供包含路径。 Also I could always ensure that the version of the library that I ship is compatible with my code. 此外,我始终可以确保我发布的库版本与我的代码兼容。

However, is it possible that there are clashes if the user has the library installed already, or ar there any other reasons why I shouldn't do this? 但是,如果用户已经安装了库,是否可能存在冲突,或者是否有任何其他原因导致我不应该这样做?

I know that I can make it easier for users by just providing a binary distribution, but I'd like to avoid having to maintain binary distributions for all possible OSs. 我知道我可以通过提供二进制分发使用户更容易,但我想避免为所有可能的操作系统维护二进制分发。 So, I'd like to stick to a source distribution, but for the user (who proudly owns a C compiler) installation should be as easy as python setup.py install . 所以,我想坚持使用源代码,但对于用户(自豪地拥有C编译器),安装应该像python setup.py install一样简单。

Distribution is one of the hard parts for any software project. 分发是任何软件项目的难点之一。 Java and .NET lift part of this burden by defining a standard runtime and then just saying "just distribute everything else." Java和.NET通过定义标准运行时,然后只是说“只分发其他所有内容”来解除部分负担。 Of course there's a drawback: everything must be rewritten in a language supported by the runtime - as soon as you want to use native code, you lose all the advantages. 当然有一个缺点:必须用运行时支持的语言重写所有内容 - 只要您想使用本机代码,就会失去所有优势。

That's harder in Python, as it is in Ruby, C, C++ and other languages, as they usually leverage existing native libraries. 这在Python中更难,因为它在Ruby,C,C ++和其他语言中,因为它们通常利用现有的本机库。

Generally speaking: 一般来说:

  1. Make it possible to get a source sdist, via pypi.python.org as an example. 通过pypi.python.org作为示例,可以获得源sdist。 Correctly set your install_requires (probably you'll require python bindings for GSL, not GSL itself). 正确设置你的install_requires(可能你需要GSL的python绑定,而不是GSL本身)。 Use standard setuptools/distribute layout. 使用标准的setuptools / distribute布局。 This will let anyone - let's say a package maintainer for any distro - to pick up your software and package it. 这将让任何人 - 让我们说任何发行版的软件包维护者 - 拿起你的软件并打包它。

  2. Additionally, consider providing a full-blown installable package for your audience. 此外,请考虑为您的受众提供完整的可安装程序包。 You don't have to support all the distros and operating system; 您不必支持所有发行版和操作系统; pick one or two that you consider will be used most. 选择你认为最常用的一两个。 Tools like PyInstaller will let you create an installable, runnable package for many operating systems, but especially for linux you might want the user to install the distribution's own version of transitive deps (libgsl?) - you'll need a full-blown deb or rpm package to satisfy that - again, don't try supporting any and all the distro, you'll turn out mad. PyInstaller这样的工具可以让你为许多操作系统创建一个可安装的,可运行的包,但是对于linux你可能希望用户安装发行版自己的传递deps版本(libgsl?) - 你需要一个完整的deb或者rpm包以满足这一点 - 再次,不要尝试支持任何和所有的发行版,你会变得疯狂。 Support something you most use, and let other users to help you with other packaging needs. 支持您最常使用的东西,并让其他用户帮助您满足其他包装需求。

Also take a look at Python Packaging Guide 另请参阅Python Packaging Guide

You could have two separate branches of the src, one containing the libraries and another that doesn't. 你可以有两个独立的src分支,一个包含库,另一个不包含库。 That way you can explicitly warn your users in case they have installed the libraries. 这样,您可以明确地警告您的用户,以防他们安装了库。 Another solution could be (if the licences of the libraries allow you) is to wrap 'em up in a single file. 另一个解决方案可能是(如果库的许可证允许你)将它们包装在一个文件中。

I think there's no unique solution, but this are the ideas I could think so far. 我认为没有独特的解决方案,但这是我到目前为止所能想到的想法。

Good luck 祝好运

You can use virtualenv to create a private Python environment for your application. 您可以使用virtualenv为您的应用程序创建私有Python环境。 This avoids conflicts with other libraries. 这避免了与其他库的冲突。 It is best if you package modules and dependencies such as your libraries using Distribute . 最好使用Distribute打包模块和依赖项,例如库。 Distutils is something else that is worth researching. Distutils是值得研究的其他东西。

暂无
暂无

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

相关问题 我应该在哪里导入源文件中的Python库? - Where should I import Python libraries in my source file? 我应该如何为我的python应用程序构建一个简单的数据库包? - How should I build a simple database package for my python application? 我不能在 myD5 应用程序中使用 Pandas、Numpy、ZC2ED0329D2D3CF54C708317B20Z 和 PyMongoZ 的外部 Python 库 - I cant use external Python libraries of Pandas, Numpy, BeautifulSoup and PyMongo in my Mule ESB application 如果我想使用外部库从C#项目调用Python代码,我应该如何设置Visual Studio解决方案? - How should I set up a Visual Studio solution if I want to make a call from a C# project into Python code, with external libraries? 我应该使用哪些库来制作带有 python 的电报机器人? - Which libraries should i use to make a telegram bot with python? 将python脚本仅包含必要的库 - Bundle python script with only the necessary libraries 为什么在尝试使用 cx_freeze 冻结我的 python 应用程序时添加其他库时会出现错误? - Why do I get an error when adding additional libraries when trying to freeze my python application using cx_freeze? 在部署在 Kubernetes 集群上的容器化 Python 应用程序中,我应该指向哪个 IP 地址? - What IP Address should I point to in my containerized Python application that is deployed on a Kubernetes cluster? 我应该如何继续在Python中使用银行应用程序 - How should i proceed with banking Application in Python 我应该如何在Python的C ++ Builder / Delphi 2010应用程序中嵌入Python? - How should I embed Python in a C++ Builder / Delphi 2010 application?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM