You are on page 1of 12

Converting Python Scripts

to Executable Files
By Guest Contributor • February 21, 2019 •
1 Comment (/converting-python-scripts-to-executable-�les/#disqus_thread

Introduction
In this tutorial, we will explore the conversion of Python scripts to
Windows executable �les 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 (https://curl.haxx.se/) provides a library and command line tool
for transferring data using various protocols. We need it to download
the pip package manager (https://pip.pypa.io/en/stable/) 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: 
1. Go to https://curl.haxx.se/dlwiz/?type=bin&os=Win64&�av=-&
ver=*&cpu=x86_64 (https://curl.haxx.se/dlwiz/?type=bin&
os=Win64&�av=-&ver=*&cpu=x86_64)
2. Download the curl package which matches your system's
speci�cations (32-bit/64-bit)
3. Unzip the �le and go to the bin folder, you can �nd the curl.exe
�le 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, �nd 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" �le that you
copied earlier, and then click on 'OK'.

cURL should now be accessible from anywhere in your system.


Con�rm 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 (https://www.pyinstaller.org/). 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 con�rmation:

$ 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
�rst search result and click on "Open File Location", it will take you to
the folder in which that �le is located. Right-click on the "pip.exe" �le
and then select "Properties". After that, copy the "Location" value and
paste it in the Path variable just like we did in Step 1.

Subscribe to our Newsletter 


Get occassional tutorials, guides, and reviews in your inbox. No spam
ever. Unsubscribe at any time.

Enter your email...

Subscribe

Step 3: Install PyInstaller


In this step, we'll install pyinstaller using pip. We need pyinstaller to
convert our Python scripts into executable (.exe) �les. You just need to
copy paste the command below into your command prompt and run
it:

$ pip install pyinstaller

Again, to con�rm 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 �les 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 �les to
.exe with a single command. So, let's do it!

Open up the command prompt and navigate to the directory that your
Python �le/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 �le, you
should check that your �le 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 �ne before converting it to an
executable �le. Run the following command on your command
prompt:

$ python name_of_your_file.py

In my case, the �lename was 'sum.py'.

To create a standalone executable �le in the same directory as your


Python �le, 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 �nd the executable �le in the
'dist' folder. Please note that the "one�le" argument tells pyinstaller to
create a single executable �le only.

Let's now run our executable �le to see if the procedure worked!

Ta-da! It worked just as expected.

A little tip, if your executable �le closes too fast for you to notice the
output, you can add an input() line at the end of your Python �le,
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


�les, like phantomjs , you need to keep them in the same directory as
your Python �le's directory so that pyinstaller can include it in the
executable.

Conclusion
In this tutorial, we discussed in detail the conversion of Python scripts
to executable �les using Python's pyinstaller library in four steps. We
started by installing cURL, followed by pip and pyinstaller. Lastly, we
converted a sample Python �le to executable to ensure that the
procedure works on Windows.

 python (/tag/python/)


https://twitter.com
/share?text=Converting%20Python%20Scripts%20to%20Executable%20Files&
url=https://stackabuse.com/converting-python-scripts-to-executable-�les/)
https://www.facebook.com/sharer/sharer.php?u=https://stackabuse.com
/converting-python-scripts-to-executable-�les/)
https://plus.google.com/share?url=https://stackabuse.com/converting-
python-scripts-to-executable-�les/)
https://www.linkedin.com/shareArticle?mini=true%26url=https:
//stackabuse.com/converting-python-scripts-to-executable-
�les/%26source=https://stackabuse.com)

(/author/guest/)
About Guest Contributor (/author
/guest/)

Subscribe to our Newsletter


Get occassional tutorials, guides, and reviews in your inbox. No spam
ever. Unsubscribe at any time.

Enter your email...

Subscribe


Comments Community 
1 Login

 Recommend 4 t Tweet f Share Sort by Best

Join the discussion…


LOG IN WITH
OR SIGN UP WITH DISQUS ?

Name

Horlugingin Ayoade

3 months ago

Hi , unfortunately , i was not able to make it work for my cmd


but it worked for other applications
△ ▽ Reply

ALSO ON STACKABUSE

Spring Cloud: Hystrix Git: Merge Branch into


1 comment • 4 months ago Master
Avatarkunal Thakker — Hi 2 comments • 4 months ago
Dhananjay,I have been AvatarScott — Editor here. Glad to
trying to fix a problem with hear it was helpful to you!
Thanks!

Basics of Memory Spring Cloud: Turbine


Management in Python 1 comment • 4 months ago
1 comment • a month ago AvatarManuel Silva — Best
AvatarAlbertMietus — Remember: Regards From Mexico City.
garbage collection (at least Manuel Silva
the implementation) is part


 Previous Post : Python Performance Optimization (/python-performance-optimization/)

Next Post : Sorting and Merging Single Linked List  (/sorting-and-merging-single-linked-list/)

Ad

Follow Us

 Twitter (https://twitter.com/StackAbuse)
 Facebook (https://www.facebook.com/stackabuse)
 RSS (https://stackabuse.com/rss/)

Newsletter

Subscribe to our newsletter! Get occassional tutorials, guides, and


reviews in your inbox.

Enter your email...

Subscribe

No spam ever. Unsubscribe at any time.


Ad

Our Sponsors

(https://stackabu.se/digitalocean)

The simplest cloud platform for developers and teams.


Learn More (https://stackabu.se/digitalocean)

Interviewing for jobs?

(https://stackabu.se/triplebyte)

Take the quiz once and get pre-screened for life

Find your strengths and get matched to a broad range of positions

Skip straight to �nal interviews with top tech companies, like:

(https://stackabu.se/triplebyte)

   Take Triplebyte's Quiz (https://stackabu.se/triplebyte)

Recent Posts 
Solving Sequence Problems with LSTM in Python's Keras Library (/solving-
sequence-problems-with-lstm-in-pythons-keras-library/)

Java: List Files in a Directory (/java-list-�les-in-a-directory/)

Deploying a Flask Application to Heroku (/deploying-a-�ask-application-to-heroku/)

Tags

ai (/tag/ai/) algorithms (/tag/algorithms/) amqp (/tag/amqp/)

angular (/tag/angular/) announcements (/tag/announcements/)

apache (/tag/apache/) arduino (/tag/arduino/)

arti�cial intelligence (/tag/arti�cial-intelligence/) aws (/tag/aws/)

bash (/tag/bash/)

Follow Us

 Twitter (https://twitter.com/StackAbuse)
 Facebook (https://www.facebook.com/stackabuse)
 RSS (https://stackabuse.com/rss/)

Copyright © 2019, Stack Abuse (https://stackabuse.com). All Rights


Reserved.
Disclosure (/disclosure) • Privacy Policy (/privacy-policy) • Terms of Service (/terms-of-service)

You might also like