PyInstaller is a tool that packages Python applications and all their dependencies into a single executable file. Users can run the packaged application without needing to install a Python interpreter or any modules.
Normally when executing Python scripts, we need a Python environment and must pre-install all required modules. This can cause inconvenience when sharing scripts with non-technical users. By bundling the program along with the Python environment and all runtime dependencies into a single executable file, program distribution becomes much easier. To some extent, this also helps protect the source code from being exposed.
- Install PyInstaller
- Building from .spec Files
- Adding Version Information
- Extracting Python Source Code
- PyInstaller Assistant (a graphical tool)
PyInstaller Usage
pyinstaller.exe command syntax:
pyinstaller [options] scriptfile [scriptfile …] | specfile
Only common options for Windows systems are listed below. For complete documentation, please refer to:
Positional Arguments
scriptfilePython script file to processspecfile.spec file
Common Options
-h,--helpShow help message and exit-v,--versionShow program version and exit--distpath DIROutput directory for bundled application (default:./dist)--workpath WORKPATHTemporary directory (default:./build)--cleanClean PyInstaller cache and remove temporary files before building--log-level LEVELLevel of detail for build-time console messagesLEVEL may be one of TRACE, DEBUG, INFO, WARN, DEPRECATION, ERROR, FATAL (default: INFO).
-D, --onedirCreate one-folder bundle containing executable (default)-F, --onefileCreate one-file bundled executable--specpath DIRFolder to store generated spec file (default: current directory)-n, --name NAMEName for bundled application and spec file (default: first script's name)--add-data SOURCE:DESTAdditional non-binary files to include in the application--add-binary SOURCE:DESTAdditional binary files to include in the executableSimilar to --add-data option. This option can be used multiple times;
-p, --paths DIRPath to search for importsMultiple paths allowed, separated by
:or use this option multiple times;--hidden-import, --hiddenimport MODULENAMEName an import not visible in the code--collect-submodules MODULENAMECollect all submodules from the specified package or module--collect-data, --collect-datas MODULENAMECollect all data from the specified package or module--collect-binaries MODULENAMECollect all binaries from the specified package or module--collect-all MODULENAMECollect all submodules, data files and binaries from the specified package or module--exclude-module EXCLUDESExclude module or package--splash IMAGE_FILEAdd splash screen with image IMAGE_FILEThe splash screen can display progress during unpacking;
-c, --console, --nowindowedOpen console window (default)-w, --windowed, --noconsoleDo not provide console window-i, --icon <FILE.ico or FILE.exe,ID or Image or "NONE">Add icon to Windows executable (default: PyInstaller's icon);
- FILE.ico: Apply icon to Windows executable
- FILE.exe,ID: Extract icon with specified ID from exe file
- Image: If input image isn't in ico format, PyInstaller will attempt conversion using Pillow (if installed)
- NONE: Don't add any icon
--version-file FILEAdd version resource from FILE to exe--manifest <FILE or XML>Add manifest file to exe
Common Examples
Package myscript.py into a single exe file without an icon:
pyinstaller -F -i NONE d:\myscript.py