Convert Python Script to Compiler Executable (.exe File)


Can I run Python as an executable? Yes. In this tutorial, I will show you how easily you can convert your Python code (for example calculator app code) or script into a compiler executable or .exe file using Pyinstaller library.

Why to make Python Compiler Executable

Let’s say you developed a Python code and want to deliver it to your client. Now to run that code, your client needs to have Python installed in his system and he should know basics of Python like how to install required libraries, run a Python script, etc.

This is the reason you should always convert or compile your Python code into an executable (in short exe) file before delivering it to your client. So that your client no need to do anything. Like other software, he can just double-click that .exe file, and your code will run.

How to Compile a Python Code?

It is so simple to convert or compile a Python code into an executable (.exe) file if you are using Anaconda. For the purpose of better explanation, I will break the entire process into some simple steps.

Install Anaconda

I use Anaconda for my day-to-day data analysis work. While making this tutorial I also had Anaconda installed in my system. So everything I will show in this tutorial will definitely work in Anaconda for sure.

If you are using only Python it may work or you may get a compiler error, I am not sure about that. Please tell me in the comment section if you are getting any errors in Python.

I think I do not need to guide you on how to install Anaconda. You just need to download Anaconda and double-click to install it. One important thing, while installing Anaconda make sure you check the Add Anaconda to my PATH environment variable.


Create a Conda Environment

It is always a good practice to create a separate conda environment for your new project to manage dependencies and avoid conflicts with other projects. You can create an environment using the following command:

conda create -n ENV_NAME python=3.8

Here ENV_NAME is the virtual environment name. You can change this name at your convenience. I have tested it in this Python 3.8 and it works fine for me. I hope it will work for other Python versions.

Also Read:  Generate Synthetic Text Data with Faker in Python

Once you create the environment, just activate it using below command:

activate ENV_NAME

Install Required Package

There are various tools and libraries available to make a Python compiler executable, some of them are: py2exe, freeze, cython, etc. In this tutorial, I am going to use Pyinstaller library for compiling Python code into an exe file. You can install it with conda:

conda install pyinstaller

Compile Your Python Script

Now to compile your script to a binary executable, you need to have a Python code. For this tutorial, I am going to use the Calculator Windows Application code. You can find this full code here: How to create a GUI calculator in Python using Tkinter.

Create a .py file and paste this calculator code (or you can use your own code instead of this). In my case, I placed this .py file inside this directory: D:\Python.

Now you need to navigate to that directory from cmd using below command:

cd D:\Python

Now finally, you just need to run the below command to create your Python compiler executable file. Replace with the name of your Python script.

pyinstaller --onefile

Below is the entire process command log for your reference:

(ENV_NAME) C:\Users\Anindya>cd D:\Python

(ENV_NAME) C:\Users\Anindya>d:

(ENV_NAME) D:\Python>pyinstaller --onefile
634 INFO: PyInstaller: 5.6.2
634 INFO: Python: 3.8.13 (conda)
662 INFO: Platform: Windows-10-10.0.19045-SP0
662 INFO: wrote D:\Python\calculator_code.spec
662 INFO: UPX is not available.
1659 INFO: Extending PYTHONPATH with paths
2230 INFO: checking Analysis
2230 INFO: Building Analysis because Analysis-00.toc is non existent
2230 INFO: Initializing module dependency graph...
2230 INFO: Caching module graph hooks...
2245 INFO: Analyzing ...
4539 INFO: Loading module hook '' from 'C:\\Users\\Anindya\\Anaconda3\\envs\\ENV_NAME\\lib\\site-packages\\PyInstaller\\hooks'...
4783 INFO: Loading module hook '' from 'C:\\Users\\Anindya\\Anaconda3\\envs\\ENV_NAME\\lib\\site-packages\\PyInstaller\\hooks'...
6293 INFO: Loading module hook '' from 'C:\\Users\\Anindya\\Anaconda3\\envs\\ENV_NAME\\lib\\site-packages\\PyInstaller\\hooks'...
7465 INFO: Caching module dependency graph...
7637 INFO: running Analysis Analysis-00.toc
7637 INFO: Adding Microsoft.Windows.Common-Controls to dependent assemblies of final executable
  required by C:\Users\Anindya\Anaconda3\envs\ENV_NAME\python.exe
