从 Python 项目创建单个可执行文件 [英] Create a single executable from a Python project

查看:42
本文介绍了从 Python 项目创建单个可执行文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从我的 Python 项目创建一个可执行文件.用户应该能够在不需要安装 Python 的情况下下载和运行它.如果我只是分发一个包,我可以使用 pip、wheel 和 PyPI 来构建和分发它,但这需要用户有 Python 并且知道如何安装包.我可以使用什么来从 Python 项目构建自包含的可执行文件?

解决方案

以下是一些常见的.我只包含了截至我上次编辑(2021 年 7 月)时仍在积极维护的项目.

除非另有说明,否则下面列出的所有程序都会为它运行的操作系统生成一个 exe.例如,在 Windows 中运行 Pyinstaller 将生成一个 Windows exe,但在 Linux 中运行 Pyinstaller 将生成一个 Linux exe.如果你想为多个操作系统生成一个 exe,你将不得不考虑使用虚拟机或类似 Wine.


以下程序的工作方式都相似——它们将 Python 和您的程序捆绑在一起,有效地组合它们以生成可执行文件.

  • PyInstaller:

    网站 ||回购 ||PyPi

    在 Windows、Mac 和 Linux 上支持 Python 3.5 - 3.9.

  • cx_Freeze:

    网站 ||回购 ||PyPi

    在 Windows、Mac 和 Linux 上支持 Python 3.6 - 3.9.

  • py2exe:

    网站 ||回购 ||PyPi

    仅在 Windows 上支持 Python 3.6 - 3.9.

  • py2app:

    网站 ||回购 ||PyPi

    仅在 Mac 上支持 Python 2.7 (?) 和 Python 3 (?).支持的 Python 版本的确切范围目前没有记录.


当然,这不是唯一的做事方式:

  • pynsist:

    网站 ||Repo ||PyPi

    Pynsist 将为您的程序创建一个 Windows 安装程序,它将直接在用户的计算机上安装 Python,而不是将它与您的代码捆绑在一起,并创建链接到您的 Python 脚本的快捷方式.

    pynsist 工具本身需要 Python 3.5+ 才能运行,但支持将任何版本的 Python 与您的程序捆绑在一起.

    Pynsist 将仅创建 Windows 安装程序,但可以在 Windows、Mac 和 Linux 上运行.请参阅他们的常见问题解答了解更多详情.>

  • 努伊特卡:

    网站 ||Repo(Github 镜像) ||PyPi

    Nuitka 将逐字编译您的 Python 代码并生成一个 exe(与其他项目相反,它只包含 Python)来尝试加速您的代码.作为副作用,您还将获得一个可以分发的方便的 exe.请注意,您的系统上需要有 C++ 编译器.

    在 Windows、Mac 和 Linux 上支持 Python 2.6 - 2.7 和 Python 3.3 - 3.9.

  • cython:

    网站 ||回购 ||PyPi

    Cython 与 Nuitka 的相似之处在于它是一个 Python 编译器.但是,它不会直接编译您的代码,而是将其编译为 C.然后您可以使用该 C 代码并将您的代码转换为执行.您需要在您的系统上安装一个 C 编译器.

    在 Windows、Mac 和 Linux 上支持 Python 2.6 - 2.7 和 Python 3.3 - 3.9.


我个人更喜欢使用 PyInstaller,因为它对我来说是最容易启动和运行的,旨在与各种流行的库(如 numpy 或 pygame)完美配合,并且与各种操作系统和 Python 版本具有很好的兼容性.

但是,我也使用 cx_Freeze 成功构建了各种 exe,没有太多困难,因此您也应该考虑尝试使用该程序.

我还没有机会广泛尝试 pynist、Nuitka 或 Cython,但它们似乎是非常有趣和创新的解决方案.如果您在使用第一组程序时遇到问题,那么尝试这三个程序中的一个可能是值得的.由于它们的工作方式与 Pyinstaller/cx_freeze 风格的程序根本不同,因此它们可能会在第一组失败的奇怪边缘情况下取得成功.

特别是,我认为 pynist 是避免完全分发代码的整个问题的好方法:Mac 和 Linux 已经具有对 Python 的本机支持,并且仅在 Windows 上安装 Python 可能真的是最干净的解决方案.(缺点是现在您需要担心针对多个版本的 Python + 安装库).

Nuitka 和 Cython(以我有限的经验)似乎工作得相当好.同样,我自己还没有对它们进行过广泛的测试,所以我的主要观察是它们似乎需要更长的时间来生成 exe,然后冻结"生成 exe 文件.风格程序可以.


综上所述,将 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?

解决方案

Here are some common ones. I've included only projects that are being actively maintained as of my last edit (July 2021).

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 something like Wine.


The following programs all work similarly -- they bundle together Python and your program, effectively combining them to produce an executable.

  • PyInstaller:

    Website || Repo || PyPi

    Supports Python 3.5 - 3.9 on Windows, Mac, and Linux.

  • cx_Freeze:

    Website || Repo || PyPi

    Supports Python 3.6 - 3.9 on Windows, Mac, and Linux.

  • py2exe:

    Website || Repo || PyPi

    Supports Python 3.6 - 3.9 on Windows only.

  • py2app:

    Website || Repo || PyPi

    Supports Python 2.7 (?) and Python 3 (?) on Macs only. The exact range of supported Python versions is currently undocumented.


Of course, that's not the only way of doing things:

  • pynsist:

    Website || Repo || PyPi

    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.

    The pynsist tool itself requires Python 3.5+ to run, but supports bundling any version of Python with your program.

    Pynsist will create Windows installers only, but can be run from Windows, Mac, and Linux. See their FAQ for more details.

  • Nuitka:

    Website || Repo (Github mirror) || PyPi

    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.

    Supports Python 2.6 - 2.7 and Python 3.3 - 3.9 on Windows, Mac, and Linux.

  • cython:

    Website || Repo || PyPi

    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.

    Supports Python 2.6 - 2.7 and Python 3.3 - 3.9 on Windows, Mac, and Linux.


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


All this being said, converting your Python program into an executable isn't necessarily the only way of distributing your code. To learn more about what other options are available, see the following links:

这篇关于从 Python 项目创建单个可执行文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