[Tutor] how do i use p2exe

Jacob S. keridee at jayco.net
Wed Nov 10 04:59:15 CET 2004


This is what I did.
I'm using Windows XP, but it would work for any other windows version...

1) Take code below and copy into file named "setup.py".

### Start of Code ###
from distutils.core import setup
import py2exe
import os


def lookdir():
    print "Current directory is: %s" % os.getcwd()
    look = raw_input('Do you wish to see what\'s in directory? ')
    if look.lower() in m:
        print "\n".join(os.listdir(os.getcwd()))

def changedir():
    m = ['y','ye','yes','yep','okay','affirmative','sure']
    ask = 'y'
    lookdir()
    while ask not in m:
        di = raw_input('What directory do you want? ')
        os.chdir(di)
        lookdir()
        ask = raw_input('Do you want this directory? ')

changedir()
listed = []
while 1:
    ask = raw_input('What is the file you want as an executable? (Type
\'quit\' to break out of loop) ')
    if ask == 'quit' or ask == 'stop' or ask == '':
        break
    else:
        listed.append(os.path.join(desktop,ask))

setup(console = listed)
### End of Code ###

2) Take following code and save as a batch file. You will have to change the
second line to change the directory to your python dir

rem Start of Code
@echo off
cd\python23
start python setup.py py2exe
rem End of Code

3) Run the batch file. It will ask you which directory the script file is
in. That would be the file that you're trying to make and executable. Then,
when you decide which directory it is in, it will ask you the name of the
file. You type in the name. If you want more than one file, you can type in
another file name in the next prompt, else you can type in 'quit' or 'stop'
or just hit enter. When all is done and the shell window closes, you can
check out the directory that you chose. In that directory, there will be two
new folders. One is labeled build. That folder is not necessary to run your
executable and can be deleted. I usually delete it. The other is labeled
dist. It contains the files needed for your program. Your program will have
the same name, just with a exe extension instead of a py extension. Send the
whole folder on to your students, and they can double-click on the exe file,
and it will run your script as if you double-clicked it in Windows Explorer.

Also, in your code (which I will try to rewrite for fun on my own (no
offense)) you might try this instead:

print "".join(["\t","\\"*7," ","\\"*4," ","\\"*6," ","\\"*7," ","\\"*2,"
","\\"*10," ","\\"*2," ","\\"*8,"\n"])

Ignore the underline and blue if it shows up in your email thing.
This just shows that you can multiply a particular string by an integer to
copy it.

Hope all this helps,
Jacob Schmidt



More information about the Tutor mailing list