Ed's Home

Aug 31

Simple Python Distribution Tutorial

Tech | Tags: python | No Comments

I want to be able to send someone a package containing a Python script and the bare minimum for them to run that script on Windows. In Perl, this is quite easy, I believe you can just send your script with perl.exe. This is how I did it in Python.

I’m assuming you already have Python installed. I did this on Python 2.5. You may need to make some changes for other version of Python. My starting point was the tutorial on the py2exe website.

Install py2exe and create hello.py:

print "hello world"

Create a script to run hello.py, call it exec_hello.py:

import runpy
import sys
import os

sys.path += [os.path.dirname(sys.argv[0])]
runpy.run_module('hello.py')

Create a setup.py script:

from distutils.core import setup
import py2exe
setup(console=['exec_hello.py'])

Run this at the command prompt to generate the distribution:

python setup.py py2exe

Copy hello.py into the dist directory.

You should now be able to run dist/exec_hello.exe at the command prompt and it will call hello.py.

I tided things up by getting the setup script to remove unnecessary files and to copy in the hello.py script. It also renames exec_hello.exe to hello.exe:

import os
import shutil
from distutils.core import setup
import py2exe

setup(console=['exec_hello.py'])
os.remove('dist/bz2.pyd')
os.remove('dist/unicodedata.pyd')
os.remove('dist/w9xpopen.exe')
shutil.rmtree('build')
shutil.copy('hello.py', 'dist')
shutil.move('dist/exec_hello.exe', 'dist/hello.exe')

Trackback URI | Comments RSS

Leave a Reply

Name (required)

Email (required)

Website

Speak your mind

    Recent Posts
    • Rudolph the Red-Nosed Reindeer
    • Vertical gardens
    • Sunday, Sunday
    • Stop the Clocks
    • First Bite
    • Weather Map
    • Is you the Minotaur?
    • First impressions of Android
    • Call me Android
    • The black dot virus
    Categories
    • Film
    • Music
    • Tech
    • Theatre
    • Travel
    • Uncategorized
    Archives
    • January 2012
    • December 2011
    • November 2011
    • September 2011
    • August 2011
    • July 2011
    • June 2011
    • May 2011
    • April 2011
    • March 2011
    • January 2011
    • November 2010
    • September 2010
    • August 2010
    • July 2010
    • June 2010
    • May 2010
    • April 2010
    • February 2010
    • December 2009
    • November 2009
    • October 2009
    • September 2009
    • August 2009
    • July 2009
    • November 1999
    Blogroll
    • Bermudianism Bermudianism
    • Joel Love Joel Love
    • Katie Day Katie Day
    • Paul Harrison Paul Harrison
    • Rebellious Jukebox Rebellious Jukebox
    • The Other Way Works The Other Way Works
    Follow Me
    • Facebook Facebook
    • Google Reader Google Reader
    • RSS Feed RSS Feed
    • Twitter Twitter
    Websites I've Designed
    • bullring.org bullring.org
    • Casa Campana Casa Campana
    • The Other Way Works The Other Way Works
    @edporteous twitter
    • @otherwayworks check out your new signup form at http://t.co/LxfMlwBs 04:48:52 PM January 20, 2012 from web in reply to otherwayworks ReplyRetweetFavorite
    • So come on then, who's going to invent colour e-ink wallpaper? 12:30:36 AM January 08, 2012 from Twitter for iPhone ReplyRetweetFavorite
    • Even prehistoric reptiles can use Android! http://t.co/teB2aVgZ 12:16:30 AM January 06, 2012 from Twitter for iPad ReplyRetweetFavorite
    • What does Rudolph do on New Year's day? Watch this to find out... http://t.co/ftMsZU1f 02:39:26 PM January 02, 2012 from web ReplyRetweetFavorite
    • Looks like Windows Phone 7 needs more work. This long term review makes you appreciate iPhone's design http://t.co/VYD7lFAy 12:17:31 PM December 30, 2011 from Flipboard ReplyRetweetFavorite
    @edporteous
Powered by WordPress | Using Bluebird theme by Randa Clay | Copyright © Ed's Home | Top