Tutorial Py2exe
Tutorial Py2exe
Tutorial Py2exe
py2exe turns Python programs into packages that can be run on other Windows
computers without needing to install Python on those computers. Python is needed on
the computer where py2exe itself is run because py2exe is a Python program and it
includes parts of Python in the package that is built.
To successfully complete this tutorial you'll need to know the basics of Python (you can
get started at python.org's getting started page). You'll also need to know how to run
Python programs from the command prompt.
There are a few simple steps needed to use py2exe once you've installed it:
1.
2.
3.
4.
5.
1print"HelloWorld!"
hello.py
Looks good!
1fromdistutils.coreimportsetup
2importpy2exe
4setup(console=['hello.py'])
setup.py
Notice that this is ordinary Python. Let's go through it line by line...
1. When working with py2exe the only part of Distutils we'll typically need to
reference directly is the setup function, so that's all we'll import.
2. Once Distutils is loaded, we need to load py2exe so that it can add its command.
3. Whitespace is good!
4. Call setup and tell it that we want a single console application and the main entry
point is "hello.py".
The next step is to run your setup script. Make sure to give the py2exe command and
expect to see lots and lots of output:
C:\Tutorial>pythonsetup.pypy2exe
runningpy2exe
***searchingforrequiredmodules***
***parsingresults***
creatingpythonloaderforextension'zlib'
creatingpythonloaderforextension'unicodedata'
creatingpythonloaderforextension'bz2'
***findingdllsneeded***
***createbinaries***
***bytecompilepythonfiles***
bytecompilingC:\Tutorial\build\bdist.win32\winexe\temp\bz2.pytobz2.pyc
bytecompilingC:\Tutorial\build\bdist.win32\winexe\temp\unicodedata.pyto
unicodedata.pyc
bytecompilingC:\Tutorial\build\bdist.win32\winexe\temp\zlib.pytozlib.pyc
skippingbytecompilationofc:\Python24\lib\StringIO.pytoStringIO.pyc
[skippingmanylinesforbrevity]
skippingbytecompilationofc:\Python24\lib\warnings.pytowarnings.pyc
***copyextensions***
***copydlls***
copyingc:\Python24\lib\sitepackages\py2exe\run.exe>
C:\Tutorial\dist\hello.exe
***binarydependencies***
Yourexecutable(s)alsodependonthesedllswhicharenotincluded,
youmayormaynotneedtodistributethem.
Makesureyouhavethelicenseifyoudistributeanyofthem,and
makesureyoudon'tdistributefilesbelongingtotheoperatingsystem.
ADVAPI32.dllC:\WINDOWS\system32\ADVAPI32.dll
USER32.dllC:\WINDOWS\system32\USER32.dll
SHELL32.dllC:\WINDOWS\system32\SHELL32.dll
KERNEL32.dllC:\WINDOWS\system32\KERNEL32.dll
C:\Tutorial>
Two directories will be created when you run your setup script, build and dist. The build
directory is used as working space while your application is being packaged. It is safe to
delete the build directory after your setup script has finished running. The files in the dist
directory are the ones needed to run your application.
C:\Tutorial>cddist
C:\Tutorial\dist>hello.exe
HelloWorld
Excellent, it works!!!
your mouse over the dll file (or the vcredist_x86.exe installer executable) to confirm
which version you've got. You'll need the vcredist_x86.exe that contains the Microsoft
Visual C++ 2008 Redistributable Package published 29-11-2007, so not the VS2008
SP1 one (tested with Python 2.7.1).
As for older versions of Python, you need to check redist.txt within your Visual Studio
installation to see whether you have the legal right to redistribute this DLL. If you do
have these rights, then you have the option to bundle the C runtime DLL with you
application. If you don't have the rights, then you must have your users run the
redistributable C runtime installer on their machines.
5.2.1. Bundling the C runtime DLL
If you do have the rights to redistribute MSVCR90.dll, there should be a copy of it in
your Visual Studio install, under VC\redist\x86\Microsoft.VC90.CRT. Since Visual Studio
2008, you can't just copy this DLL file - you also need the manifest file that you'll find
there. The redist.txt file states that you must distribute all three dlls and the unmodified
manifest file and it is a violation of the license agreement to distribute only one of the
dlls without the others (though py2exe only needs MSVCR90.dll.) The pertinent
passage from the redist.txt file is as follows:
"For your convenience, we have provided the following folders for use when
redistributing VC++ runtime files. Subject to the license terms for the software,
you may redistribute the folder (unmodified) in the application local folder as a
sub-folder with no change to the folder name. You may also redistribute all the
files (*.dll and *.manifest) within a folder, listed below the folder for your
convenience, as an entire set."
You must make py2exe copy the three dlls and the manifest file into your project's dist
directory, in a subdirectory called 'Microsoft.VC90.CRT'. To achieve this, add a
data_files option to your project's setup.py:
fromglobimportglob
data_files=[("Microsoft.VC90.CRT",glob(r'C:\ProgramFiles\Microsoft
VisualStudio9.0\VC\redist\x86\Microsoft.VC90.CRT\*.*'))]
setup(
data_files=data_files,
etc
)
With this in place, running py2exe should put the files into your dist directory:
dist
|
+Microsoft.VC90.CRT
||
|+Microsoft.VC90.CRT.manifest
|+msvcm90.dll
|+msvcp90.dll
|+msvcr90.dll
|
|etc
Now, simply copying the whole dist directory to your users machines should now allow
your application to run, even on machines that don't have their own copy of the C++
runtime.
Note that this method of including the C runtime is used by several Visual C++
applications - if you search your Program Files folder for msvcr90.dll, you may find
several applications that have this DLL and the associated manifest bundled alongside
their executable like this.
Also note that despite all the above, py2exe will complain that it cannot find
MSVCP90.dll. You must edit your setup.py to add the path to the dlls to the sys.path,
e.g.
sys.path.append("C:\\ProgramFiles\\MicrosoftVisualStudio
9.0\\VC\\redist\\x86\\Microsoft.VC90.CRT")