Home > Net >  How to compile a python script into executable program and can be use by others
How to compile a python script into executable program and can be use by others

Time:11-29

My python script is finished and working and I want to compile and have other users enjoy/benefit from it. The users don't need to install Pycharm or Visual Studio Code, something like an executable file or run in a command prompt then execute on their local machine or is there a way to convert it on a Tampermonkey Script?

How do I achieve this? Thank you very much in advance!

Googled and Youtubed but it's not what I'm looking for.

CodePudding user response:

This question is probably answered multiple times, but the PyInstaller module is a great way to generate an executable that will run on Windows, and an app that will run on macOS.

Check out PyInstaller on PyPI.org: https://pypi.org/project/pyinstaller/

Project description PyPI PyPI - Python Version Read the Docs (version) PyPI - Downloads PyInstaller bundles a Python application and all its dependencies into a single package. The user can run the packaged app without installing a Python interpreter or any modules.

Documentation: https://pyinstaller.org/

Code: https://github.com/pyinstaller/pyinstaller

PyInstaller reads a Python script written by you. It analyzes your code to discover every other module and library your script needs in order to execute. Then it collects copies of all those files – including the active Python interpreter! – and puts them with your script in a single folder, or optionally in a single executable file.

PyInstaller is tested against Windows, macOS, and GNU/Linux. However, it is not a cross-compiler: to make a Windows app you run PyInstaller in Windows; to make a GNU/Linux app you run it in GNU/Linux, etc. PyInstaller has been used successfully with AIX, Solaris, FreeBSD and OpenBSD, but is not tested against them as part of the continuous integration tests.

Main Advantages Works out-of-the-box with any Python version 3.7-3.11.

Fully multi-platform, and uses the OS support to load the dynamic libraries, thus ensuring full compatibility.

Correctly bundles the major Python packages such as numpy, PyQt5, PySide2, PyQt6, PySide6, wxPython, matplotlib and others out-of-the-box.

Compatible with many 3rd-party packages out-of-the-box. (All the required tricks to make external packages work are already integrated.)

Works with code signing on macOS.

Bundles MS Visual C DLLs on Windows.

Installation PyInstaller is available on PyPI. You can install it through pip:

pip install pyinstaller Requirements and Tested Platforms Python: 3.7-3.11. Note that Python 3.10.0 contains a bug making it unsupportable by PyInstaller. PyInstaller will also not work with beta releases of Python 3.12.

tinyaes 1.0 (only if using bytecode encryption). Instead of installing tinyaes, pip install pyinstaller[encryption] instead.

Windows (32bit/64bit): PyInstaller should work on Windows 7 or newer, but we only officially support Windows 8 .

Support for Python installed from the Windows store without using virtual environments requires PyInstaller 4.4 or later.

Note that Windows on arm64 is not yet supported. If you have such a device and want to help us add arm64 support then please let us know on our issue tracker.

Linux: GNU libc based distributions on architectures x86_64, aarch64, i686, ppc64le, s390x.

musl libc based distributions on architectures x86_64, aarch64.

ldd: Console application to print the shared libraries required by each program or shared library. This typically can be found in the distribution-package glibc or libc-bin.

objdump: Console application to display information from object files. This typically can be found in the distribution-package binutils.

objcopy: Console application to copy and translate object files. This typically can be found in the distribution-package binutils, too.

Raspberry Pi users on armv5-armv7 should add piwheels as an extra index url then pip install pyinstaller as usual.

macOS (x86_64 or arm64): macOS 10.15 (Catalina) or newer.

Supports building universal2 applications provided that your installation of Python and all your dependencies are also compiled universal2.

Usage Basic usage is very simple, just run it against your main script:

pyinstaller /path/to/yourscript.py For more details, see the manual.

Untested Platforms The following platforms have been contributed and any feedback or enhancements on these are welcome.

FreeBSD ldd

Solaris ldd

objdump

AIX AIX 6.1 or newer. PyInstaller will not work with statically linked Python libraries.

ldd

Linux on any other libc implementation/architecture combination not listed above.

Before using any contributed platform, you need to build the PyInstaller bootloader. This will happen automatically when you pip install pyinstaller provided that you have an appropriate C compiler (typically either gcc or clang) and zlib’s development headers already installed.

Support Official debugging guide: https://pyinstaller.org/en/v5.6.2/when-things-go-wrong.html

Assorted user contributed help topics: https://github.com/pyinstaller/pyinstaller/wiki

Web based Q&A forums: https://github.com/pyinstaller/pyinstaller/discussions

Email based Q&A forums: https://groups.google.com/g/pyinstaller

Changes in this Release You can find a detailed list of changes in this release in the Changelog section of the manual.

  • Related