Exe a python application with PyInstaller
overview
If you want other people to run scripts written in Python, you need to install the Python environment. However, if you can wrap your Python script in a single file and convert it to an executable (.exe file), you don’t need to install a Python environment. So, I’ll use PyInstaller to show you how to pack a Python script into a single file and turn it into an executable.
version check
python: v3.11.2
pyinstaller: v5.8.0
convert script
pyinstaller --onefile your_script.py
Here, replace your_script.py with the filename of the Python script you want to convert.
This command packs your_script.py into a single executable and outputs it to the dist directory.
Run executable
Finally, run the generated executable in /dist. Executables do not require a Python execution environment and can be easily run by others.
That’s all there is to putting a Python script into a single file and turning it into an executable. By using PyInstaller, you can save yourself the trouble of installing a Python environment when sharing your Python scripts with others. Please try.
Manageable project structure (bonus)
The above method works well for exe conversion, but it is inconvenient when sharing with other project members. So write npm scripts to be more concise.
- Install node.js to use the supplemental npm scripts
Create requipments for version control
//requipments.txt
pyinstaller >= 5.8.0
{
"name": "pyinstaller",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"install": "pip install -r requirements.txt",
"build": "pyinstaller --onefile main.py"
},
"author": "",
"license": "ISC"
}
After creating the following files, install pyinstaller using npm scripts.
npm run install
Once the installation is complete, use npm scripts to create an Exe.
npm run build
Please see below for the project https://github.com/wiblok/python/tree/main/pyInstaller