pyinstaller packages Python programs into exe execution files under Windows

When do you need a python packager?

Python The program requirespython interpreter. If the python interpreter is not installed, then Unable to run python program.
But when the person using the Python program does not know how to use the command to execute the program, or does not know how to install the Python interpreter, or just does not want to install so many things, just like clicking on the exe to execute, You need to package the Python program as an exe program under Windows.

In order to solve this situation, the packaging of Python programs is very important. The packaged program integrates the Python running environment, so that the Python program can be run on a server that does not have a Python interpreter installed.

How to package Python program files

  • Use pyinstaller tool for packaging.
  • Use py2exe tool for packaging.

pyinstaller

1. Install the pyinstaller module

Run the following command

pip install pyinstaller

Insert image description here
As shown above, if Successfully installed is displayed, the installation is successful.

2. pyinstaller packages a single simple python file

The command used for packaging is

pyinstaller -F python filename

How to actually do it? First, we need to write a python script. For example, create a new test.py file with the following content:

# -*- coding:utf-8 -*-

print(123)
print('hello')
print("I'm a man")
print('''python is good!
I like python...''')

Right-click to run directly inPycharm, or use Windows The built-incmd command line is run as follows to make sure the script can be used normally.
Insert image description here
Insert image description here

Then, useWindows cmd command line to enter the directory where the script is located, and execute the command:pyinstaller -F test.py, press Enter as follows:
Insert image description here
Insert image description here

As above, a bunch of things are printed, and the message finally prompts that a Windows application test.exe file is generated. So let's take a look at what's generated.

Insert image description here
Insert image description here

Double-click to start, or start from the cmd command line to run

Insert image description here
Insert image description here
The generated test.exe file has 7M So many, the actualtest.py script file is less than1K . This is because the packaged test.exe file contains the environment for running the Python script.
In this way, the Python script you wrote can be directly sent to others for use, and it can be run even without installing a Python environment.

3. pyinstaller packages multiple python files

Since it is multi-file packaging, first let’s create a new foldertest, and then in the folderCreate two new files in testtest.py, test2. py and the folder tool, and then in tool< Create a new filetest3.py in the a i=12> folder. The directory tree and file paths are shown below:

Insert image description here
Insert image description hereInsert image description here

The contents of the three folders are as follows:

test.py

# -*- coding:utf-8 -*-

from test2 import my_add

print(my_add(20, 15))

test2.py

# -*- coding:utf-8 -*-

from tool.test3 import say_num

def my_add(x, y):
    z = x + y
    return say_num(x, y, z)

test3.py

# -*- coding:utf-8 -*-

def say_num(num1, num2, sum):
    return '{0} + {1} = {2}...'.format(num1, num2, sum)

Then, run the program first to confirm that it can run normally, as follows:

Insert image description here

Then you can use the pyinstaller -F test.py test2.py tool/test3.py command to package. As follows:

Insert image description here
Insert image description here

As shown in the figure above, after successful execution, a test will be generated under the dist folder. exe file. This is the packaged file.

4. pyinstaller packages through configuration files

Let’s discuss what should be done when there are many packaged Python project script files.

This second method also uses thepyinstaller command, but the objects executed are different . Let’s use the above test example to illustrate.

First, useWindows cmd command line to entertest In the directory where the script is located, execute the command: pyinstaller -F test.py and press Enter. At this time, test< Generate build and dist in the /span> file, as shown below: test.spec directory folder, and also generated the

Insert image description here
Delete the build and dist folders, keeping the files test.spec and open it. This file is the configuration file when packaging. Now we modify the configuration file and then use the configuration file for packaging.

The contents of the opened file are as follows:
Insert image description here

Since our test program does not have resource files such as pictures, music, videos, etc., here we only use the file list for explanation. Add a script file list after the block_cipher = None code, and then replace the first parameter of the Analysis() function with this list. Modify the content as shown below

Insert image description here

After modification, save and close the file, and then execute the packaging command:pyinstaller test.spec Press Enter. After running, the same will appear in < The file test.exe is generated under the /span> folder. When there are many files, you only need to add the relative path of the file to the **.spec** file. dist

It is worth noting: The . spec file must be placed in the root directory of the project, that is, all project files must be in the directory where the . spec file is located. \color{red}{It is worth noting that the .spec file must be placed in the root directory of the project, that is, all project files must be in the directory where the .spec file is located. }It is worth noting:.spec Files must be placed in the root directory of the project, that is, all project files must be in .s In the directory where the pec file is located.

The above is a record of my actual operations during use. I share it with you as a note and for your own subsequent review.

For more details, please refer to the official website


Next article:py2exe packages Python programs into exe execution files under Windows

Guess you like

Origin blog.csdn.net/weixin_44131612/article/details/131847854