How to compile a Python program into an executable file? This article will tell you the steps about how to convert a python program (.py
files) to an executable file on both Windows, Linux, macOS operating systems.
1. Compile python program to windows .exe file steps.
- Using Pycharm to install
pyinstaller
. - You can also install the
pyinstaller
library with the commandpip install pyinstaller
.$ pip install pyinstaller Collecting pyinstaller Downloading pyinstaller-4.2.tar.gz (3.6 MB) |████████████████████████████████| 3.6 MB 26 kB/s Installing build dependencies ... done Getting requirements to build wheel ... done Preparing wheel metadata ... done Collecting importlib-metadata Downloading importlib_metadata-3.8.0-py3-none-any.whl (13 kB) Collecting pyinstaller-hooks-contrib>=2020.6 Downloading pyinstaller_hooks_contrib-2021.1-py2.py3-none-any.whl (181 kB) |████████████████████████████████| 181 kB 22 kB/s Collecting macholib>=1.8 Downloading macholib-1.14-py2.py3-none-any.whl (37 kB) Requirement already satisfied: setuptools in /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages (from pyinstaller) (40.8.0) Collecting altgraph Downloading altgraph-0.17-py2.py3-none-any.whl (21 kB) Collecting zipp>=0.5 Downloading zipp-3.4.1-py3-none-any.whl (5.2 kB) Requirement already satisfied: typing-extensions>=3.6.4 in /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages (from importlib-metadata->pyinstaller) (3.7.4.3) Building wheels for collected packages: pyinstaller Building wheel for pyinstaller (PEP 517) ... done Created wheel for pyinstaller: filename=pyinstaller-4.2-py3-none-any.whl size=2413076 sha256=7a1f646083dbb948c03111bce2271a3766ea746e8fbebf351e4593ff66ae55bb Stored in directory: /Users/songzhao/Library/Caches/pip/wheels/85/03/fe/8402ecc0126d8b6156708a4d0b638f2bb6b082d10da4fde9f8 Successfully built pyinstaller Installing collected packages: zipp, altgraph, pyinstaller-hooks-contrib, macholib, importlib-metadata, pyinstaller Successfully installed altgraph-0.17 importlib-metadata-3.8.0 macholib-1.14 pyinstaller-4.2 pyinstaller-hooks-contrib-2021.1 zipp-3.4.1
- Find the installation directory of
pyinstaller
.$ pip show pyinstaller Name: pyinstaller Version: 4.2 Summary: PyInstaller bundles a Python application and all its dependencies into a single package. Home-page: http://www.pyinstaller.org/ Author: Hartmut Goebel, Giovanni Bajo, David Vierra, David Cortesi, Martin Zibricky Author-email: None License: GPLv2-or-later with a special exception which allows to use PyInstaller to build and distribute non-free programs (including commercial ones) Location: /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages Requires: importlib-metadata, setuptools, pyinstaller-hooks-contrib, macholib, altgraph Required-by:
- Open a terminal and go to your python script file saved folder then run the command
pyinstaller -F *.py
.$ pyinstaller -F *.py 70 INFO: PyInstaller: 4.2 71 INFO: Python: 3.7.3 87 INFO: Platform: Darwin-19.6.0-x86_64-i386-64bit 88 INFO: wrote /Users/songzhao/Documents/WorkSpace/dev2qa.com-example-code/PythonExampleProject/com/dev2qa/example/lxml/ParseHtml.spec 98 INFO: UPX is not available. 101 INFO: Extending PYTHONPATH with paths ['/Users/songzhao/Documents/WorkSpace/dev2qa.com-example-code/PythonExampleProject', '/Users/songzhao/Documents/WorkSpace/dev2qa.com-example-code/PythonExampleProject', '/Users/songzhao/Documents/WorkSpace/dev2qa.com-example-code/PythonExampleProject/com/dev2qa/example/lxml'] 121 INFO: checking Analysis 121 INFO: Building Analysis because Analysis-00.toc is non existent 121 INFO: Initializing module dependency graph... 125 INFO: Caching module graph hooks... 134 WARNING: Several hooks defined for module 'win32ctypes.core'. Please take care they do not conflict. 137 INFO: Analyzing base_library.zip ... 4597 INFO: Caching module dependency graph... 4733 INFO: running Analysis Analysis-00.toc 4744 INFO: Analyzing /Users/songzhao/Documents/WorkSpace/dev2qa.com-example-code/PythonExampleProject/com/dev2qa/example/lxml/ParseHtml.py 5013 INFO: Processing pre-safe import module hook urllib3.packages.six.moves from '/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/PyInstaller/hooks/pre_safe_import_module/hook-urllib3.packages.six.moves.py'. 6974 INFO: Processing pre-safe import module hook six.moves from '/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/PyInstaller/hooks/pre_safe_import_module/hook-six.moves.py'. 8701 INFO: Analyzing /Users/songzhao/Documents/WorkSpace/dev2qa.com-example-code/PythonExampleProject/com/dev2qa/example/lxml/__init__.py 8702 INFO: Processing module hooks... 8702 INFO: Loading module hook 'hook-cryptography.py' from '/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/_pyinstaller_hooks_contrib/hooks/stdhooks'... 9033 INFO: Loading module hook 'hook-certifi.py' from '/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/_pyinstaller_hooks_contrib/hooks/stdhooks'... 9040 INFO: Loading module hook 'hook-lxml.py' from '/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/_pyinstaller_hooks_contrib/hooks/stdhooks'... 9573 INFO: Loading module hook 'hook-lxml.etree.py' from '/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/_pyinstaller_hooks_contrib/hooks/stdhooks'... 9575 INFO: Loading module hook 'hook-encodings.py' from '/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/PyInstaller/hooks'... 9661 INFO: Loading module hook 'hook-pickle.py' from '/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/PyInstaller/hooks'... 9664 INFO: Excluding import of argparse from module pickle 9665 INFO: Loading module hook 'hook-heapq.py' from '/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/PyInstaller/hooks'... 9667 INFO: Excluding import of doctest from module heapq 9667 INFO: Loading module hook 'hook-difflib.py' from '/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/PyInstaller/hooks'... 9669 INFO: Excluding import of doctest from module difflib 9669 INFO: Loading module hook 'hook-xml.py' from '/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/PyInstaller/hooks'... 9761 INFO: Loading module hook 'hook-lxml.objectify.py' from '/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/_pyinstaller_hooks_contrib/hooks/stdhooks'... 9762 INFO: Loading module hook 'hook-lxml.isoschematron.py' from '/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/_pyinstaller_hooks_contrib/hooks/stdhooks'... 9808 INFO: Looking for ctypes DLLs 9808 INFO: Analyzing run-time hooks ... 9814 INFO: Including run-time hook '/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/_pyinstaller_hooks_contrib/hooks/rthooks/pyi_rth_certifi.py' 9828 INFO: Looking for dynamic libraries 10082 INFO: Looking for eggs 10082 INFO: Using Python library /Library/Frameworks/Python.framework/Versions/3.7/Python 10088 INFO: Warnings written to /Users/songzhao/Documents/WorkSpace/dev2qa.com-example-code/PythonExampleProject/com/dev2qa/example/lxml/build/ParseHtml/warn-ParseHtml.txt 10152 INFO: Graph cross-reference written to /Users/songzhao/Documents/WorkSpace/dev2qa.com-example-code/PythonExampleProject/com/dev2qa/example/lxml/build/ParseHtml/xref-ParseHtml.html 10197 INFO: checking PYZ 10197 INFO: Building PYZ because PYZ-00.toc is non existent 10197 INFO: Building PYZ (ZlibArchive) /Users/songzhao/Documents/WorkSpace/dev2qa.com-example-code/PythonExampleProject/com/dev2qa/example/lxml/build/ParseHtml/PYZ-00.pyz 11092 INFO: Building PYZ (ZlibArchive) /Users/songzhao/Documents/WorkSpace/dev2qa.com-example-code/PythonExampleProject/com/dev2qa/example/lxml/build/ParseHtml/PYZ-00.pyz completed successfully. 11111 INFO: checking PKG 11112 INFO: Building PKG because PKG-00.toc is non existent 11112 INFO: Building PKG (CArchive) PKG-00.pkg 17846 INFO: Building PKG (CArchive) PKG-00.pkg completed successfully. 17852 INFO: Bootloader /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/PyInstaller/bootloader/Darwin-64bit/run 17852 INFO: checking EXE 17852 INFO: Building EXE because EXE-00.toc is non existent 17852 INFO: Building EXE from EXE-00.toc 17852 INFO: Appending archive to EXE /Users/songzhao/Documents/WorkSpace/dev2qa.com-example-code/PythonExampleProject/com/dev2qa/example/lxml/dist/ParseHtml 17918 INFO: Fixing EXE for code signing /Users/songzhao/Documents/WorkSpace/dev2qa.com-example-code/PythonExampleProject/com/dev2qa/example/lxml/dist/ParseHtml 17922 INFO: Building EXE from EXE-00.toc completed successfully. (base) bogon:lxml songzhao$ pwd /Users/songzhao/Documents/WorkSpace/dev2qa.com-example-code/PythonExampleProject/com/dev2qa/example/lxml
- Then it will create the executable file in the current folder’s
dist
subfolder.$ tree ./ ./ ├── ParseHtml.py ├── ParseHtml.spec ├── __init__.py ├── __pycache__ │ └── ParseHtml.cpython-37.pyc ├── build │ └── ParseHtml │ ├── Analysis-00.toc │ ├── EXE-00.toc │ ├── PKG-00.pkg │ ├── PKG-00.toc │ ├── PYZ-00.pyz │ ├── PYZ-00.toc │ ├── base_library.zip │ ├── warn-ParseHtml.txt │ └── xref-ParseHtml.html └── dist └── ParseHtml
- Go to the
dist
folder, if your OS is macOS or Linux, you will find a file with executable rights for all users.$ cd dist $ ls -l -rwxr-xr-x 1 songzhao staff 12361616 Mar 27 22:54 ParseHtml
- If your OS is Windows, the executable file is a file with the
.exe
file extension. - Now you can execute the python program by input the executable file name in a terminal.