EDIT: If you are using different libraries, installing Python directly to Windows could be cumbersome. Configuring py2exe’s “setup.py” may also require extra effort. I offer another alternative for this case. Please see at the end of this post.
Sometimes you may need to convert your .py scripts into Windows executables. All we want is actually very clear. We want to create self compatible Windows executable files from .py scripts so that they can run in Windows machines without requiring any further installation to Windows machine. Thankfully, there is a nice and easy way to do this, which is py2exe. According to the original source of py2exe, py2exe is a Python Distutils extension which converts Python scripts into executable Windows programs, able to run without requiring a Python installation.
IMPORTANT NOTE: You will need to install proper Python version to your Windows machine to make this conversion using py2exe. In this case, you can say “But you said without requiring a Python Installation!”. Well, what we mean by this phrase is that once you made this conversion, you can run converted .exe files in any Windows machine and you do not need to install anything to that machines. However, you need to install required programs to make this conversion. Now, I guess it is more clear, right?
Let’s learn how to install Python and py2exe and lastly an example conversion from .py into .exe using py2exe.
WARNING!: You need to do these operations in Windows.
Step 1: Python Installation
Download python from official website. For example, python 3.4.3
WARNING!: py2exe has some compatibility problems with higher versions of Python. Therefore, if it is not required, do not use python version 3.5. or more. If it is a must for you then, this stackoverflow discussion might help.
Download Windows x86 MSI Installer if you are using 64 bit machine. Install python using the installer.
C:\Python34 (default location for installation)
In customize python screen click down arrow button and choose Entire feature will be installed on local drive as you can see below.
Next and next and wait for installation to finish.
Python 3.4.3 installer automatically adjust environmental variable paths, so hopefully you will not have any problem. However, if you have any problem, follow this tutorial.
In case of difficulty, here is an another tutorial to install python into Windows.
Step 2: py2exe Installation
Open windows commandline (cmd) and go to the directory which contain installed python scripts. If you chose default directory to install python then it will be:
$ cd C:\Python34\Scripts
Install py2exe using pip
$ pip install py2exe
Step 3: Convert your .py Script to .exe
First you need to create a setup.py script which calls py2exe and your script.
Suppose you have a python script named hello.py and you want to convert it to .exe. In this case an example setup.py will be:
from distutils.core import setup import py2exe setup(console=['hello.py'])
NOTE: It is important that both setup.py and hello.py should be in same directory. Otherwise you need to edit setup.py to show absolute path directory of your script.
Let’s say you put your setup.py and hello.py in a directory named Tutorial whose location is
From windows cmd go to that directory
$ cd C:\Tutorial
And run setup.py
$ python setup.py py2exe
If everything goes fine, you will be able to see hello.exe in
EDIT: For Python Scripts with Lots of Library Imports
As I mentioned, if you are using different libraries, installing Python directly to Windows could be cumbersome. Configuring py2exe’s “setup.py” may also require extra effort. For this case, I will recommend you to use either Anaconda or Miniconda to install Python to Windows and use pyinstaller instead of py2exe to convert .py scripts to .exe.
For example, I generally use Python Pandas library a lot and it is generally difficult to install it to Windows. I used Pyinstaller with Miniconda to convert my python script with Pandas library to windows executable.
First, follow this link to install Python to Windows using Anaconda/Miniconda.
Install the libraries you used in python script. (Using pip will be advantageous)
Go to your .py script directory and run Pyinstaller.
$ pyinstaller your_script.py
That’s it! I hope this will be useful for you. :)