Explain Codes LogoExplain Codes Logo

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

python
packaging
pyinstaller
cross-platform
Anton ShumikhinbyAnton Shumikhin·Aug 9, 2024
TLDR

Transform your Python script into a standalone executable by using a tool called PyInstaller. Start by installing it with pip install pyinstaller, then run pyinstaller --onefile your_script.py to create your executable. This --onefile option ensures your output is a single EXE file, making distribution straightforward. The executable will be generated in the dist folder.

Tools for Python to executable conversion

The packaging landscape for Python scripts is rich with alternatives. Here's a brief comparative snapshot:

  • PyInstaller: A versatile tool, working across Windows, Linux, macOS, and allows for single-file executables.
  • py2exe: Exclusively for Windows, excellent for compiling to bytecode for improved performance.
  • Nuitka: Offers a performance boost by compiling Python into C++ and creating standalone executables.
  • Cython: Enables C-level performance by compiling Python to .pyc and further to C, yielding efficient standalone executables.

Tackling common problems with PyInstaller

Packaging tools can throw up some common issues, but there are workaround solutions at hand:

  • ImportError: This can be resolved by including side-packages in your script, or you might consider downgrading PyInstaller.
  • Multiprocessing issues: If you face problems with multiprocessing while using PyInstaller, check their documentation for potential hybrid solutions.
  • Big size exectuable files: Opt for --onedir option with PyInstaller if your standalone executable is turning out to be too large.
  • Antivirus alerts: Sometimes, AV programs flag executable files, you can circumvent this issue by code signing or considering other packaging tools.

Here's a little slice of sanity for Windows users, a batch file to simplify PyInstaller:

@echo off echo Script for the developers too lazy to type commands manually! pyinstaller --onefile --windowed --icon=app.ico your_script.py echo. echo Good news! Your coffee is still hot and your executable is ready in the dist folder. pause

Investigating special cases with dependencies

While trying to package Python scripts, you may often need to cater to specific requirements and issues. Here's how you handle them:

  • pynput package: pip install pynput==1.6.8, as some newer versions may cause issues.
  • 64-bit Windows Singleton issues: py2exe can struggle with single file packages on 64-bit systems.
  • PyQt apps: If you're creating cross-platform apps using PyQt, both PyInstaller and py2exe can encounter specific issues. Review their documentation for fixes.

Here are some useful resources:

Essential notes on cross-platform packaging

PyInstaller excels when creating cross-platform standalone executables (Windows, Linux, and macOS). Keep these factors in mind:

  • Testing: Always test your standalone executable across different platforms, as there may be variations in filesystem structures and permissions.
  • macOS specifics: On macOS, to avoid terminal-based apps, you may need to use --windowed, and remember to sign your app for Gatekeeper.