SoFunction
Updated on 2024-11-16

Examples of commonly used PyInstaller packaging commands

After learning pyqt, the interface was designed and needed to be packaged as an exe program.

Every time I pack, I have to look up information for a long time, so I'm going to record the common commands.

PyInstaller is a Python application packaging tool that packages Python programs into a single standalone executable.

To package a Python program with PyInstaller, use the pyinstaller command on the command line. The basic syntax of the command is as follows:

pyinstaller [options] script[.py]

where script is the filename of the Python script file to be packaged, possibly with a .py suffix, and options is an optional command-line option that can be used to specify how to package the file, where to send the output file, and so on.

Here are some common command line options:

  • -F: Packages all files into a single executable.
  • -D: Packages all files into a single directory containing the executable and all dependent files.
  • -c: Combines a program with a command prompt in order to run it at the command prompt.
  • -d: Pack debugging information into an executable file.
  • -onefile: Packages all files into a single executable.
  • -o: Specifies the location of the output file.
  • -w: packaged as a window file.
  • -p DIR, -path=DIR: set the import path to import the required modules

For example, if you want to use PyInstaller to package a Python script file named and package all the files into a single executable, you can use the following command:

pyinstaller -F 

The following command can be used to specify the name of the executable file:

pyinstaller  -n my_script

Added an icon to the program:

Pyinstaller -F -i Risk icon.ico 

Packaging pyqt designed interfaces:

pyinstaller --paths D:\anaconda3\Lib\site-packages\PyQt5\Qt\bin  -F -w -i Risk icon.ico  Risk calculation plus capital management_logic (loanword).py

-paths D:\anaconda3\Lib\site-packages\PyQt5\Qt\bin This line of command is indispensable!

Note: When packing, you need to go to the directory where the py file exists, and then go to the cmd interface to pack.

pyinstaller --paths D:\anaconda3\Lib\site-packages\PyQt5\Qt\bin  -F -w  -i Risk Analysis Icon.ico Risk Calculation Interface Lite Logic.py

Packed it up, tested it and it runs fine!

P.S.: pyinstaller runs with an error after packaging.

When running the Python  program after packaging it with PyInstaller  and encountering problems, possible causes and solutions include:

  • Insufficient disk space. Check disk space, especially the system disk, to make sure there is enough room to run the program.1
  • Missing module. Check if the environment has the required packages installed. If not, use the `pip install` command to install them. For specific modules, such as `mmcv`, the `--hidden-import` parameter needs to be added to the package command.
  • File path problem. Make sure there are no Chinese characters or spaces in the path of the packaged file.23
  • Permission problems. Check to see if the file is occupied by another program, or if it is blocked by antivirus software. It may be necessary to run the command as administrator, or disable the antivirus program and try packing again.4
  • Third-party library version mismatch. Make sure the Pyinstaller version matches the Python and third-party library versions. If they do not match, try uninstalling and reinstalling the appropriate version.2
  • Missing dependency packages. In PyCharm, view the third-party libraries used in your project via Settings and copy these libraries to the path specified during packaging.5
  • Hidden import issue. Add the `--hidden-import` parameter to the packaging command to specify the modules that need to be hidden from import.16
  • System files are missing. Check system files, e.g. ``, possibly due to missing or corrupted system files.6

If none of the above methods solves the problem, you may want to consider replacing the packing tool or checking the system environment.

summarize

To this article on the PyInstaller commonly used packaging commands are introduced to this article, more related PyInstaller commonly used packaging commands content please search for my previous articles or continue to browse the following related articles I hope you will support me in the future more!