Nothing Special   »   [go: up one dir, main page]

Convert A Python 3.5 Script (.Py) To A Windows Executable (.Exe)

Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

Convert a Python 3.5 Script (.

py) to a Windows
Executable (.exe)

I was up until 1 AM trying to figure out how to convert a Python (.py) script into a windows executable (.exe).
It’s not an easy feat! I finally had it figured out around noon time today.

What is frustrating about this endeavor is there aren’t a lot of options. What options are out there, do not have
much in the way of up-to-date supporting documents.

From what I could find from my friend google.com, there are three main options:

cx_freeze - Unfortunately, it doesn’t support Python 3.5 yet.


py2exe - I initially tried this software first, but could not make it work.
PyInstaller - This one I got to work! It is complicated to install, but pretty simple to use.

Of the three, PyInstaller had the best supporting documentation, but I still spent two days struggling to even
install this package!

I doubt I am not the only to ever face the dilemma of wanting to convert .py to .exe and frustrated by the lack of
decent tutorials out there. So in this tutorial, I will demonstrate how to install PyInstaller and then how to
convert a Python 3.5 script .py to .exe using PyInstaller.

Start by installing pywin32/64.exe (https://sourceforge.net/projects/pywin32/files/pywin32/Build%20221/)

Step 1: Go to http://www.pyinstaller.org/ and navigate to Documentation,


Step 2: Scroll down to Installing in Windows, and click the pip-Win link,

Step 3: Download pip-Win, (https://bitbucket.org/pcarbonn/pipwin/downloads/pip-Win_1.7.exe)


Step 4: Install pip-Win. Navigate to download folder, double click, and then hit run,

Step 5: Enter venv -c -i pyi-env-name into the pip-Win command field and click run. This will create a virtual
environment, make it the current environment, and open a command shell so commands can be run in this
environment.
Step 6: In this new command shell, enter pip install PyInstaller,

Step 7: Verify installation by typing pyinstaller --version in the command window, the result should resemble
3.n (in my case 3.0).
It is possible, that despite positive messages verifying installation that you will still get a message that
pyinstaller is not recognized as an internal or external command blah blah blah... As was the case with my own
installation.

Most likely, you will need to add the windows path environment to include the location of PythonXY\Scripts;
where XY is the version number (in my case \Python35-32\Scripts)

If this happens, navigate START > control panel > System and Security > System > Advanced System Settings
> Environment Variables.

Then scroll down to Path under System variables, type a semicolon (;), and then type in your python script path,

After all that, PyInstaller should be successfully installed and you should be able to easily convert any .py to
.exe. In my case, the script I wanted converted was named LifeOfFred.py, you can read about this program here.
Lets see how to create LifeOfFred.exe.

Step 1: Start pip-Win and enter venv pyi-env-name into the command field, this will open the command shell.
Step 2: In the command window enter pyinstaller LifeOfFred.py,
This will create two folders (if not already created) named build and dist. Each of which contains a folder with
the same name as the script (LifeOfFred in my case).

The build folder contains all the working files and the dist folder contains the executable file and all the files
needed to run the program. You can now compress the associated dist folder for distribution to many worldwide
users.

In my case, I could compress the LifeOfFred folder inside of the dist folder - LifeOfFred.zip. I could then
transmit this to my users, who would install the program by simply unzipping the folder. To run the program,
they would open the folder and launch LifeOfFred.exe.

However, you may prefer to bundle the script and all the dependencies into a single executable file. This is
easily done by the --onefile command. In my case, I typed pyinstaller --onefile --windowed LifeOfFred.py in the
command window,
Now my bundled LifeOfFred.exe can be found just inside PythonXY\dist,

Hopefully, now, my two days of struggle will be an easy hour for whoever made it to the end of this!

BTW, you can download the bundled LifeOfFred.exe, though it is not a useful program!

You might also like