Convert the written .py/.java program into an .exe file

Table of contents

1. Background

1.1. Preface

1.2. Description

2. Analysis of advantages and disadvantages

2.1. Advantages

2.2. Disadvantages

3. Steps to convert .py into .exe

3.1. Download the required libraries

3.2. pycharm console.py->.exe file

3.3. Command line .py->.exe file

4. .py->.exe->Errors and solutions that may be encountered when executing .exe

4.1. ''pyinstaller'' is not recognized as an internal or external command, operable program or batch file.

5. .exe->.py file

6. Change .java->.exe


1. Background

1.1. Preface

When we write the code and test that the function meets the requirements, it is possible to execute this program every day (for example, I wrote a crawler script and run it regularly every day to get the updated content of the novel I want to read), then the program must be opened every time Editor or command line? Or, if I write a program that can achieve a certain cool function and show off to the girl I like / share it with my brothers , do I need to send a large string of source code to them and let them execute it once?

Of course that won't work. A good solution is to turn this program into an executable (.exe) file and execute it with a click. Wouldn't it be very convenient?

1.2. Description

Operating system: win 10

Editor:pycharmedu

Language and version: python 3.10

Required library: pyinstaller

Implementation idea: Use Python’s third library to generate executable files (.exe)

Introduction to parameters used by pyinstaller:

-F, function: generate a single executable file

-c, function: specifies to use the command line window to run the program

-w, function: Specify not to display the command line window when the program is running

-i, function: add the icon of the executable file (-i image path)

-n, function: the name of the executable file after generation

For example, generate an executable file, set the file icon, and the generated name is Paidaxing.exe. If the command line window is not displayed when executing the program, the executed instructions should be:

pyinstaller -F -w -i image path -n Patrick Star program file.py

2. Analysis of advantages and disadvantages

2.1. Advantages

1) Click to open and run, convenient

2) If you encounter various problems when generating .exe files and can solve them, you can increase your knowledge.

2.2. Disadvantages

1) The generation process is prone to various errors.

2) The generated .exe file takes up a lot of space (because many irrelevant libraries are packaged)

3. Steps to convert .py into .exe

3.1. Download the required libraries

1) Download in pycharm, the steps are shown in Figures 1 and 2 below:

 figure 1

 figure 2

After clicking download, just wait. If the network is fast, the download can be completed in 20 seconds. If the network is slow, it may take a few minutes.


2) You can also download it from the pycharm console, as shown in Figure 3 below:

image 3

3) Download the required libraries on the command line

The steps are as shown in Figures 4, 5, and 6:

Win+R enter cmd to open the command line:

Figure 4

Input: pip install pyinstaller

Figure 5

result:

 Figure 6

3.2. pycharm console.py->.exe file

The steps are as shown in Figures 7, 8, and 9:

1) Switch the path to the location where the .exe file needs to be generated and enter the command (installer -F -w filename.py), as shown in Figure 7 below:

 Figure 7

2) Start generating the .exe file, as shown in Figure 8 below:

Figure 8

3) The .exe file is successfully generated, as shown in Figure 9 below:

Figure 9

 4) View the generated .exe file

As shown in Figure 10 and Figure 11 below:

Figure 10

The generated .exe file is in the dist directory, and the .spec file is the production process file;

Figure 11

 5) Run the program

After running the program, a command line box will pop up. After the program is successfully executed, the box will disappear;

For example, the .exe program I generated here is to send an email. After the operation is completed, the result is as shown in Figure 12:

Figure 12

3.3. Command line .py->.exe file

1) Win+R enter cmd to open the command line:

Figure 12

2) Enter the generation instructions on the command line

The process is as shown in Figure 13 and Figure 14:

Figure 13

Figure 14

3) View generated files

As shown in Figure 15 and Figure 16 below:

Figure 15

Figure 16

4) Run

When running, the command line will pop up to display the results. The results here are normal. I just simply output the content and will not display it.

4. .py->.exe->Errors and solutions that may be encountered when executing .exe

4.1. ''pyinstaller'' is not recognized as an internal or external command, operable program or batch file.

Background: I opened the command line to enter the generation instructions in the directory where the .exe file is to be generated, but this error was reported.

Solution:

1) Open cmd in the directory where the .exe file is to be generated and check whether pyinstaller->pip list exists (when this error occurs, it most likely does not exist). If it does not exist, download ->pip install pyinstaller before continuing. Just encounter an error command.

The above problems were encountered and successfully solved by me, and I will add more when I encounter other problems later.

5. .exe->.py file

Note: There is a premise here, that is, the .exe file must be generated from a .py file, otherwise I don’t know what will go wrong.

The content shared by others is as follows: Click here

Or search directly: You can also view the .exe code generated by .py

6. Change .java->.exe

Although I have learned Java, I haven’t used it for a long time, so I learned from the content shared by others and recorded it for future reference. This is the article address: .java->.exe icon-default.png?t=MBR7https://blog.csdn.net/anmin8888/article /details/108039457?ops_request_misc=&request_id=&biz_id=102&utm_term=%E5%B0%86%E4%BB%A3%E7%A0%81%E7%A8%8B%E5%BA%8F%E5%8F%98% E6%88%90.exe%E6%96%87%E4%BB%B6&utm_medium=distribute.pc_search_result.none-task-blog-2~all~sobaiduweb~default-1-108039457.142^v71^control_1,201^v4^ add_ask&spm=1018.2226.3001.4187

Guess you like

Origin blog.csdn.net/qq_57663276/article/details/128676353