In this tutorial, we will explore the conversion of Python scripts to Windows executable files in four simple steps. Although there are many ways to do it, we'll be covering, according to popular opinion, the simplest one so far.
This tutorial has been designed after reviewing many common errors that people face while performing this task, and hence contains detailed information to install and set up all the dependencies as well. Feel free to skip any step, if you already have those dependencies installed. Without any further ado, let's start.
Step 1: Install cURL
cURL provides a library and command line tool for transferring data using various protocols. We need it to download the pip package manager in the next step. Many of you would already have it set up, which you can check by running the following command:
$ curl --version
If the command above returns a curl version, you can skip the next instructions in this step. As for the rest of you, you can install curl by following these three steps:
- Go to https://curl.haxx.se/dlwiz/?type=bin&os=Win64&flav=-&ver=*&cpu=x86_64
- Download the curl package which matches your system's specifications (32-bit/64-bit)
- Unzip the file and go to the bin folder, you can find the curl.exe file there
However, this means that you can only use the curl command in that particular folder. In order to be able to use the curl command from anywhere on your machine, right-click on curl.exe, click on "Properties" and copy the "Location" value. After that, right-click on "My PC" and click on "Properties". In the option panel on the left, select the option "Advanced System Settings". It has been highlighted in the screenshot below.
In the window that appears, click "Environment Variables" near the bottom right. It has been highlighted in the screenshot below.
In the next window, find and double click on the user variable named "Path", then click on "New". A new text box will be created in that window; paste the "Location" value of the "curl.exe" file that you copied earlier, and then click on 'OK'.
cURL should now be accessible from anywhere in your system. Confirm your installation by running the command below:
$ curl --version
Let's go to the next step.
Step 2: Install pip
In this step, we will install pip, which is basically a package manager for Python packages. We need it in the next step to install the pyinstaller library. Most of you would already have it set up, to check run the following command:
$ pip --version
If the command above returned a pip version, you can skip the next instructions in this step.
As for the rest, you can install pip by running the following two commands in the command prompt:
$ curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
$ python get-pip.py
That's it. Pip has now been installed to your local machine! You can run the following command for confirmation:
$ pip --version
Before moving to the next step, you need to repeat what we did for curl.exe so that you can access the pip command from anywhere in your machine, but this time we'll be doing so for "pip.exe".
Hit the Windows key and search for "pip.exe", then right-click on the first search result and click on "Open File Location", it will take you to the folder in which that file is located. Right-click on the "pip.exe" file and then select "Properties". After that, copy the "Location" value and paste it in the Path variable just like we did in Step 1.
Step 3: Install PyInstaller
In this step, we'll install pyinstaller using pip. We need pyinstaller to convert our Python scripts into executable (.exe) files. You just need to copy paste the command below into your command prompt and run it:
$ pip install pyinstaller
Free eBook: Git Essentials
Check out our hands-on, practical guide to learning Git, with best-practices, industry-accepted standards, and included cheat sheet. Stop Googling Git commands and actually learn it!
Again, to confirm your installation, run the following command:
$ pyinstaller --version
Note: If you've Anaconda installed in your system, then you're probably using
conda package manager instead. In that case, run the following commands below, in sequence:
$ conda install -c conda-forge pyinstaller $ conda install -c anaconda pywin32
This step marks the end of all installations. In the next step, we'll be converting our Python files to an executable with just a single command.
Step 4: Convert Python Files to Executables
This is the last step. We'll use pyinstaller to convert our
.py files to
.exe with a single command. So, let's do it!
Open up the command prompt and navigate to the directory that your Python file/script is located in. Alternatively, you can open that directory using File Explorer, right-click + shift and then select "Open Command Prompt in this folder". Before converting your file, you should check that your file works as expected. For that purpose, I have written a basic Python script which prints the number 10 when executed.
Let's run the script and see if it works fine before converting it to an executable file. Run the following command on your command prompt:
$ python name_of_your_file.py
In my case, the filename was 'sum.py'.
To create a standalone executable file in the same directory as your Python file, run the following command:
$ pyinstaller --onefile <file_name>.py
This instruction might take some time to complete. Upon completion, it will generate three folders. You can find the executable file in the 'dist' folder. Please note that the "onefile" argument tells pyinstaller to create a single executable file only.
Let's now run our executable file to see if the procedure worked!
Ta-da! It worked just as expected.
A little tip, if your executable file closes too fast for you to notice the output, you can add an
input() line at the end of your Python file, which keeps the prompt open while waiting for using input. That is how I was able to take a screenshot of my output as well.
Also note that if your executable depends on any other executable files, like
phantomjs, you need to keep them in the same directory as your Python file's directory so that pyinstaller can include it in the executable.
In this tutorial, we discussed in detail the conversion of Python scripts to executable files using Python's pyinstaller library in four steps. We started by installing cURL, followed by pip and pyinstaller. Lastly, we converted a sample Python file to executable to ensure that the procedure works on Windows.