问题描述
我想从我的Python项目创建一个单一的可执行文件。用户应该能够下载并运行它,而不需要安装Python。如果我只是分发一个包,我可以使用pip,wheel和PyPI来构建和分发它,但这需要用户有Python并知道如何安装软件包。我可以使用什么来从Python项目构建自包含的可执行文件?
I want to create a single executable from my Python project. A user should be able to download and run it without needing Python installed. If I were just distributing a package, I could use pip, wheel, and PyPI to build and distribute it, but this requires that the user has Python and knows how to install packages. What can I use to build a self-contained executable from a Python project?
PS(28 nov 2016):
目前我使用PyInstaller使用python 3.5,将一个瓶子应用程序编译为单个可执行文件。
PS(28 nov 2016):At the moment I'm using PyInstaller with python 3.5, to compile a bottle app to a single executable. Tiny bit of searching, and it works.
推荐答案
以下是一些常见的问题。除非明确指出,下面列出的所有项目都在我上次编辑(2016年12月)时积极维护。
Here are some common ones. Unless explicitly noted, all projects listed below are being actively maintained as of my last edit (December 2016).
我还包括了他们各自回购的链接,以便自己检查他们更新的频率。
I've also included links to their respective repos, in case you want to check for yourself on how frequently they've been updated.
此外,除非另有说明,下面列出的所有程序都会生成一个特定于其运行的操作系统的exe。因此,例如,在Windows中运行Pyinstaller将生成一个Windows exe,但在Linux中运行Pyinstaller将生成一个Linux exe。如果您想为多个操作系统生成一个exe,您必须使用虚拟机或使用类似。
Also, unless otherwise noted, all programs listed below will produce an exe specifically for the operating system it's running in. So for example, running Pyinstaller in Windows will produce a Windows exe, but running Pyinstaller in Linux will produce a Linux exe. If you want to produce an exe for multiple operating systems, you will have to look into using virtual machines or look into using something like Wine.
以下程序都工作类似 - 它们将Python和您的程序捆绑在一起,有效地结合
The following programs all work similarly -- they bundle together Python and your program, effectively combining them to produce an executable.
- cx_Freeze 5.0 was released in November 2016.
- 回购:
- 支持:仅限Windows。 支持Python 2.4+,并且
- 回购:
- 支持:仅限Mac上的Python 2.7 + / 3.3 +。
- Repo: https://bitbucket.org/ronaldoussoren/py2app
- Supports: Python 2.7+/3.3+ on Mac only.
- 回购:
- 支持: Python 2.4 - 2.7,但不支持Windows和Linux上的Python 3.
- 其他注意事项:目前没有有效的维护者。
- Repo: https://github.com/schmir/bbfreeze
- Supports: Python 2.4 - 2.7 but not Python 3 on Windows and Linux.
- Other notes: Currently does not have an active maintainer.
您可以在。
当然,这不是唯一的做法
Of course, that's not the only way of doing things:
- 回购:
- 支持: Python 2.7或Python 3.3+。
- 其他注意事项:Pynsist会为您的程式建立Windows安装程式,用户的计算机,而不是捆绑它与您的代码,并创建链接到您的Python脚本的快捷方式。虽然此程序仅生成Windows安装程序,但您似乎可以。
- Repo: https://github.com/takluyver/pynsist
- Supports: Python 2.7 or Python 3.3+. Will create Windows installers only.
- Other notes: Pynsist will create a Windows installer for your program which will directly install Python on the user's computer instead of bundling it with your code and create shortcuts that link to your Python script. Although this program produces only Windows installers, it appears that you can still run Pynsist on Mac and Linux computers.
- Repo(Github mirror) >
- 支持:在Windows,Mac和Linux上的Python 2.6 - 2.7和Python 3.2+。
- 编译你的Python代码并生成一个exe(而不是其他项目,只包括Python)来尝试和加快你的代码。作为副作用,你也会得到一个方便的exe,你可以分发。请注意,您需要在系统上提供。 li>
- Repo (Github mirror): https://github.com/kayhayen/Nuitka
- Supports: Python 2.6 - 2.7 and Python 3.2+ on Windows, Mac, and Linux.
- Other notes: Nuitka will literally compile your Python code and produce an exe (as opposed to the other projects, which simply include Python) to try and speed up your code. As a side effect, you'll also get a handy exe you can distribute. Note that you need to have a C++ compiler available on your system.
- 回购:
- 支持:Python 2.6+和Python 3.2+ Mac和Linux。
- 其他注意事项:Cython与Nuitka类似,它是一个Python编译器。但是,您不必直接编译代码,而是将其编译为C.然后,您可以使用该C代码,并。您需要在您的系统上提供C编译器。
- Repo: https://github.com/cython/cython
- Supports: Python 2.6+ and Python 3.2+ on Windows, Mac, and Linux.
- Other notes: Cython is similar to Nuitka in that it is a Python compiler. However, instead of directly compiling your code, it'll compile it to C. You can then take that C code and turn your code into an exe. You'll need to have a C compiler available on your system.
我个人的偏好是使用PyInstaller,因为它是我最容易启动和运行,被设计为与各种流行的图书馆如numpy或pygame很好地工作,并与很大的兼容性各种操作系统和Python版本。
My personal preference is to use PyInstaller since it was the easiest for me to get up and running, was designed to work nicely with various popular libraries such as numpy or pygame, and has great compatibility with various OSes and Python versions.
但是,我也使用cx_Freeze和py2exe成功构建了各种exes,没有太多的困难,所以你也应该考虑检查这些。
However, I've also successfully built various exes using cx_Freeze and py2exe without too much difficulty, so you should also definitely consider checking those out.
我还没有机会广泛地尝试pynist,Nuitka或Cython,但它们似乎是非常有趣和创新的解决方案。如果你使用第一组程序遇到麻烦,可能值得尝试这三个程序之一。因为他们的工作原理与Pyinstaller / cx_freeze风格程序不同,所以他们可能会在第一组失败的情况下取得成功。
I haven't yet had a chance to to try pynist, Nuitka, or Cython extensively, but they seem like pretty interesting and innovative solutions. If you run into trouble using the first group of programs, it might be worthwhile to try one of these three. Since they work fundamentally differently then the Pyinstaller/cx_freeze-style programs, they might succeed in those odd edge cases where the first group fails.
特别是,我认为pynist是一个很好的方法来解决整个分发你的代码的问题:Mac和Linux已经有Python的本地支持,只是在Windows上安装Python可能真的是最干净的解决方案。 (缺点是现在你需要担心定位多个版本的Python +安装库)。
In particular, I think pynist is a good way of sidestepping the entire issue of distributing your code altogether: Macs and Linux already have native support for Python, and just installing Python on Windows might genuinely be the cleanest solution. (The downside is now that you need to worry about targeting multiple versions of Python + installing libraries).
Nuitka和Cython(在我有限的经验) 。再次,我没有自己测试它们广泛,所以我的主要观察是,他们似乎需要更长的时间来生产一个冻结风格的程序做的exe。
Nuitka and Cython (in my limited experience) seem to work fairly well. Again, I haven't tested them extensively myself, and so my main observation is that they seem to take much longer to produce an exe then the "freeze" style programs do.
这篇关于从Python项目创建单个可执行文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!