Linux command
pyinstaller 命令
网络
复制后可按需替换文件名、目录或参数。
常用示例
Build a one-folder bundle
pyinstaller [path/to/myscript.py]
Build a single-file executable
pyinstaller --onefile [path/to/myscript.py]
Build a GUI app
pyinstaller --windowed --onefile [path/to/app.py]
Set a custom name and icon
pyinstaller --name [MyApp] --icon [path/to/icon.ico] [path/to/app.py]
Include extra data files
pyinstaller --add-data "[path/to/data.json]:[data]" [path/to/app.py]
Force-include a module
pyinstaller --hidden-import [package.module] [path/to/app.py]
Rebuild from a saved spec file
pyinstaller [path/to/myscript.spec]
Clean caches
pyinstaller --clean --noconfirm --onefile [path/to/app.py]
说明
pyinstaller packages a Python program together with its interpreter and every imported module into a self-contained bundle that runs on machines without Python installed. It works by tracing imports starting from the entry script, copying the resulting set of modules, shared libraries, and data files into a build directory, and producing either a one-folder layout (--onedir, default) or a single executable file (--onefile) extracted to a temporary location at runtime. The build runs in two phases. First, PyInstaller writes a _script_.spec file that captures the script paths, options, and analyzer hints for the project. Second, it processes that spec file to assemble the actual bundle in dist/. Editing the spec file directly is the recommended way to handle non-trivial projects, since spec files are plain Python and let you control hooks, binaries, data trees, and runtime options that the CLI flags cannot fully express. PyInstaller is cross-platform but not cross-compiling: a bundle built on Linux runs on Linux, a Windows bundle must be built on Windows, and a macOS bundle on macOS. It supports CPython 3.8 and later, and integrates hooks for hundreds of popular packages (NumPy, PyQt, Django, TensorFlow) so they bundle correctly out of the box.
参数
- -F, --onefile
- Produce a single executable file. At launch the binary self-extracts into a temporary directory and runs from there.
- -D, --onedir
- Produce a folder containing the executable and its dependencies. This is the default and the fastest to start.
- -n _NAME_, --name _NAME_
- Assign a name to the bundled app and to the generated .spec file. Defaults to the script's base name.
- -w, --windowed, --noconsole
- On Windows and macOS, do not attach a console window. Use for GUI applications.
- -c, --console, --nowindowed
- Force a console window (the default on most platforms).
- --icon _FILE_
- Apply a custom icon: .ico on Windows, .icns on macOS. Use NONE to suppress the default.
- --add-data _SOURCE_:_DEST_
- Bundle additional data files or directories under _DEST_ inside the bundle. Use ; instead of : as the separator on Windows.
- --add-binary _SOURCE_:_DEST_
- Same as --add-data but for shared libraries that need binary handling.
- --hidden-import _MODULE_
- Force a module to be included even when the static analyzer cannot see the import (typical for plugin systems or dynamic imports).
- -p _DIR_, --paths _DIR_
- Prepend _DIR_ to the module search path, like adding to PYTHONPATH for analysis only.
- --clean
- Wipe PyInstaller's cache and temporary build artifacts before starting.
- -y, --noconfirm
- Replace the output directory without asking for confirmation.
- --log-level _LEVEL_
- Set verbosity: TRACE, DEBUG, INFO (default), WARN, ERROR, FATAL.
- --specpath _DIR_
- Place the generated .spec file in _DIR_ instead of the current directory.
- --distpath _DIR_, --workpath _DIR_
- Override the dist/ and build/ output directories.
- --upx-dir _DIR_
- Use the UPX executable packer from _DIR_ to compress the bundled binaries.
FAQ
What is the pyinstaller command used for?
pyinstaller packages a Python program together with its interpreter and every imported module into a self-contained bundle that runs on machines without Python installed. It works by tracing imports starting from the entry script, copying the resulting set of modules, shared libraries, and data files into a build directory, and producing either a one-folder layout (--onedir, default) or a single executable file (--onefile) extracted to a temporary location at runtime. The build runs in two phases. First, PyInstaller writes a _script_.spec file that captures the script paths, options, and analyzer hints for the project. Second, it processes that spec file to assemble the actual bundle in dist/. Editing the spec file directly is the recommended way to handle non-trivial projects, since spec files are plain Python and let you control hooks, binaries, data trees, and runtime options that the CLI flags cannot fully express. PyInstaller is cross-platform but not cross-compiling: a bundle built on Linux runs on Linux, a Windows bundle must be built on Windows, and a macOS bundle on macOS. It supports CPython 3.8 and later, and integrates hooks for hundreds of popular packages (NumPy, PyQt, Django, TensorFlow) so they bundle correctly out of the box.
How do I run a basic pyinstaller example?
Run `pyinstaller [path/to/myscript.py]` in a terminal, then adjust file names, paths, flags, or remote targets for your system.
What does -F, --onefile do in pyinstaller?
Produce a single executable file. At launch the binary self-extracts into a temporary directory and runs from there.