[r376]: trunk / py2exe / sandbox / docs / py2exe.txt Maximize Restore History

Download this file

py2exe.txt    203 lines (142 with data), 6.8 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
.. image:: py2exe.jpg
:align: center
.. _license: LICENSE.TXT
.. _distutils: http://www.python.org/doc/current/dist/
.. _setup-script: http://www.python.org/doc/current/dist/setup-script.html
.. _py2exe-0.5.0.win32-py2.3.exe: http://prdownloads.sourceforge.net/py2exe/py2exe-0.5.0.win32-py2.3.exe?download
.. _wiki: http://starship.python.net/crew/theller/moin.cgi/Py2Exe
Convert python scripts into standalone windows programs
=======================================================
Copyright (c) 2001, 2002, 2003, 2004 Thomas Heller, Mark Hammond.
Abstract
--------
**py2exe** is a Python distutils_ extension which converts python
scripts into executable windows programs, able to run without
requiring a python installation.
It has been used to create wxPython, Tkinter, Pmw, PyGTK, pygame,
win32com client and server modules and other standalone programs.
**py2exe** is distributed under an open-source license_.
News
----
**py2exe 0.5.0** released.
Python 2.3 is required, because the new zipimport feature is used.
It builds a single directory from your script(s), containing one
or more related executables files, a single zipfile containing the
Python modules needed, the Python dll, and the Python extension modules.
Using py2exe
------------
Assuming you have written a python script ``myscript.py`` which you
want to convert into an executable windows program, able to run on
systems without a python installation. If you don't already have
written a *distutils* setup-script_, write one, and insert the
statement 'import py2exe' before the call to the setup function::
# setup.py
from distutils.core import setup
import py2exe
setup(console=["myscript.py"])
Running ::
python setup.py py2exe --help
will display all available command-line flags to the **py2exe**
command.
Now you can call the setup script like in this way::
python setup.py py2exe
and a subdirectory ``dist`` will be created, containing the files
``myscript.exe``, ``python23.dll``, and ``library.zip``. If your script
uses compiled C extension modules, they will be copied here as well,
also all dlls needed at runtime.
These files include everything that is needed for your program, and
you should distribute the whole directory contents.
The above setup script creates a console program, if you want a GUI
program without the console window, simply replace
``console=["myscript.py"]`` with ``windows=["myscript.py"]``.
**py2exe** can create more than one exe file in one run, this is
useful if you have a couple of related scripts. Pass a list of all
scripts in the ``console`` and/or ``windows`` keyword argument.
Specifying additional files
---------------------------
Some applications need additional files at runtime, like configuration
files, fonts, or bitmaps.
**py2exe** can copy these files into subdirectories of ``dist`` if they
are specified in the setup script with the ``data_files``
option. ``data_files`` should contain a sequence of ``(target-dir,
files)`` tuples, where files is a sequence of files to be copied.
Here's an example::
# setup.py
from distutils.core import setup
import glob
import py2exe
setup(console=["myscript.py"],
data_files=[("bitmaps",
["bm/large.gif", "bm/small.gif"]),
("fonts",
glob.glob("fonts\\*.fnt"))],
)
This would create a subdirectory ``dist\bitmaps``, containing the two
``.gif`` files, and a subdirectory ``dist\fonts``, containing all the
``.fnt`` files.
Windows NT services
-------------------
You can build Windows NT services by passing a ``service`` keyword
argument to the ``setup`` function, the value must be a list of Python
module names containing a service class (identified by the ``_svc_name_``
attribute)::
# setup.py
from distutils.core import setup
import py2exe
setup(service=["MyService"])
The build service executable are able to install and remove themselves
by calling them with certain command line flags, run the exe with the
``-help`` argument to find out more.
COM servers
-----------
COM servers are built by passing a ``com_server`` keyword argument to
the setup function, again the value must be a list of Python module
names containing one or more COM server classes (identified by their
``_reg_progid_`` attribute)::
# setup.py
from distutils.core import setup
import py2exe
setup(com_server=["win32com.server.interp"])
By default both DLL and EXE servers are built, you should simply
delete those you don't need.
Advanced features
-----------------
So far, you have to look into the supplied samples.
There is also a wiki_ now where you can look for information, or
publish your tips.
How does it work?
-----------------
**py2exe** uses python's ``modulefinder`` to examine your script and
find all python and extension modules needed to run it. Pure python
modules are compiled into ``.pyc`` or ``.pyo`` files in a temporary
directory. Compiled extension modules (``.pyd``) are also found and
parsed for binary dependencies.
A zip-compatible archive is built, containing all python files from
this directory. Your main script is inserted as a resource into a
custom embedded python interpreter supplied with py2exe, and the
zip-archive is installed as the only item on ``sys.path``.
In simple cases, only ``pythonxx.dll`` is needed in addition to
``myscript.exe``. If, however, your script needs extension modules,
unfortunately those cannot be included or imported from the
zip-archive, so they are needed as separate files (and are copied into
the ``dist`` directory).
*Attention*: **py2exe** tries to track down all binary dependencies
for all pyd's and dll's copied to the dist directory recursively, and
copies all these dependend files into the dist directory. **py2exe**
has a builtin list of some system dlls which are not copied, but this
list can never be complete.
Installing py2exe
-----------------
Download and run the installer
py2exe-0.5.0.win32-py2.3.exe_.
This installs **py2exe** together with some samples, the samples are in
the ``lib\site-packages\py2exe\samples`` subdirectory.
For **Windows 95/98/Me**, you need the *Microsoft Layer for Unicode on
Windows 95/98/ME Systems (MSLU)* dll from here_ (Internet Explorer is
required to download it: Scroll down to the Win95/98/Me section).
Download and run the self-extracting ``unicows.exe`` file, and copy
the unpacked ``unicows.dll`` file in the directory which contains your
``python.exe``. Note that this is only needed on the machine where
you want to build executables with **py2exe**, it is not required on
the machine where you want to run the created programs.
.. _here: http://www.microsoft.com/msdownload/platformsdk/sdkupdate/psdkredist.htm