7913 INFO: Analyzing D:\Python\
8013 INFO: Processing module hooks...
8029 INFO: Loading module hook '' from 'C:\\Users\\Anindya\\Anaconda3\\envs\\ENV_NAME\\lib\\site-packages\\PyInstaller\\hooks'...
8167 INFO: checking Tree
8167 INFO: Building Tree because Tree-00.toc is non existent
8167 INFO: Building Tree Tree-00.toc
8283 INFO: checking Tree
8283 INFO: Building Tree because Tree-01.toc is non existent
8283 INFO: Building Tree Tree-01.toc
8367 INFO: checking Tree
8367 INFO: Building Tree because Tree-02.toc is non existent
8367 INFO: Building Tree Tree-02.toc
8383 INFO: Looking for ctypes DLLs
8383 INFO: Analyzing run-time hooks ...
8383 INFO: Including run-time hook 'C:\\Users\\Anindya\\Anaconda3\\envs\\ENV_NAME\\lib\\site-packages\\PyInstaller\\hooks\\rthooks\\'
8383 INFO: Including run-time hook 'C:\\Users\\Anindya\\Anaconda3\\envs\\ENV_NAME\\lib\\site-packages\\PyInstaller\\hooks\\rthooks\\'
8383 INFO: Looking for dynamic libraries
432 INFO: Extra DLL search directories (AddDllDirectory): []
432 INFO: Extra DLL search directories (PATH): ['C:\\Users\\Anindya\\Anaconda3\\envs\\ENV_NAME', 'C:\\Users\\Anindya\\Anaconda3\\envs\\ENV_NAME\\Library\\mingw-w64\\bin', 'C:\\Users\\Anindya\\Anaconda3\\envs\\ENV_NAME\\Library\\usr\\bin', 'C:\\Users\\Anindya\\Anaconda3\\envs\\ENV_NAME\\Library\\bin', 'C:\\Users\\Anindya\\Anaconda3\\envs\\ENV_NAME\\Scripts', 'C:\\Program Files (x86)\\Intel\\Intel(R) Management Engine Components\\iCLS\\', 'C:\\Program Files\\Intel\\Intel(R) Management Engine Components\\iCLS\\', 'C:\\Windows\\system32', 'C:\\Windows', 'C:\\Windows\\System32\\Wbem', 'C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\', 'C:\\Windows\\System32\\OpenSSH\\', 'C:\\Program Files (x86)\\NVIDIA Corporation\\PhysX\\Common', 'C:\\Program Files\\Intel\\WiFi\\bin\\', 'C:\\Program Files\\Common Files\\Intel\\WirelessCommon\\', 'C:\\Program Files (x86)\\Intel\\Intel(R) Management Engine Components\\DAL', 'C:\\Program Files\\Intel\\Intel(R) Management Engine Components\\DAL', 'C:\\Program Files (x86)\\Intel\\Intel(R) Management Engine Components\\IPT', 'C:\\Program Files\\Intel\\Intel(R) Management Engine Components\\IPT', '%SystemRoot%\\system32', '%SystemRoot%', '%SystemRoot%\\System32\\Wbem', '%SYSTEMROOT%\\System32\\WindowsPowerShell\\v1.0\\', '%SYSTEMROOT%\\System32\\OpenSSH\\', 'C:\\Users\\Anindya\\Anaconda3', 'C:\\Users\\Anindya\\Anaconda3\\Library\\mingw-w64\\bin', 'C:\\Users\\Anindya\\Anaconda3\\Library\\usr\\bin', 'C:\\Users\\Anindya\\Anaconda3\\Library\\bin', 'C:\\Users\\Anindya\\Anaconda3\\Scripts', 'C:\\Users\\Anindya\\AppData\\Local\\Microsoft\\WindowsApps']
9507 INFO: Looking for eggs
9507 INFO: Using Python library C:\Users\Anindya\Anaconda3\envs\ENV_NAME\python38.dll
9507 INFO: Found binding redirects:
9670 INFO: Warnings written to D:\Python\build\calculator_code\warn-calculator_code.txt
9739 INFO: Graph cross-reference written to D:\Python\build\calculator_code\xref-calculator_code.html
9771 INFO: checking PYZ
9771 INFO: Building PYZ because PYZ-00.toc is non existent
9771 INFO: Building PYZ (ZlibArchive) D:\Python\build\calculator_code\PYZ-00.pyz
10151 INFO: Building PYZ (ZlibArchive) D:\Python\build\calculator_code\PYZ-00.pyz completed successfully.
10167 INFO: checking PKG
10167 INFO: Building PKG because PKG-00.toc is non existent
10167 INFO: Building PKG (CArchive) calculator_code.pkg
13359 INFO: Building PKG (CArchive) calculator_code.pkg completed successfully.
13375 INFO: Bootloader C:\Users\Anindya\Anaconda3\envs\ENV_NAME\lib\site-packages\PyInstaller\bootloader\Windows-64bit\run.exe
13375 INFO: checking EXE
13375 INFO: Building EXE because EXE-00.toc is non existent
13375 INFO: Building EXE from EXE-00.toc
13375 INFO: Copying bootloader EXE to D:\Python\dist\calculator_code.exe.notanexecutable
13406 INFO: Copying icon to EXE
13406 INFO: Copying icons from ['C:\\Users\\Anindya\\Anaconda3\\envs\\ENV_NAME\\lib\\site-packages\\PyInstaller\\bootloader\\images\\icon-console.ico']
13406 INFO: Writing RT_GROUP_ICON 0 resource with 104 bytes
13413 INFO: Writing RT_ICON 1 resource with 3752 bytes
13413 INFO: Writing RT_ICON 2 resource with 2216 bytes
13413 INFO: Writing RT_ICON 3 resource with 1384 bytes
13413 INFO: Writing RT_ICON 4 resource with 37019 bytes
13413 INFO: Writing RT_ICON 5 resource with 9640 bytes
13413 INFO: Writing RT_ICON 6 resource with 4264 bytes
13413 INFO: Writing RT_ICON 7 resource with 1128 bytes
13413 INFO: Copying 0 resources to EXE
13413 INFO: Embedding manifest in EXE
13413 INFO: Updating manifest in D:\Python\dist\calculator_code.exe.notanexecutable
13413 INFO: Updating resource type 24 name 1 language 0
13413 INFO: Appending PKG archive to EXE
13413 INFO: Fixing EXE headers
13544 INFO: Building EXE from EXE-00.toc completed successfully.

Where is Python Executable file?

Once pyinstaller completes the compilation process, you can find the standalone EXE inside dist directory within the directory where your script is located. So your folder structure should looks like below:

Woking directory
    ├── build
    ├── dist
         └── calculator_code.exe

This calculator_code.exe is your final Python compiler executable file. The name of this exe file will be same as your .py file.

Also Read:  Python Find in List: Complete Guide

Some Points

There are various libraries available to make a Python compiler executable, like: py2exe, freeze, cython, etc. But I prefer to use pyinstaller among them. Because it is easy to use and a simple command.

Along with that, when you use this library to ‘compile’ code on Windows, it produces an .exe file. It’s important to note that you cannot create a Windows executable (.exe) on a Linux system, nor can you generate a Linux executable on Windows. Each platform requires its own specific executable. (However, you can use Wine to find a workaround for this limitation in Linux.)

This is it for this tutorial. If you new to Python then you can check this Udemy course: Learn Python in 100 days of coding. After that, if you want to learn data science then this Udemy course is for you: Machine Learning A-Z: AI, Python & R + ChatGPT Bonus.

To learn advanced machine learning, You should definitely take this course: Machine Learning Specialization – Andrew Ng.

Leave a Comment

Your email address will not be published. Required fields are marked *