Home > Backend Development > Python Tutorial > Learn this trick and it really works! Copy and paste to quickly package Python programs into exe!

Learn this trick and it really works! Copy and paste to quickly package Python programs into exe!

WBOY
Release: 2023-04-15 18:10:03
forward
1537 people have browsed it

Learn this trick and it really works! Copy and paste to quickly package Python programs into exe!

Hello everyone, I am a rookie.

In order to send Python programs to colleagues and friends who do not understand code and do not have Python installed, the best way is to package the Python program into an exe executable file and then send it to them.

I have packaged several times before, and the operation is not difficult, but I cannot remember the packaging commands. I need to check the commands again every time I package. Therefore, this article records the packaging process. When you need to package, you can directly copy and paste it and complete it quickly without using your brain.

Preparation

1. Install pyinstaller

pip install pyinstaller
Copy after login

2. Prepare the icon

The icon uses an ico icon of 48*48 size (can be adjusted as needed) It is more appropriate. You can first prepare the image to be used as an icon, find an online ico icon conversion website, convert it into an ico icon, and download it for use.

There are many tools for converting ico icons online. Just search online to find one that works. Public accounts cannot put external links. When you get the tools for this article below, you can get the link to the tool I used.

3. Prepare the packaging file

Create a folder and put the Python program that needs to be packaged and its dependent files and icons into this folder.

Execute packaging

1. Execute the packaging command

Open the command line in the folder and execute the packaging command.

pyinstaller -F -w -i *.ico -n xxx *.py
Copy after login

*.ico is the name of the icon file prepared earlier, *.py is the name of the Python program that needs to be packaged, and xxx after the -n parameter specifies the exe file name of the packaging result.

This article takes the code for drawing a little tiger at the beginning of the year as an example. Copy the code draw_tiger.py and the icon tiger.ico to the newly created folder, open the command line and execute the packaging command:

pyinstaller - F -w -i tiger.ico -n tiger draw_tiger.py

Wait for the command execution to complete, and the packaging is completed.

2. Run the packaging program

After the packaging is completed, three folders and a file ending with .spec will be generated in the current folder. Double-click to open the .exe file in the dist directory. You can run the program (if the Python program has dependent files and they are not automatically placed in the dist directory, you must copy the dependent files into it first). You can also create a desktop shortcut for the .exe file, and then open and run it directly on the desktop, which is more convenient.

I also uploaded the .exe file packaged this time to Baidu Netdisk, which can be obtained by replying "Tiger Tiger Shengwei" in the background of the official account.

Learn this trick and it really works! Copy and paste to quickly package Python programs into exe!

This .exe file can be sent to colleagues and friends, and can be run directly on their computers.

If there are dependent files such as called files or pictures in the Python program, just put them in the same folder as the .exe file and send them to colleagues and friends.

Packaging parameters and result description

Packaging parameters

  • -F Capital F, create an .exe executable file, all third-party dependencies and codes are Package it into the .exe file.
  • -w does not display the command line window.
  • -i specifies the icon of the .exe file.
  • -n Specifies the file name of the .exe file. If the file name is not specified, the file name of the Python program will be used by default.

If you need to view all parameters of pyinstaller, you can execute pyinstaller --help on the command line to view it.

pyinstaller --help
Copy after login

Packaging results

  • __pycache__: stores the binary .pyc file of the Python program. The .pyc file is a binary file compiled by the Python program (suffix .py), which can improve loading Speed, and hiding source code.
  • build: Stores temporary files generated during the packaging process.
  • dist: Stores the packaged .exe executable file.
  • .spec: Packaged configuration file (the packaged program can be configured from this file, but it is generally not needed).

The above is an introduction to packaging Python programs into exe. Bookmark this article to facilitate future copying. Welcome to like, read and share.

The above is the detailed content of Learn this trick and it really works! Copy and paste to quickly package Python programs into exe!. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:51cto.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template