Simple instructions for using Pyinstaller

PyInstaller is a very practical Python library that can package Python code into executable files, so that users can run Python programs directly without installing the Python environment. This article will introduce PyInstaller's introduction, usage, advanced usage, detailed explanation of all parameters, detailed explanation of spec files, and the principle implementation of PyInstaller.

1. Introduction to PyInstaller

PyInstaller is a Python library that converts Python applications into standalone executables. PyInstaller supports cross-platform and can generate executable files on Windows, Linux and MacOS.

PyInstaller analyzes a Python program and packages the program into a complete executable, including all dependencies. In addition, PyInstaller can automatically detect Python dependent libraries and package them into executable files.

2. How to use PyInstaller

Install PyInstaller

To use PyInstaller, it needs to be installed first. PyInstaller can be installed from the command line using the following command:

pip install pyinstaller

Packaging Python programs

In PyInstaller, use the following command to package the Python program into an executable file:

pyinstaller yourscript.py

where yourscript.pyis the Python script you want to package.

After executing the above command, PyInstaller will automatically analyze your Python program and package it into an executable file, and package the dependencies into the executable file.

To run the packaged executable, just enter the following command on the command line:

./yourscript

packaged as a single file

By default, executables generated by PyInstaller contain multiple files. If you need to package all files into a single executable, you can use the following command:

pyinstaller --onefile yourscript.py

This command will generate a single executable yourscriptcontaining the Python program and all dependencies.

packaged as a console application

If your Python program is a console application, you can use the following command to package it as a console application:

pyinstaller --console yourscript.py

This command will generate a console application yourscriptthat will run in a command window.

Packaged as a GUI application

If your Python program is a GUI application, you can use the following command to package it as a GUI application:

pyinstaller --windowed --icon=youricon.ico yourscript.py

Among them, --windowedthe parameter means to generate a console-less window application, and --iconthe parameter means to define the program icon. This command will generate a GUI application yourscriptthat will not run in a command window.

3. Advanced usage of PyInstaller

set environment variables

If your Python program needs to use environment variables, it can be achieved by setting the environment variables of PyInstaller. For example, if your Python program needs to use MY_VARenvironment variables, you can use the following command:

pyinstaller --env MY_VAR=value yourscript.py

exclude dependencies

When packaging a Python program, some dependencies may cause packaging failures or errors in the resulting executable. In this case, you can exclude certain dependencies with a command like this:

pyinstaller --exclude-module=module_name yourscript.py

where module_nameis the name of the module you want to exclude.

Custom packaging options

If the default packaging options provided by PyInstaller cannot meet your needs, you can customize the packaging options through the spec file. The spec file is a Python script that contains packaging options and other necessary information. Use the following command to generate a spec file:

pyinstaller --name=yourappname yourscript.py --specpath=.

You can then edit the spec file to customize packaging options. Finally, use the following command to package the Python program into an executable file:

pyinstaller yourappname.spec

4. Detailed explanation of all parameters of PyInstaller

PyInstaller has many command line options, some of the most commonly used options are listed below:

  • --name: Specifies the name of the executable file.
  • --onefile: Packs all files into a single executable.
  • --console: Builds a console application.
  • --windowed: Generates a consoleless window application.
  • --icon: Specifies the icon for the executable.
  • --exclude-module: Exclude a module.
  • --add-data: Add a file or directory to the executable.
  • --debug: Enable debug information.
  • --log-level: Set the log level.
  • --clean: Clean up temporary files and cache.
  • --specpath: Specifies the output directory of the spec file.

5. Detailed explanation of the spec file

The spec file is the configuration file for PyInstaller, which contains all the information needed to package a Python program. When using PyInstaller to package a Python program, a spec file is generated by default.

The spec file is a Python script that contains packaging options and other necessary information. You can edit the spec file to customize packaging options. For example, you can specify the name of the executable file using code like this:

a = Analysis(['yourscript.py'],
             pathex=['/path/to/script_dir'],
             binaries=[],
             datas=[],
             hiddenimports=[],
             hookspath=[],
             runtime_hooks=[],
             excludes=[],
             win_no_prefer_redirects=False,
             win_private_assemblies=False,
             cipher=None,
             noarchive=False)
pyz = PYZ(a.pure, a.zipped_data,
             cipher=None)
exe = EXE(pyz,
          a.scripts,
          name='yourappname',
          exclude_binaries=True,
          exclude_dll=False,
          icon='youricon.ico',
          debug=False,
          bootloader_ignore_signals=False,
          strip=False,
          upx=True,
          upx_exclude=[],
          upx_includes=[],
          runtime_tmpdir=None,
          console=True )

where name='yourappname'the name of the executable file is specified.

6. Principle implementation of PyInstaller

The principle of PyInstaller is to convert the Python program into C language code, and compile the C language code into an executable file. Specifically, PyInstaller will analyze the Python program into an abstract syntax tree (AST), and then convert the AST into C language code.

PyInstaller will use the PyInstaller bootloader to start the Python interpreter and main program. During startup, PyInstaller reads all dependencies from the executable and loads them into memory. PyInstaller will then load the Python script into memory and execute the Python program.

in conclusion

This article introduces the introduction, usage, advanced usage, detailed explanation of all parameters, detailed explanation of spec file and the principle implementation of PyInstaller. PyInstaller is a very convenient Python library that can package Python programs into executable files and package dependencies into executable files, so that users can run Python programs directly without installing the Python environment.

Guess you like

Origin blog.csdn.net/weixin_40025666/article/details/131191945