Create a Single Executable from a Python Project

Create a single executable from a Python project

There are several different ways of doing this.


The first -- and likely most common -- way is to use "freeze" style programs. These programs work by bundling together Python and your program, essentially combining them into a single executable:

  • PyInstaller:

    Website || Repo || PyPi

    Supports Python 3.7 - 3.10 on Windows, Mac, and Linux.

  • cx_Freeze:

    Website || Repo || PyPi

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

  • py2exe:

    Website || Repo || PyPi

    Supports Python 3.7 - 3.10 on Windows only.

  • py2app:

    Website || Repo || PyPi

    Supports Python 3.6 - 3.10 on Macs only.

The main thing to keep in mind is that these types of programs will generally only produce an exe for the operating system you run it 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.


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.10 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.7 and Python 3.3 - 3.11 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:

  • https://packaging.python.org/overview/#packaging-python-applications
  • https://docs.python-guide.org/shipping/packaging/#for-linux-distributions

How can I make a Python script standalone executable to run without ANY dependency?

You can use py2exe as already answered and use Cython to convert your key .py files in .pyc, C compiled files, like .dll in Windows and .so on Linux.

It is much harder to revert than common .pyo and .pyc files (and also gain in performance!).

How to package Python Project into a standalone executable?

PyInstaller

PyInstaller is a program that freezes (packages) Python programs into stand-alone executables, under Windows, GNU/Linux, Mac OS X, FreeBSD, Solaris and AIX. Its main advantages over similar tools are that PyInstaller works with Python 2.7 and 3.4—3.7, it builds smaller executables thanks to transparent compression, it is fully multi-platform, and use the OS support to load the dynamic libraries, thus ensuring full compatibility.

It work even if users do not have python installed.

Here an example from a github project. As you can see, you can download sources, but also a zip containing every package used to run your project. In this example, it contains many files, but you can package everything into a single .exe file.

How to use (Manual):

Install PyInstaller from PyPI:

pip install pyinstaller

Go to your program’s directory and run:

pyinstaller yourprogram.py

This will generate the bundle in a subdirectory called dist.

You can use --onefile argument in order to generate the bundle with only a single executable file.

Case specific:

You asked how to get arguments send by the user. Here is some way to do it, more or less convenient:

  • Use input(),
  • Use a config file,
  • Use default parameter.

Generate an executable from python project

use pyinstaller. just run
pip install pyinstaller and then open the folder the file is located in with shell and run pyinstaller --onefile FILE.py where file is the name of the python file that should be run when the exe is run

How to convert a python project into an executable file with all additional scripts?

pyinstaller uses a few dirty tricks to compress a bunch of files into one

I recommend using cx_Freeze instead along with inno setup installer maker

do pip install cx_Freeze to install that and go here for inno setup

then copy the following into a file named setup.py in the same folder as your project

from cx_Freeze import setup, Executable

setup(name = "YOUR APP NAME" ,
version = "1.0.0" ,
description = "DESCRIPTION" ,
executables = [Executable("PYTHON FILE", base = "Win32GUI")]
)

lastly run python setup.py build

if you want as onefile download this file here

just edit the file a bit and use inno compiler to make into installer

How to convert tkinter script which uses multiple image resources to create a single executable

You can try using PyInstaller

PyInstaller - How to convert to .exe

1. Installation

The pyinstaller module makes you able to create a standalone exe file for pc from a python script, so that you can use it on other computers than do not have python or your python version.

To install pyinstaller: pip install

2. Create an exe

After you installed pyinstaller, go in the folder where your python script to be transformed in exe is and type in the command line (go in the address bar on top of the window and type cmd). Then type :

pyinstaller yourscript.py

where your script is the name of your file.

You will find a folder called “dist” where there is a folder called yourscript (if the name of your script was this) where there are many files and the exe file that will allow you to run the script also on other computer. You just have to copy the whole folder.

3. What to do if there are images

If you use images, that are in the same folder, for example, you can copy manually into the folder made by pyinstaller or you can run this command to make it do it for you:

pyinstaller –add-data “*.png;.” yourscript.py

This way you will see that in the folder where your exe is there are also the png files (all of them, if you want just one do not put the asterisc, but the whole name of the file.

External links - PyInstaller



Related Topics



Leave a reply



Submit