Create a single executable from a Python project

前端 未结 3 1400
梦谈多话
梦谈多话 2020-11-22 05:01

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 p

相关标签:
3条回答
  • 2020-11-22 05:21

    Here are some common ones. Unless explicitly noted, all projects listed below are being actively maintained as of my last edit (January 2020).

    I've also included links to their respective websites, repos, and PyPi listings in case you want to check for yourself on how frequently they've been updated.

    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.


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

    • PyInstaller:

      • Links: Website || Repo || PyPi
      • Supports: Python 2.7 and Python 3.4 - 3.7 on Windows, Mac, and Linux.
    • cx_Freeze:

      • Links: Website || Repo || PyPi
      • Supports: Python 2.7 and 3.5 - 3.8 on Windows, Mac, and Linux.
    • py2app:

      • Links: Website || Repo || PyPi
      • Supports: Python 2.7 and 3.3 (?) - 3.7 (?) on Mac only.
      • Note: As of my last edit, I've been unable to conclusively determine the exact range of Python versions supported by pyapp. The latest release mentions supporting Python 3.7.

    Note: prior versions of this post included write-ups for py2exe (repo) and bbfreeze (repo). Both projects have been removed from this list: they've seen little-to-no activity for several years and appear to be completely unmaintained. See edit history for the old writeups.


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

    • 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.8 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 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:

    • https://packaging.python.org/overview/#packaging-python-applications
    • https://docs.python-guide.org/shipping/packaging/#for-linux-distributions
    0 讨论(0)
  • 2020-11-22 05:21

    pyinstaller is still under active development. You can see the latest changes on GitHub.

    It has support for all three major platforms:

    • Windows (32-bit and 64-bit)
    • Linux (32-bit and 64-bit)
    • Mac OS X (32-bit and 64-bit)

    and it supports Python versions 2.6 and 2.7. It does not support Python 3, but there is an experimental Python 3 branch.

    Update

    As of version 3.2.1 it supports Python 2.7, 3.3-3.5

    0 讨论(0)
  • 2020-11-22 05:22

    Better use pip install auto-py-to-exe which provides a beautiful UI with all options required to create an executable.

    After installing just type auto-py-to-exe in your terminal or command prompt.

    0 讨论(0)
提交回复
热议问题