Use of the pyinstaller library - a third-party library for packaging Python language scripts into executable files

The pyinstaller library is a third-party library that packages Python language scripts (.py files) into executable files, which can be used in operating systems such as windows\linux\Mac OS X.

pyinstaller needs to be installed using the pip/pip3 tool on the command line (console). The code is as follows:

pip3 install pyinstaller


Generate an executable file method:
Method 1: Use the command in the console

 pyinstaller  /home/np/Games/Games.py #(绝对路径或者相对路径)


Two folders, dist and build, will be generated. Where the build directory is the directory where pyinstaller stores temporary files, which can be safely deleted. The final packers are in the Games directory inside dist. Other files in the directory are dynamic link libraries for the executable Games.exe.
The result is as follows:

Method 2: You can generate an independent executable file for the Python source file through the -F parameter. The code is as follows:

pyinstaller -F /home/np/Games/Games.py

After execution, the Games.exe file appears in the dist directory without any dependent libraries, just execute it. As shown below:

Using the pyinstaller library requires attention to the following issues:

1) Spaces and periods (.) must not appear in the file path;
2) The source file must be encoded in UTF-8, and other encoding types are not supported. All source files written in IDLE
must be stored in UTF-8 encoding and can be used directly.

Use pyinstaller to package a python script into a windows exe executable program
First of all, it needs to be clear that you must install python on windows and use python under windows to perform the packaging operation before you can get the exe executable program (you can first install it in linux to complete debugging).
Because both py2exe and pyinstaller are released for the same platform. If the packaging operation is performed under linux, the final result must be a binary executable program under linux.

1. Install pyinstaller

pip install pyinstaller

2. Modify environment variables

You need to add the scripts directory in the python installation directory to the environment variable PATH. For
example here is

C:\Python27\scripts

3. Write code
4. Package
Enter the directory where the code is located and execute

pyinstaller -F your_script_name.py

The -F parameter means to generate only one exe file. Otherwise, a bunch of dll dynamic libraries will be generated at the same time, which is inconvenient to copy and use. After the packaging is completed, a dist directory will be
generated in the current directory, and the files in it are the exe files we need in the end . is different

6. Run the executable file under Linux
(1) First authorize the executable file:

chmod u+x /home/np/dist/Games 

Linux operation command:
cd enter a directory
ls display the files in the current directory
ls -l display the files and attributes

You can see that the Games file is executed by the executable attribute -x;
(2)

./Games

write picture description here
Second, the process is the same under Windows, you can also change the icon for the .exe file. code show as below:

pyinstaller -F -i ico_path  py_path

First of all, you need to download a normal ico, you cannot directly modify the suffix.
Download the picture:   https://www.easyicon.net
Change the picture to ico:  http://www.ico.la/
Enter the command pyinstaller -F -i “demo.ico” “main.py”

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324591128&siteId=291194637