This article is participating in “Python Theme Month”
Today, a friend in the group asked how to compile Python code into EXE file, just have time, now write.
Or the other computer does not have Python, directly compiled into an EXE file, it is very convenient.
The school committee has made a short video here to see the whole process.
Click on this link to see the video: C station companion food: Python code compilation published as an executable file [nanny-level operation guide] (like the past to give a three consecutive duck, don’t next time certain! B needs a lot of encouragement, thanks!
Video cover: (nuggets don’t support B station video!!)
Ok, let’s see how we can do that.
Lei School committee found this artifact:PyInstaller
It’s like a free translator, you see, translating Python code directly into exe files!
This is the official website of the description, it seems quite 6!
How convenient: Install, and then compile the Python program into an EXE executable.
Make sure PIP is installed
See Quick Install and Master PIP
Install PyInstaller using PIP
Execute the following code:
pip install pyinstaller
Copy the code
The effect is as follows:
For example, the student committee wants to compile and package the Python program demo.py
The program code of demo.py is as follows:
import time
# Very churro cycle, do nothing but sleep for 3 seconds
# then print!
Lei school committee advised friends to do technology do not learn this procedure, ha ha ha
while True:
time.sleep(3)
print("waiting ..."Run the following code to translate the Python code to exe bash PyInstaller demo.pyCopy the code
The above picture shows the whole process. Line 13462 indicates that the exe file is compiled and stored in the build/demo directory of the current directory. The exe file is named demo.
Go to the Build/Demo directory and double-click the Demo file to run it. (Since the blogger is using a MAC and doesn’t have the software running Windows installed, it doesn’t work.)
But the official documentation says that the final result of the compilation is in the dist folder, so let’s see.
Good, right execution!
The original demo. Py script is only 4k, and the entire dist folder is 16M (it can be packaged and sent to others for execution).
(After all, PyInstaller is not directly translated into machine code, but is parsed by the Cpython parser, which does not require Python to be installed on the other computer and is much smaller than installing Python.)
Here is a partial cpython library display for the dist/demo directory:
Such a detailed step, to a three consecutive is not too much!
By the way, the school committee also has this can pay attention to long-term reading => lei School committee interesting programming story compilation
Continuous learning and continuous development, I am the Lei School committee! Programming is fun, but the key is to get the technology straight. Welcome to follow, and feel free to like the collection!