Saturday, November 20, 2010

Python to .exe

Have you ever wondered how to distribute your python programs as a self contained .exe, so that your potential user don't even have to install any Python interpreter?

The answer is easy, use py2exe. In this post I'm going to provide a step by step guide on how to use it. Take it as just a quick introductory guide to get you started:

  • Obviously a working python script is necessary, as well as a python interpreter (preferably >= 2.), since py2exe is just a python module.
  • I don't mean to be repetitive, but your python script must be a working one. For your first experience with py2exe it is recommended to have a very simple script depending on just the standard library.
  • Create a setup script, that is another python script with the obvious name of setup.py:

from distutils.core import setup
import py2exe

setup(console=['yourapp.py'])
  • Run the setup script:
    python setup.py py2exe
  • Now you should have an exe file ready to be executed. The file is placed under the dist folder, created just under the folder where the setup script was executed

I hope it works fine for you, it does the trick for me.

Enjoy!!!!!