SoFunction
Updated on 2024-11-16

Learn two ways to package python as an exe in one article

preamble

Python can be used for web applications as well as client applications. However, client-side applications need to run py scripts, which is a problem if the user doesn't know python. Fortunately, pyton has a third-party module that can convert scripts to exe.

Some people may want to ask since it can be made into a website, why do we have to make the client, directly deployed to the server to the client can not? My answer is of course to chase the young lady ah. In the company to write some python scripts for the sister packaged into exe to reduce the workload at work. And then make some bugs, one after another will not produce a story?

A common way to package python is through pyinstaller.

pip install pyinstaller

Installation is slower above, fly fast with the clear source below.

# Tsinghua Source
pip install pyinstaller -i /simple/

step by step

pyinstaller is a command line tool, here are the detailed steps

1. cmd Switch to the directory of the python file.

2. Execute the command pyinstaller -F -w -i .

After execution, you will see that 3 folders have been created

The dist folder contains the exe file that we have packaged.

3, double-click the exe can run successfully.

Detailed parameters

In the above packing command, several parameters are used: -F, -W, -i, the meanings of these parameters are shown in the table below:

parameters usage
-F The result is an exe file where all third-party dependencies, resources and code are packaged into the exe
-D The result is a directory where various third-party dependencies, resources, and exes are stored at the same time (default)
-a Unicode support is not included
-d When the generated exe is executed, some logs will be output, which is helpful for error checking.
-w Command line window not displayed
-c Display command line window (default)
-p Specify an additional import path, similar to using python path
-i Designated Icons
-v Show version number
-n Filename of the generated .exe

pyinstaller -F -w -i means -F, the package only generates an exe file, -w, it does not open a command line window when running the program, -i means it is packaged with its own ico icon.

Graphics Window Packaging

Some people may feel the need to memorize the meaning of various parameters for command line packaging, is there a windowed way of packaging, not to mention, there is. auto-py-to-exe is a module that wraps pyinstaller into a GUI window.

pip install auto-py-to-exe -i /simple/

Once the installation is complete, type `` at the command line to open the packaging window.

There are several main parameters in the window:

  1. Script Location: the path to the python script.
  2. Onefile (--onedir / --onefile): the -D and -F parameters above, generating a single exe file or a folder.
  3. Console Window (--console / --windowed): the -w and -c parameters above, indicating whether a command line window will appear at runtime.
  4. ICON: the address of the ico icon.

After setting these parameters, the pyinstaller command is displayed in the Current Command box below. Click the last button to generate the exe file.

 

summarize

to this article on the two python packaged into an exe way to this article, more related python packaged into an exe way to search for my previous articles or continue to browse the following related articles I hope you will support me in the future more!