Turning a python script into a Windows executable file

Yes, not the most exciting blog post, but sometimes it’s good to strive for utility. If you know Python, at some point you might create a useful script that you would like to package and send to others. Perhaps like me, you work for a company that uses PCs, and you would like to create a .exe file. It’s fairly simple using py2exe, however when you go to distribute your new .exe file, you’ll realize that it depends on several libraries, so it’s really not as mobile as you thought.

To get around this, you’ll need to create an executable file with all of the libraries included. Here is how it’s done, thanks to Minty’s response on Stackoverflow.

1) If you haven’t done so already, download the latest Python version (right now it’s 2.7.3) as well as py2exe. Install Python and then install py2exe.

Be sure to use Python version 2.x. If you’re using Python 3, you’ll find that the most common extension that converts executables–py2exe–does not work with version 3. You’ll have to use cx_freeze. I couldn’t get cx_freeze to work, so good luck with that.

2) Create your python script–henceforth known as your_script.py— and save it somewhere easy, like on your desktop.

3) Create another script with the following code, and name it setup.py. You’ll want to save this in your python directory. On my machine, this is at C:\Python27\

from distutils.core import setup
import py2exe, sys, os

sys.argv.append('py2exe')

setup(
    options = {'py2exe': {'bundle_files': 1}},
    windows = [{'script': "your_script.py"}],
    zipfile = None,
)

This is fine to use if you want to create a stand-alone script that the user double-clicks and it runs. However, I needed users to run my script via the command line and input some text, so I made the following modification:

from distutils.core import setup
import py2exe, sys, os

sys.argv.append('py2exe')

setup(
    options = {'py2exe': {'bundle_files': 1}},
    console = [{'script': "your_script.py"}],
    zipfile = None,
)

Make sure to use the absolute path to your_script.py, for example: C:\users\dylan\desktop\your_script.py.

4) Open up command line and navigate to your Python directory where you saved setup.py. Enter the following:

python setup.py py2exe

You’ll see a bunch of text scroll by talking about binaries and the like. Hopefully you receive no errors.

5) Navigate to the dist folder within your Python directory. In there you’ll find the .exe version of your_script.py.

6) Test out your executable file on your machine. Also would be a good idea to send it to another user and have them test it before distributing.

Note: This method creates a large executable file. My script was rather small and simple, and the final executable file was 5MB! However, I found the alternative method which probably creates a smaller file too confusing. It involves packaging the Microsoft Visual C runtime DLL with your executable–instructions can be found at the bottom of the py2exe tutorial.

Advertisements
  1. Good answers in return of this difficulty with solid arguments and explaining
    all on the topic of that.

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: