Tuesday, May 6, 2008

Python to Windows executable

I'm working on freelance project which is a python application using PyQT as a GUI, during the testing phase the application needs to be installed on clients workstation and in order to do that I have to install Python and PyQT before I can run my application. This is fine If I'm the one who will install the application but the problem will arise if the end user will be the one installing it, they might run into problems such as installing the wrong version of Python and PyQt. I also need to include Python and PyQT installer to make sure that they have the right version but this will make my distribution package large. It would be great if i can redistribute my application that the end user can install on his machine minus the worries of incompatible Python version and the exclusion of Python and PyQT installer. So I googled around and found this site http://www.py2exe.org this can convert my Python script into packages and executable Windows program without installing Python, py2exe is not an installer builder what it does is convert and assemble the files needed in order to run Python program without installing Python.

py2exe requires the creation of setup.py and we just need to import py2exe package and setup script from disutils package like so:

setup.py
-------------------------------------------------------------
from distutils.core import setup
import py2exe

setup(console=['testing.py'])
-------------------------------------------------------------

the next step is to run the setup script on the command prompt like this:
c:\testing > python setup.py py2exe

We will see lots of output printed on the console and at the end two directories will be created -- "build" and "dist" folder, we can delete build directory while dist directory contains the package and windows executables. As I have mentioned a while ago py2exe is not an installer builder if we need one we can use Inno Setup to build one.

Here's my setup script
-------------------------------------------------------------
from distutils.core import setup
import py2exe, os

class build_DIRIncludes(py2exe):
    def run(self):
    # First, let py2exe do it's work.
    py2exe.run(self)

    print "--- moving conf ---"
    print os.popen('move conf').read()

    print "--- moving inputs ---"
    print os.popen('move inputs').read()

    print "--- moving output ---"
    print os.popen('move output').read()

    print "done"

setup(
    options = {"py2exe": {"optimize": 2,
                    "packages": ["PyQt4","sip"],
                    "dist_dir": "my_application",
                      }
                },
    windows=['my_application.py'],
    cmdclass = {"py2exe": build_DIRIncludes},
    )
-------------------------------------------------------------

0 comments:

Post a Comment