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

I'm building a Python application and don't want to force my clients to install Python and modules.

So, is there a way to compile a Python script to be a standalone executable?


Solution 1:

You can use PyInstaller to package Python programs as standalone executables. It works on Windows, Linux, and Mac.

PyInstaller Quickstart

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.

pyinstaller -F yourprogram.py

Adding -F (or --onefile) parameter will pack everything into single "exe".

pyinstaller -F --paths=<your_path>\Lib\site-packages  yourprogram.py

running into "ImportError" you might consider side-packages.

 pip install pynput==1.6.8

still runing in Import-Erorr - try to downgrade pyinstaller - see Getting error when using pynput with pyinstaller

For a more detailed walkthrough, see the manual.

Solution 2:

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

Solution 3:

You might wish to investigate Nuitka. It takes Python source code and converts it in to C++ API calls. Then it compiles into an executable binary (ELF on Linux). It has been around for a few years now and supports a wide range of Python versions.

You will probably also get a performance improvement if you use it. It is recommended.

Solution 4:

Yes, it is possible to compile Python scripts into standalone executables.

PyInstaller can be used to convert Python programs into stand-alone executables, under Windows, Linux, Mac OS X, FreeBSD, Solaris, and AIX. It is one of the recommended converters.

py2exe converts Python scripts into only executable on the Windows platform.

Cython is a static compiler for both the Python programming language and the extended Cython programming language.

Solution 5:

I would like to compile some useful information about creating standalone files on Windows using Python 2.7.

I have used py2exe and it works, but I had some problems.

  • It has shown some problems for creating single files in Windows 64 bits: Using bundle_files = 1 with py2exe is not working;

  • It is necessary to create a setup.py file for it to work. http://www.py2exe.org/index.cgi/Tutorial#Step2;

  • I have had problems with dependencies that you have to solve by importing packages in the setup file;

  • I was not able to make it work together with PyQt.

This last reason made me try PyInstaller http://www.pyinstaller.org/.

In my opinion, it is much better because:

  • It is easier to use.

I suggest creating a .bat file with the following lines for example (pyinstaller.exe must be in in the Windows path):

pyinstaller.exe --onefile MyCode.py
  • You can create a single file, among other options (https://pyinstaller.readthedocs.io/en/stable/usage.html#options).

  • I had only one problem using PyInstaller and multiprocessing package that was solved by using this recipe: https://github.com/pyinstaller/pyinstaller/wiki/Recipe-Multiprocessing.

So, I think that, at least for python 2.7, a better and simpler option is PyInstaller.