SoFunction
Updated on 2024-10-29

Solution to pyinstaller can't execute error after packing a single exe

1. Description of the implementation environment

python version 3.7

Install pywin32 and pyinstaller directly using pip.

pip install pywin32
pip install pyinstaller

2. Use of third-party libraries

It is recommended to find the package of the third-party libraries before packing, copy the package to the same directory, and then use the above two ways to pack, otherwise the package will fail or even if the package is successful, the program will flashback. pyinstaller -p parameter is the scanning path added to the pyinstaller to pack the program, assuming that venv\Lib\site- packages is the package storage path, then you can also use the following commands to pack the program. packages is the package storage path, then you can also use the following command to package:

pyinstaller -p venv\Lib\site-packages -F

3、failed to execute script incorrect

first using

pyinstaller -F -w

The single .exe file that you get from exe packaging will prompt a failed to execute script error after running.

Troubleshooting process: use pyinstaller -D for exe packaging, get a directory file, execute the .exe file through the command line The 'six' package is required; normally this is bundled with this package error

Explain that after pyinstaller is packaged, it needs libraries such as SIX, and finally confirm that the following libraries need to be added to it:

import six
import packaging
import 
import 
import 

Of course, the six and packaging libraries are recommended to be installed using pip. After adding the above libraries, you can use pyinstaller -D to package and execute them without errors.

4. Data folder not found

Some programs contain data folders which can't be packaged directly as resource files, you need to create these data files in the folder where the executable is located. Normally we can use (( __file__)) in the script to get the path to the data folder, and then splice it together to get the data folder. However, when we use pyinstaller -F to package it into a single exe file, it works fine without reading the data folder, but once we open the data file, it flashes back and the command line window shows that the data file cannot be opened. Because PyInstaller will create a temporary folder temp, and the program code will run in this temporary folder, we can use the following statements to check the official run path:

  import sys
  import os
  print([0])
  print([0])
  print((()))
  print((([0])))

The result is (()) and (([0])) are the paths of the data folder. So you can get the file paths as follows, and then splice them together to get the real path to the data folder as needed:

  if hasattr(sys, '_MEIPASS'):
  # PyInstaller creates the temporary folder temp
  # and store the path in _MEIPASS
     = (())
  else:
    , filename = (( __file__))

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

pyinstaller -D 
pyinstaller -F 
pyinstaller -w -F 

Above this pyinstaller packaged a single exe can not be executed after the error solution is all I have shared with you, I hope to be able to give you a reference, and I hope you support me more.