从Python项目创建一个可执行(Create a single executable from a

2019-06-17 19:36发布

我想从我的Python项目创建一个单独的可执行文件。 用户应该能够下载并无需安装了Python运行它。 如果我只是分发包,我可以使用画中画,车轮和PyPI将建立和分发,但这需要用户具有Python和知道如何安装软件包。 我能用来建立一个独立的可执行文件从一个Python项目?

Answer 1:

下面是一些常见的。 除非明确指出,下面列出的所有项目正在积极维护作为我最后的编辑(八月2018)的。

我还包含如果你想检查自己对他们是如何频繁更新链接到他们各自的网站,回购和上市的PyPI。

此外,除非另有说明,以下列出的所有程序将具体产生EXE为它在运行的操作系统。因此,例如,在Windows中运行Pyinstaller会产生一个Windows的EXE,但在Linux上运行Pyinstaller会产生一个Linux的exe文件。 如果你想生产多种操作系统的exe文件,你将不得不考虑使用虚拟机或可考虑使用像酒 。


以下程序的所有工作类似 - 他们绑在一起Python和程序,有效地将它们组合起来,生成一个可执行。

  • PyInstaller:

    • 链接 : 网站 || 回购 || 的PyPI
    • 支持 :Python 2.7版和Python 3.4 -在Windows,Mac和Linux 3.7。
  • cx_Freeze:

    • 链接: 网站 || 回购 || 的PyPI
    • 支持:Python的2.7和3.5 -在Windows,Mac和Linux 3.7(?)。
  • pyapp:

    • 链接: 网站 || 回购 || 的PyPI
    • 支持:Python的2.7和3.3 -在仅适用于Mac 3.7(?)(?)。
    • 注:由于我上次编辑的,我一直无法最终确定由pyapp支持Python版本的确切范围。 最新版本支持提到的Python 3.7。

注:这篇文章的先前版本包括写起坐py2exe ( 回购 )和bbfreeze ( 回购 )。 这两个项目已经从这个列表中删除:他们已经看到了小到没有几年的活动,似乎完全没有维护。 见编辑历史旧writeups。


当然,这不是做事的唯一途径:

  • pynsist:

    • Links: Website || Repo || PyPi
    • Supports: All Python versions? Note -- will create Windows installers only.
    • Other notes: Pynsist seems to support bundling together any arbitrary Python version with your application. However, the tool itself requires Python 3.5+ to run.

      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.

  • Nuitka:

    • Links: Website || Repo (Github mirror) || PyPi
    • Supports: Python 2.6 - 2.7 and Python 3.3 - 3.7 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.
  • cython:

    • Links: Website || Repo || PyPi
    • Supports: Python 2.6 - 2.7 and Python 3.2 - 3.7 (?) 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.

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.

However, I've also successfully built various exes using cx_Freeze without too much difficulty, so you should also consider that program out.

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.

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 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.

As a final note, if you want to support Linux only, you may want to look into creating a distro package for your package, instead of turning it into an executable. Listing tools that can help you do this is beyond the scope of this post, but here's some reading material if you decide to explore this route.



Answer 2:

pyinstaller仍在积极发展。 你可以看到最新的变化在GitHub上。

它有三个主要的平台支持:

  • 窗口(32位和64位)
  • Linux操作系统(32位和64位)
  • 的Mac OS X(32位和64位)

它支持Python版本2.6和2.7。 它不支持Python 3中,但有一个实验性的Python 3支 。

更新

随着3.2.1版本,它支持Python 2.7,3.3-3.5



文章来源: Create a single executable from a Python project