End users like Executable files in Windows. So, a developer should be able to create a Python Executable File (.exe) from the source file (.py). Let us create an exe file from a python file using a predefined module "pyinstaller" using this last minute tutorial in Windows 10, 8 an 7.
1. In the first step, you should install a PYINSTALLER.
2. In the second step, you should use the PYINSTALLER to generate a .exe file from the .py file.
1. How to install Python PYINSTALLER batteries-included Package or Module
Batteries-Included is the phrase used to indicate that it is a Python Package or Module that can be imported into our project and use right away. To install the Python PYINSTALLER, you should use the command "pip install pyinstaller" in the DOS command prompt in administrator mode like below.
C:\Python>pip install pyinstaller Defaulting to user installation because normal site-packages is not writeable Collecting pyinstaller Downloading pyinstaller-4.0.tar.gz (3.5 MB) |████████████████████████████████| 3.5 MB 1.1 MB/s Installing build dependencies ... done Getting requirements to build wheel ... done Preparing wheel metadata ... done Requirement already satisfied: setuptools in c:\program files\python39\lib\site-packages (from pyinstaller) (49.2.1) Collecting pefile>=2017.8.1; sys_platform == "win32" Downloading pefile-2019.4.18.tar.gz (62 kB) |████████████████████████████████| 62 kB 50 kB/s Collecting pyinstaller-hooks-contrib>=2020.6 Downloading pyinstaller_hooks_contrib-2020.10-py2.py3-none-any.whl (166 kB) |████████████████████████████████| 166 kB 930 kB/s Collecting altgraph Downloading altgraph-0.17-py2.py3-none-any.whl (21 kB) Collecting pywin32-ctypes>=0.2.0; sys_platform == "win32"f Downloading pywin32_ctypes-0.2.0-py2.py3-none-any.whl (28 kB) Collecting future Downloading future-0.18.2.tar.gz (829 kB) |████████████████████████████████| 829 kB 1.1 MB/s Using legacy 'setup.py install' for pefile, since package 'wheel' is not installed. Using legacy 'setup.py install' for the future, since package 'wheel' is not installed. Building wheels for collected packages: pyinstaller Building wheel for pyinstaller (PEP 517) ... done Created wheel for pyinstaller: filename=pyinstaller-4.0-py3-none-any.whl size=2789243 sha256=7363e18dfa1e4c42b284e6394f87d9adab00be775154156ca7e4a377ce9e5e5a Stored in directory: c:\users\abc\appdata\local\pip\cache\wheels\2a\50\fe\03923179863b Successfully built pyinstaller Installing collected packages: future, pefile, pyinstaller-hooks-contrib, altgraph, pywin32-ctypes, pyinstaller Running setup.py install for future ... done Running setup.py install for pefile ... done WARNING: The scripts pyi-archive_viewer.exe, pyi-bindepend.exe, pyi-grab_version.exe, pyi-makespec.exe, pyi-set_version.exe and pyinstaller.exe are installed in 'C:\Users\abc\AppData\Roaming\Python\Python39\Scripts' which is not on PATH. Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
The above installation trace shows a warning message "Defaulting to user installation because normal site-packages is not writeable". It is because of not using the DOS prompt in admin mode. So, do not forget to open DOS prompt in administrator mode when installing any module. If the usage is limited to only compiling and running python programs, admin mode is not required.
To test the successful installation of PYINSTALLER, just type pyinstaller in the command prompt like below.
C:\Python>pyinstaller usage: pyinstaller [-h] [-v] [-D] [-F] [--specpath DIR] [-n NAME] [--add-data <SRC;DEST or SRC:DEST>] [--add-binary <SRC;DEST or SRC:DEST>] [-p DIR] [--hidden-import MODULENAME] [--additional-hooks-dir HOOKSPATH] [--runtime-hook RUNTIME_HOOKS] [--exclude-module EXCLUDES] [--key KEY] [-d {all,imports,bootloader,noarchive}] [-s] [--noupx] [--upx-exclude FILE] [-c] [-w] [-i <FILE.ico or FILE.exe,ID or FILE.icns>] [--version-file FILE] [-m <FILE or XML>] [-r RESOURCE] [--uac-admin] [--uac-uiaccess] [--win-private-assemblies] [--win-no-prefer-redirects] [--osx-bundle-identifier BUNDLE_IDENTIFIER] [--runtime-tmpdir PATH] [--bootloader-ignore-signals] [--distpath DIR] [--workpath WORKPATH] [-y] [--upx-dir UPX_DIR] [-a] [--clean] [--log-level LEVEL] scriptname [scriptname ...] pyinstaller: error: the following arguments are required: scriptname C:\Python>
If the pyinstaller command does not work, it means that it has been installed in a directory outside "C:\Program Files\Python39\Scripts". The scripts should contain a file pyinstaller.exe.
This script path is added to the Windows Environment Variables at the time of installing Python software. This is the reason for the availability of python and pyinstaller commands globally from any directory.
Alternatively (the second way), you can install the PYINSTALLER package from the Python Shell window.
Let us move to using pyinstaller to generate an exe file from .py file.
2. How to Create Python Executable File .exe from .py file
Now, you know that it is the PYINSTALLER that converts the python source file in .py format to the .exe format. Let us use the pyinstaller command to create an executable file.
C:\Python>pyinstaller hello.py 44 INFO: PyInstaller: 4.0 44 INFO: Python: 3.9.0 44 INFO: Platform: Windows-10-10.0.19041-SP0 44 INFO: wrote C:\Python\hello.spec 44 INFO: UPX is not available. 44 INFO: Extending PYTHONPATH with paths ['C:\\Python', 'C:\\Python'] 44 INFO: checking Analysis 44 INFO: Building Analysis because Analysis-00.toc is non existent 44 INFO: Initializing module dependency graph... 44 INFO: Caching module graph hooks... 60 INFO: Analyzing base_library.zip ... 1743 INFO: Processing pre-find module path hook distutils from 'c:\\program files\\python39\\lib\\site-packages\\PyInstaller\\hooks\\pre_find_module_path\\hook-distutils.py'. 1759 INFO: distutils: retargeting to non-venv dir 'c:\\program files\\python39\\lib' 3097 INFO: Caching module dependency graph... 3181 INFO: running Analysis Analysis-00.toc 3183 INFO: Adding Microsoft.Windows.Common-Controls to dependent assemblies of final executable required by c:\program files\python39\python.exe 3227 WARNING: lib not found: api-ms-win-core-path-l1-1-0.dll dependency of c:\program files\python39\python39.dll 3378 INFO: Analyzing C:\Python\hello.py 3378 INFO: Processing module hooks... 3378 INFO: Loading module hook 'hook-distutils.py' from 'c:\\program files\\python39\\lib\\site-packages\\PyInstaller\\hooks'... 3378 INFO: Loading module hook 'hook-encodings.py' from 'c:\\program files\\python39\\lib\\site-packages\\PyInstaller\\hooks'... 3426 INFO: Loading module hook 'hook-lib2to3.py' from 'c:\\program files\\python39\\lib\\site-packages\\PyInstaller\\hooks'... 3517 INFO: Loading module hook 'hook-sysconfig.py' from 'c:\\program files\\python39\\lib\\site-packages\\PyInstaller\\hooks'... 3518 INFO: Loading module hook 'hook-xml.etree.cElementTree.py' from 'c:\\program files\\python39\\lib\\site-packages\\PyInstaller\\hooks'... 3519 INFO: Loading module hook 'hook-xml.py' from 'c:\\program files\\python39\\lib\\site-packages\\PyInstaller\\hooks'... 3542 INFO: Loading module hook 'hook-_tkinter.py' from 'c:\\program files\\python39\\lib\\site-packages\\PyInstaller\\hooks'... 3631 INFO: checking Tree 3631 INFO: Building Tree because Tree-00.toc is non existent 3632 INFO: Building Tree Tree-00.toc 3673 INFO: checking Tree 3673 INFO: Building Tree because Tree-01.toc is non existent 3673 INFO: Building Tree Tree-01.toc 3692 INFO: Looking for ctypes DLLs 3710 INFO: Analyzing run-time hooks ... 3712 INFO: Including run-time hook 'c:\\program files\\python39\\lib\\site-packages\\PyInstaller\\hooks\\rthooks\\pyi_rth__tkinter.py' 3713 INFO: Including run-time hook 'c:\\program files\\python39\\lib\\site-packages\\PyInstaller\\hooks\\rthooks\\pyi_rth_multiprocessing.py' 3718 INFO: Looking for dynamic libraries 3855 INFO: Looking for eggs 3855 INFO: Using Python library c:\program files\python39\python39.dll 3855 INFO: Found binding redirects: [] 3855 INFO: Warnings written to C:\Python\build\hello\warn-hello.txt 3895 INFO: Graph cross-reference written to C:\Python\build\hello\xref-hello.html 3905 INFO: checking PYZ 3905 INFO: Building PYZ because PYZ-00.toc is non existent 3905 INFO: Building PYZ (ZlibArchive) C:\Python\build\hello\PYZ-00.pyz 4299 INFO: Building PYZ (ZlibArchive) C:\Python\build\hello\PYZ-00.pyz completed successfully. 4299 INFO: checking PKG 4299 INFO: Building PKG because PKG-00.toc is non existent 4314 INFO: Building PKG (CArchive) PKG-00.pkg 4324 INFO: Building PKG (CArchive) PKG-00.pkg completed successfully. 4324 INFO: Bootloader c:\program files\python39\lib\site-packages\PyInstaller\bootloader\Windows-64bit\run.exe 4324 INFO: checking EXE 4325 INFO: Building EXE because EXE-00.toc is non existent 4325 INFO: Building EXE from EXE-00.toc 4325 INFO: Appending archive to EXE C:\Python\build\hello\hello.exe 4327 INFO: Building EXE from EXE-00.toc completed successfully. 4330 INFO: checking COLLECT 4331 INFO: Building COLLECT because COLLECT-00.toc is non existent 4331 INFO: Building COLLECT COLLECT-00.toc 5340 INFO: Building COLLECT COLLECT-00.toc completed successfully. C:\Python>cd dist C:\Python\dist>cd hello C:\Python\dist\hello>hello Sum = 20 . Thats it.!! C:\Python\dist\hello>
You can observe that "pyinstaller" command created two directories namely "dist" and "build" in the current path. In each folder, it creates a new folder with the same name as that of the Python-Program-Name. So, you should distribute folder with the name "dist/Python-Program-Name". It contains an executable file with the same name as "Python-Program-Name". This is the file to be opened by the end-user.
The end-user can either double click the exe file or type the name of the exe file in the command prompt to run the distributed python program.
The size of the python distributable folder will be in MegaBytes as it contains DLL, PYD, H, TCL and other files.
The PYINSTALLER is intelligent enough to pack all third-party libraries used by your Python program to create an executable file. This is how to convert python py files to .exe in Windows 10, 8 and 7.
Share this Last Minute Python tutorial with your friends and colleagues to encourage authors.