PyInstallerPackage single fileexeMatters needing attention


0. Website

  • The official website

www.pyinstaller.org/

  • The official manual

Pyinstaller. Readthedocs. IO/en/v3.3.1 /


1. PyInstallerPackage single fileexeThe problem encountered when

I wrote a small Python script to count the number and duration of all the video files in the current directory and its subdirectories, using subprocess.Popen to call the external EXE file ffprobe.exe, and using PyQt5 to create a simple little form. Show program icon in the form title, source execution effect is as follows:

However, there are four problems when using PyInstaller to package a single file, EXE:

  • Can’t findPyQt5.sip
  • Could not find the window icon
  • Can’t findffprobe.exe
  • The script is running properlyffprobe.exeerror

The following are solutions for each problem. Some of these problems have multiple solutions, one of which is provided here for reference only.


2. Can’t find itPyQt5.sipThe solution of

PyInstaller does not import the module, so import it explicitly in the source code:

from PyQt5 import sip
Copy the code

Reference site: blog.csdn.net/yueguangman…


3. Unable to find a solution for window ICONS

The reason why the window icon cannot be found is that when the single file is packaged, PyInstaller changes the resource storage path and cannot be referenced using the original relative path.

So first add the resource_path function to the source code, which returns the absolute path of the resource:

def resource_path(relative_path) :
    Returns the absolute path of the resource. ' ' '
    if hasattr(sys, '_MEIPASS') :# PyInstaller creates a temporary folder temp
        # and store the path in _MEIPASS
        base_path = sys._MEIPASS
    else:
        base_path = os.path.abspath('. ')
    return os.path.join(base_path, relative_path)
Copy the code

Self.setwindowicon (QIcon(resource_path(r’img\ movie.png ‘))))

As a final step, use the –add-data option to add all resource files when generating the EXE using the PyInstaller command. On Windows this option is in the format –add-data=” source address; Target address “, can be used multiple times. Other system command formats require address separators to be separated from semicolons; Change it to colon:, see the official manual. Pyinstaller –add-data=”img movie.png; img” –add-data=”ffprobe.exe; .” -i=”img movie.ico “-fw movielen.py


4. I couldn’t find itffprobe.exeThe solution of

The cause of this problem is the same as not finding the window icon, so the solution is the same. Also note that the last time you use the PyInstaller command you need to add ffprobe.exe with the –add-data option. Alternatively, you can do this by writing a Spec configuration file, which you can refer to the official manual for details.


5. Usesubprocess.PopenSolution that cannot call an external program

In my script, the external executable, ffprobe.exe, was invoked through subprocess.popen, but failed to run as a single file, EXE. The reason for this is that I used the -w option during the packaging process, which means no console mode, and I needed to modify the source code to explicitly specify the stdin, stdout, and stderr arguments to subprocess. The source code is modified as follows: sp = subprocess.Popen(cmd, stdin = subprocess.PIPE, stdout = subprocess.PIPE, stderr = subprocess.STDOUT, shell = True)

Finally, using the following command, successfully packaged single file EXE (no console mode), and has its own icon, picture resources perfectly displayed, can encapsulate and call other EXE files. Pyinstaller –add-data=”img movie.png; img” –add-data=”ffprobe.exe; .” -i=”img movie.ico “-fw movielen.py

The final result is as follows:

Reference website: github.com/pyinstaller…