使用 virtualenv 分发 python 代码?
我想将一些带有一些外部依赖项的Python代码分发到仅安装了核心Python的机器(以及不熟悉easy_install
等的用户)。
我想知道 virtualenv 是否可以用于此目的?我应该能够编写一些 bash 脚本来触发 virtualenv (使用合适的包),然后运行我的代码..但这看起来有点混乱,我想知道我是否在重新发明轮子?
是否有任何简单的解决方案可以分发带有依赖项的 python 代码,并且理想情况下不需要在客户端计算机上使用 sudo
?
I want to distribute some python code, with a few external dependencies, to machines with only core python installed (and users that unfamiliar with easy_install
etc.).
I was wondering if perhaps virtualenv can be used for this purpose? I should be able to write some bash scripts that trigger the virtualenv (with the suitable packages) and then run my code.. but this seems somewhat messy, and I'm wondering if I'm re-inventing the wheel?
Are there any simple solutions to distributing python code with dependencies, that ideally doesn't require sudo
on client machines?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
为此,您可以使用 PyInstaller 等工具。您的应用程序将在所有平台上显示为单个可执行文件,并包含依赖项。用户甚至不需要安装Python!
请参阅我的 logview 软件包的示例,该软件包依赖于 PyQt4 和 ZeroMQ,并包含 Linux 发行版、Mac OSX 和 Windows 均使用 PyInstaller 创建。
You can use a tool like PyInstaller for this purpose. Your application will appear as a single executable on all platforms, and include dependencies. The user doesn't even need Python installed!
See as an example my logview package, which has dependencies on PyQt4 and ZeroMQ and includes distributions for Linux, Mac OSX and Windows all created using PyInstaller.
构建 - http://pypi.python.org/pypi/zc.buildout
作为示例,看看我的干净项目: http://hg.jackleo.info /hyde-0.5.3-buildout-enviroment/src 它只有两个文件可以发挥作用,而且 Makefile 是可选的,但是你需要 bootstrap.py (Make 文件下载它,但它只运行在Linux)。 buildout.cfg 是主文件,您可以在其中编写依赖项和配置项目的制定方式。
要获取 bootstrap.py 只需从 http://svn 下载.zope.org/repos/main/zc.buildout/trunk/bootstrap/bootstrap.py
然后运行 python bootstap.py 和 bin/buildout。我不建议在本地安装构建,尽管这是可能的,只需使用一个引导程序下载即可。
我必须承认,扩建并不是最简单的解决方案,但它确实很强大。所以学习是值得花时间的。
更新2014-05-30
由于它最近被投票并用作答案(可能),我想通知一些更改。
首先 - buildout 现已从 github https://raw.githubusercontent 下载。 com/buildout/buildout/master/bootstrap/bootstrap.py
由于 buildout 2 的重大更改,hyde 项目可能会失败。
在这里您可以找到更好的示例 http://www.buildout.org/en/latest /docs/index.html 我还想建议查看“与 Buildout 相关的链接集合”部分,它可能包含您的项目的信息。
其次我个人更赞成可以使用python安装的setup.py脚本。有关 Egg 结构的更多信息,请访问 http://peak.telecommunity.com/DevCenter/PythonEggs 如果这看起来太可怕 - 查找谷歌(查询
python Egg
)。在我看来,它实际上比构建更简单(肯定更容易调试),而且它可能更有用,因为它可以更轻松地分发并在 virtualenv 的帮助下安装在任何地方,或者在全局范围内使用构建,您必须提供所有始终使用源代码构建脚本。Buildout - http://pypi.python.org/pypi/zc.buildout
As sample look at my clean project: http://hg.jackleo.info/hyde-0.5.3-buildout-enviroment/src its only 2 files that do the magic, more over Makefile is optional but then you'll need bootstrap.py (Make file downloads it, but it runs only on Linux). buildout.cfg is the main file where you write dependency's and configuration how project is laid down.
To get bootstrap.py just download from http://svn.zope.org/repos/main/zc.buildout/trunk/bootstrap/bootstrap.py
Then run
python bootstap.py
andbin/buildout
. I do not recommend to install buildout locally although it is possible, just use the one bootstrap downloads.I must admit that buildout is not the easiest solution but its really powerful. So learning is worth time.
UPDATE 2014-05-30
Since It was recently up-voted and used as an answer (probably), I wan to notify of few changes.
First of - buildout is now downloaded from github https://raw.githubusercontent.com/buildout/buildout/master/bootstrap/bootstrap.py
That hyde project would probably fail due to buildout 2 breaking changes.
Here you can find better samples http://www.buildout.org/en/latest/docs/index.html also I want to suggest to look at "collection of links related to Buildout" part, it might contain info for your project.
Secondly I am personally more in favor of setup.py script that can be installed using python. More about the egg structure can be found here http://peak.telecommunity.com/DevCenter/PythonEggs and if that looks too scary - look up google (query for
python egg
). It's actually more simple in my opinion than buildout (definitely easier to debug) as well as it is probably more useful since it can be distributed more easily and installed anywhere with a help of virtualenv or globally where with buildout you have to provide all of the building scripts with the source all of the time.如果您有这样的要求,您不想分发您的 virtualenv。但是您可以使用 pip 创建一个需求文件(通常称为requirements.txt),并告诉您的用户创建一个 virtualenv,然后运行 pip install -r requests。 txt,这将为它们安装所有依赖项。
请参阅 pip 文档,了解要求文件格式< /a> 和 Pinax 项目,了解在这方面做得很好的项目示例。
You don't want to distribute your virtualenv, if that's what you're asking. But you can use
pip
to create a requirements file - typically calledrequirements.txt
- and tell your users to create a virtualenv then runpip install -r requirements.txt
, which will install all the dependencies for them.See the pip docs for a description of the requirements file format, and the Pinax project for an example of a project that does this very well.