Use PyInstaller to convert Python scripts into executable files (exe)
When developing and distributing Python applications, packaging scripts into executable files is a common need.
PyInstaller is a popular tool that converts Python scripts into standalone, executable binaries, allowing applications to run in environments without a Python interpreter. This article will detail how to use PyInstaller for conversion.
1. Install PyInstaller
First, make sure you have the Python interpreter installed. PyInstaller can then be installed using pip:
Step 1: Check whether the pip environment is installed:
pip --version
Step 2: Install pyinstaller
pip install pyinstaller
2. Create Python script
Before you start the conversion, you first need to write the Python script you want to convert. You can write scripts according to your own needs, such as a simple “hello.py” script that outputs “Hello, World!”.
3. Use PyInstaller to convert the script
Step 1: Open our python script storage directory, enter cmd in the directory box and press Enter to enter our cmd command line interface.
Step 2: We enter the following command in the cmd command line interface to convert the py file:
pyinstaller --onefile hello.py
cmd will produce the following results:
C:\ali>pyinstaller --onefile hello.py 154 INFO: PyInstaller: 3.6 154 INFO: Python: 3.8.6 155 INFO: Platform: Windows-10-10.0.25982-SP0 156 INFO: wrote C:\ali\hello.spec 162 INFO: UPX is not available. 164 INFO: Extending PYTHONPATH with paths ['C:\ali', 'C:\ali'] 164 INFO: checking Analysis 164 INFO: Building Analysis because Analysis-00.toc is non existent 164 INFO: Initializing module dependency graph... 171 INFO: Caching module graph hooks... 184 INFO: Analyzing base_library.zip ... 2507 INFO: Processing pre-find module path hook distutils 2509 INFO: distutils: retargeting to non-venv dir 'c:\program files\python\\lib' 5674 INFO: Caching module dependency graph... 5786 INFO: running Analysis Analysis-00.toc 5804 INFO: Adding Microsoft.Windows.Common-Controls to dependent assemblies of final executable required by c:\program files\python\python.exe 5947 WARNING: lib not found: api-ms-win-core-fibers-l1-1-0.dll dependency of C:\WINDOWS\system32\ucrtbase.dll 5953 WARNING: lib not found: api-ms-win-core-kernel32-legacy-l1-1-1.dll dependency of C:\WINDOWS\system32\ucrtbase.dll 5967 WARNING: lib not found: api-ms-win-core-sysinfo-l1-2-0.dll dependency of C:\WINDOWS\system32\ucrtbase.dll 5980 WARNING: lib not found: api-ms-win-core-fibers-l1-1-1.dll dependency of C:\WINDOWS\system32\ucrtbase.dll 6127 INFO: Analyzing C:\ali\hello.py 6129 INFO: Processing module hooks... 6129 INFO: Loading module hook "hook-distutils.py"... 6133 INFO: Loading module hook "hook-encodings.py"... 6227 INFO: Loading module hook "hook-lib2to3.py"... 6237 INFO: Loading module hook "hook-pydoc.py"... 6239 INFO: Loading module hook "hook-sysconfig.py"... 6241 INFO: Loading module hook "hook-xml.etree.cElementTree.py"... 6242 INFO: Loading module hook "hook-xml.py"... 6339 INFO: Loading module hook "hook-_tkinter.py"... 6574 INFO: checking Tree 6574 INFO: Building Tree because Tree-00.toc is non existent 6574 INFO: Building Tree Tree-00.toc 6717 INFO: checking Tree 6717 INFO: Building Tree because Tree-01.toc is non existent 6718 INFO: Building Tree Tree-01.toc 6747 INFO: Looking for ctypes DLLs 6767 INFO: Analyzing run-time hooks ... 6770 INFO: Including run-time hook 'pyi_rth__tkinter.py' 6772 INFO: Including run-time hook 'pyi_rth_multiprocessing.py' 6778 INFO: Looking for dynamic libraries 7074 INFO: Looking for eggs 7074 INFO: Using Python library c:\program files\python\python38.dll 7074 INFO: Found binding redirects: [] 7077 INFO: Warnings written to C:\ali\build\hello\warn-hello.txt 7112 INFO: Graph cross-reference written to C:\ali\build\hello\xref-hello.html 7135 INFO: checking PYZ 7135 INFO: Building PYZ because PYZ-00.toc is non existent 7135 INFO: Building PYZ (ZlibArchive) C:\ali\build\hello\PYZ-00.pyz 7627 INFO: Building PYZ (ZlibArchive) C:\ali\build\hello\PYZ-00.pyz completed successfully. 7635 INFO: checking PKG 7635 INFO: Building PKG because PKG-00.toc is non existent 7635 INFO: Building PKG (CArchive) PKG-00.pkg 10023 INFO: Building PKG (CArchive) PKG-00.pkg completed successfully. 10038 INFO: Bootloader c:\program files\python\lib\site-packages\PyInstaller\bootloader\Windows-64bit\run.exe 10039 INFO: checking EXE 10039 INFO: Building EXE because EXE-00.toc is non existent 10039 INFO: Building EXE from EXE-00.toc 10041 INFO: Appending archive to EXE C:\ali\dist\hello.exe 10051 INFO: Building EXE from EXE-00.toc completed successfully.
This will generate an executable file named “hello.exe”. The original path where we store the hello.py file has the following files:
4. Run the executable file
After the conversion is completed, the generated executable file can be found in the dist folder in the same directory. Double-click to run “hello.exe” and you will see the output of “Hello, World!” in the command line or terminal window.
5. Advanced options
PyInstaller provides many advanced options for customizing the conversion process. For example, you can specify the name, icon, dependencies, etc. of the generated executable. For other content, please refer to the official documentation of PyInstaller for more details.
The following are the relevant commands of PyInstaller:
1. Install PyInstaller
Enter the following command in the terminal to install PyInstaller:
pip install pyinstaller
2. Packaging Python code
In the terminal, go to the directory where the Python code is located and enter the following command:
pyinstaller yourscript.py
Among them, yourscript.py
is the name of the Python code file you want to package.
After running this command, PyInstaller will automatically create a dist
directory, which contains the executable file and other necessary files.
3. Add additional files
If your Python code depends on other files (such as configuration files, images, etc.), you need to use the --add-data
parameter to add these files to the executable file, for example:
pyinstaller --add-data 'config.ini:.' yourscript.py
The above command will add the config.ini
file to the executable file and place it in the directory where the executable file is located.
4. Specify the output directory
By default, PyInstaller places executable files in the dist
directory. If you want to place the executable file in another directory, you can use the --distpath
parameter to specify the output directory, for example:
pyinstaller --distpath /path/to/output yourscript.py
5. Specify packaging method
PyInstaller supports multiple packaging methods, including a single file, a series of files and directories, or a ZIP package. You can use the --onefile
parameter to package Python code into a single executable file, for example:
pyinstaller --onefile yourscript.py
If you want to package Python code into a ZIP package, you can use the --onedir
parameter, for example:
pyinstaller --onedir yourscript.py
6. Other parameters
PyInstaller also supports many other parameters, such as setting icons, disabling console windows, setting Python versions, etc. You can view all available parameters through the pyinstaller --help
command.
pyinstaller --icon=youricon.ico --noconsole yourscript.py
The above command will set youricon.ico
as the executable file’s icon and disable the console window.
The above are some common commands of PyInstaller, which can be adjusted according to the actual situation. Hope this blog can help you.
6. Conclusion
Using PyInstaller to convert a Python script into an executable file is a simple and effective way to enable the application to run independently in an environment without a Python interpreter. By following the steps provided in this article, you can easily convert your own Python script into an executable file.
Hope this blog document is helpful to you! If you have any questions, please feel free to ask.