Recipe 16.11. Automating the py2exe Compilation of Scripts into Windows Executables
Credit: Alexander Semenov
Problem
You often use py2exe
to build Windows .exe files from Python scripts,
but you don't want to bother writing a
setup.py build script for each and every such
script.
Solution
distutils is a package in the standard Python
library, ready to be imported from your Python code.
py2exe is a third-party extension to
distutils for the specific task of generating
Windows executables from Python code: you must download and install
py2exe separately, but once installed, it
cooperates smoothly with the standard distutils.
Thanks to these features, you can easily write Python scripts to
automate distutils tasks (including
py2exe tasks). For example:
from distutils.core import setupSave this as makexe.py in the
import sys, os, py2exe
# the key trick with our arguments and Python's sys.path
name = sys.argv[1]
sys.argv[1] = 'py2exe'
sys.path.append(os.path.dirname(os.path.abspath(name)))
setup(name=name[:-3], scripts=[name])
Tools\Scripts\ folder of your Python
installation. (You should always add this folder to your Windows
PATH because it contains many useful tools.) Now,
from a Windows command prompt, you're able to
cd to a directory where you have placed a script
(say C:\MyDir\), and there run, say:
C:\MyDir> makexe.py myscript.pyand (assuming that you have a myscript.py script
there, and .py among your Windows executable
extensions, with association to the Python interpreter)
py2exe prepares all the files you need for
distributing your masterpiece (as a Windows executable and supporting
DLLs), neatly arranged in folder
c:\MyDir\dist\myscript\.
Discussion
The distutils package is part of the Python
Standard Library. It helps you prepare your Python modules and
extensions for distribution, as well as letting you install such
packages as distributed by others. py2exe is a
freely downloadable third-party extension that works on top of
distutils to help you build a Windows
.exe file (and a set of supporting DLL files)
from a Python-coded program, so that you can distribute your program
in executable form to other Windows PCs that may not have Python
installed; see http://starship.python.net/crew/theller/py2exe/,
both to download py2exe and for detailed
documentation of this useful tool.Following the details given in the distutils (and
py2exe) documentation, the canonical way to use
distutils (including py2exe) is
to write a script, conventionally always named
setup.py, to perform all kinds of
distutils tasks on your package. Normally, you
write a setup.py for each package you
distribute, placing it in the top directory of the package (known as
the distribution root in
distutils terminology).However, there is nothing mandatory about the convention of writing a
setup.py script per package.
distutils and py2exe, after
all, are written as modules to be imported from Python. So, you can,
if you so choose, use all the power of Python to code scripts that
help you perform distutils and
py2exe tasks in whatever ways you find most
convenient.This recipe shows how I eliminate the need to write a separate
setup.py script for each Python script that I
convert to an executable with py2exe, and related
issues such as the need to keep such scripts in dedicated
"distribution root" directories. I
suggest you name this recipe's script
makexe.py, but any name will do, as long as you
avoid naming it py2exe.py (a natural enough
temptation). (Naming it py2exe.py would break
the script because the script must import py2exe,
and if you named the script py2exe.py it would
"import itself" instead!)Place this script on any directory on your Windows
PATH where you normally keep executable Python
scripts. I suggest you use the Tools\Scripts
folder of the Python distribution, a folder that contains several
other useful scripts you'll want to have handy (have
a look in that folderit's worth your time).
I'm not going to delve into the details of how to
set and examine your Windows PATH, open a command
prompt, make your Python scripts executable, and so on. Such system
administration details differ slightly on each version of Windows,
and you'll need to master them for any Windows
version on which you want to perform significant programming, anyway.Once you have implemented this Solution, you'll find
that making your Python scripts into Windows executables has become
so easy and so fast that soon you'll be distributing
your neat and useful programs to friends and acquaintances right and
left. You won't need to convince them to install the
Python runtime files before they can install and run your programs,
either! (Of course, in this way they will end up
with what amounts to several copies of the runtime files, if they
install several of your compiled programsthere is little you
can do about that.)
See Also
The section "Distributing Python
Modules" of the standard Python documentation set is
still incomplete but a good source of information on the
distutils package; Python in a
Nutshell covers the essentials of the
distutils package; py2exe is at
http://starship.python.net/crew/theller/py2exe/.