py2exe用法


How to use py2exe - the basics
[url=javascript:void window.open('http://www.pharscape.org/index2.php?option=com_content&task=view&id=33&Itemid=51&pop=1&page=0', 'win2', 'status=no,toolbar=no,scrollbars=yes,titlebar=no,menubar=no,resizable=yes,width=640,height=480,directories=no,location=no');]

[/url]
[url=javascript:void window.open('http://www.pharscape.org/index2.php?option=com_content&task=emailform&id=33', 'win2', 'status=no,toolbar=no,scrollbars=yes,titlebar=no,menubar=no,resizable=yes,width=400,height=250,directories=no,location=no');]

[/url]

A lot of new Windows based Python coders think they don't have a real program until their code has been transformed into an executable with it's magical .exe file extension.
Most python programs never get that sprinkling of fairy dust.
Anyway, this is how you do it...
This is a step-by-step guide to making python programs standalone executables.:

1. Download and install py2exe from the
py2exe website
(make sure you download the right version!)
2. Create a directory on you hardisk called mycode ... - Right click on the Windows Desktop and select New .. Folder and name it "mycode"
3. Double click the mycode folder to open it
4. Right click in the folder and select New.. Text Document, name it "hello.txt"
5. Right click in the folder and select New.. Text Document, name it "setup.txt"
6. Double click hello.txt to open it in Notepad
7. Copy the following text into the file and save it:
#Start here
print "Hello py2exe"
raw_input("Press any key to finish")
#End here
8. Double click the setup.txt and copy in the following code (and save the file):
#Start here
from distutils.core import setup
import py2exe
setup(console = ["hello.py"])
#End here
9. Right click on the hello.txt file and rename it to hello.py (ignore the warning)
10. Right click on the setup.txt file and rename it to setup.py (ignore the warning)
11. Start a command console (also called a DOS box) (It is found in the START-> Accessories menu on Windows XP). Alternatiively -start a command console by clicking Start -> Run...  and type cmd in the dialog.
12. In the console type
cd desktop
cd mycode
13. Type
start hello.py
14. If you entered the code correctly then a new console is created which asks you to press a key
15. The program works so now we can also use py2exe to make a standalone program. Type
start setup.py py2exe
16. If there are no errors in the setup.py file then py2exe will run. It creates a new sub-directory called dist. Type
cd dist
17. There should be a file called hello.exe Type:
hello
18. You have finished.

Remember - every file in the dist directory is needed for your hello.exe to run properly. (Zip all the files together and send it to your friends).

Py2exe - the littlest script?
[url=javascript:void window.open('http://www.pharscape.org/index2.php?option=com_content&task=view&id=18&Itemid=51&pop=1&page=0', 'win2', 'status=no,toolbar=no,scrollbars=yes,titlebar=no,menubar=no,resizable=yes,width=640,height=480,directories=no,location=no');]

[/url]
[url=javascript:void window.open('http://www.pharscape.org/index2.php?option=com_content&task=emailform&id=18', 'win2', 'status=no,toolbar=no,scrollbars=yes,titlebar=no,menubar=no,resizable=yes,width=400,height=250,directories=no,location=no');]

[/url]
Py2exe
is as easy or as complex to use as you wish to make it. The following code demonstrates a minimal script.
For a console application (one that allows input and output using the print and raw_input function):
from distutils.core import setup
import py2exe
import sys
sys.argv.append("py2exe")
setup(console = [{"script": 'prog.py'}])
If you want a "pure" windows application perhaps using Tkinter or wxPython:
from distutils.core import setup
import py2exe
import sys
sys.argv.append("py2exe")
setup(windows = [{"script": 'prog.py'}])
Copy either of these scripts to the directory containing the program you want to "compile", saving it as "setup.py". You will need to replace the name "prog.py" with the name of your script.
To compile the script just run
python setup.py