Home  >  Article  >  Backend Development  >  How to use Python to create exe files [suitable for beginners]

How to use Python to create exe files [suitable for beginners]

云罗郡主
云罗郡主Original
2019-01-23 14:21:594609browse

How to start an exe file in Python for beginners? exe is the extension of an executable file that can be used in the Windows environment. Below we use pyinstaller to make exe. Please take a look at the details.

How to use Python to create exe files [suitable for beginners]

Before that, we must first be familiar with Python. [Recommended tutorial: Python video tutorial]

1: What is exe?

exe is one of the file extensions in the Windows environment. It is an executable file. Although the extension is hidden under normal circumstances, we can uncheck the Explorer folder Select "Don't show registered extensions" in the advanced settings to display it.

Executable file means that it is a file that can be executed by itself, not other programs. For example, to execute it in a Python program, write as follows in the command prompt, where hello.py is the file name.

python hello.py

If you convert it to an executable file, i.e. an exe file, you can write it as follows in the command prompt or double-click it from the explorer Corresponding documents.

Use hello.exe

Two: How to make the file an exe

Use pyinstaller to execute the exe file in Python. First, let us install pyinstaller, Install from the command prompt using the following commands.

pip install pyinstaller

To make it an exe, enter the following, -Onefile and -noconsole are optional.

-Onefile and -noconsole are optional.

pyinstaller python filename [--onefile] [-noconsole]

--Onefile creates exe files by grouping related files into one. - noconsole does not display the black screen of the console (command prompt)

Three: Actually make the file exe

Try to enter and execute the following program, and save it as hello. py

从datetime导入日期时间
s =输入("请输入你的名字")
y = int(输入(f"你好,{s}!今年多大了?"))
#获取自Python诞生以来的年数(1991年)
py = datetime.now().year  -  1991
if(y == py):
  打印("嗯,这是相同的年龄!")
elif(y <py):
  打印("哇,年轻!
其他:
  打印("它比我大!
输入(“按任意键退出")

Then enter the following in the hello.py folder at the command prompt:

pyinstaller hello.py --onefile

How to use Python to create exe files [suitable for beginners]

When executed, a file named build and dist folder, there is a hello.exe file in the dist folder, which is an exepted Python file, just double-click to run it.

How to use Python to create exe files [suitable for beginners]

The above is a complete introduction to how to use Python to create exe files. If you want to know more, please pay attention to the php Chinese website.


The above is the detailed content of How to use Python to create exe files [suitable for beginners]. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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