pyinstaller cannot perform error resolution after packaging a single exe

  • 2021-06-29 11:38:33
  • OfStack

1. Enforcement of Environmental Instructions

python Version 3.7

Install pywin32, pyinstaller directly using pip

pip install pywin32
pip install pyinstaller

2. Use of third-party libraries

It is recommended that you always find the package of the third party library before packaging, copy the package to the same directory as myfile.py, and then use the above two methods to package, otherwise the package will fail or even if the package is successful, the program will flip.The pyinstaller-p parameter is the scan path for the added pyinstaller packager. Assuming venvLibsite-packages is the package store path, you can also package using the following commands:

pyinstaller -p venv\Lib\site-packages -F xxx.py

3. failed to execute script Error

use first

pyinstaller -F -w code.py

Package the exe, resulting in a single.exe file that prompts for an failed to execute script error after running

Schedule miss: exe is packaged using pyinstaller-D code.py, a catalog file is obtained, and executed from the command line.exe file prompts The'six'package is required;normally this is bundled with this package Error

Explain that after pyinstaller is packaged, libraries such as six are required, and finally confirm that the following libraries need to be added to code.py:


import six
import packaging
import packaging.version
import packaging.specifiers
import packaging.requirements

Of course, the six and packaging libraries recommend installing with pip.After joining the above libraries, there will be no more errors in packaging execution using pyinstaller-D code.py.

4. No data folder found

Some programs contain data folders that cannot be packaged directly as resource files. These data files need to be created in the folder in which the executed files are located.1 Normally in scripting we can use os.path.split (os.path.abspath (ufile_u)The path to code.py is then stitched together into a data folder.But then packaged into a single exe file using pyinstaller-F code.py, it works well without reading the data folder. Once the data file is opened, it will flip away and the command line window will show that the data file cannot be opened.Because PyInstaller creates a temporary folder, temp, in which the program code runs, we can view the official run path in the following statements:


  import sys
  import os
  print(sys.path[0])
  print(sys.argv[0])
  print(os.path.dirname(os.path.realpath(sys.executable)))
  print(os.path.dirname(os.path.realpath(sys.argv[0])))

The result is that os.path.dirname (os.path.realpath (sys.executable)) and os.path.dirname (os.path.realpath (sys.argv[0]) are the paths to the containing data folders.So you can get the file path as follows, then stitch together the real path of the data folder as needed:


  if hasattr(sys, '_MEIPASS'):
  # PyInstaller Will create temporary folders temp
  #  And store the path in _MEIPASS in 
    self.appPath = os.path.dirname(os.path.realpath(sys.executable))
  else:
    self.appPath, filename = os.path.split(os.path.abspath( __file__))

After the modification is completed, the packages are packaged in the following three ways and run successfully


pyinstaller -D code.py
pyinstaller -F code.py
pyinstaller -w -F code.py

Related articles: