Saturday, March 31, 2012

convert python programs to standalone windows exe file

hi guys wassup i found a interesting thing that i would like to share as a python programmer
the program is py2exe as the name suggests it converts python files to an exe standalone file here is the method

Create your setup script (setup.py)

py2exe extends Distutils with a new "command". If you've installed third party Python modules then there's a good chance you've seen at least one distutils command:

C:\Tutorial>python setup.py install
"install" is a Distutils command that installs something (typically a Python module or package). The details Distutils needs to do that installation are contained in setup.py (and sometimes other associated files).
"py2exe" is a new Distutils command that is added when you import py2exe. To use py2exe you need to create a setup.py file to tell Distutils and py2exe what you want to do. Here's a setup.py whose simplicity is appropriate for our sample program...

     from distutils.core import setup
     import py2exe
    
     setup(console=['hello.py'])
save it as setup.py and hello.py is your python filename 

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".

Run your setup script

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:\Python27> python setup.py py2exe
running py2exe
creating C:\Python27\build
creating C:\Python27\build\bdist.win32
creating C:\Python27\build\bdist.win32\winexe
creating C:\Python27\build\bdist.win32\winexe\collect-2.7
creating C:\Python27\build\bdist.win32\winexe\bundle-2.7
creating C:\Python27\build\bdist.win32\winexe\temp
creating C:\Python27\dist
*** searching for required modules ***
*** parsing results ***
creating python loader for extension 'unicodedata' (C:\Python27\DLLs\unic
creating python loader for extension 'select' (C:\Python27\DLLs\select.py
creating python loader for extension '_socket' (C:\Python27\DLLs\_socket.
creating python loader for extension '_hashlib' (C:\Python27\DLLs\_hashli
creating python loader for extension '_ssl' (C:\Python27\DLLs\_ssl.pyd ->
creating python loader for extension 'bz2' (C:\Python27\DLLs\bz2.pyd -> b
*** finding dlls needed ***
*** create binaries ***
*** byte compile python files ***
byte-compiling C:\Python27\build\bdist.win32\winexe\temp\_hashlib.py to _
byte-compiling C:\Python27\build\bdist.win32\winexe\temp\_socket.py to _s
byte-compiling C:\Python27\build\bdist.win32\winexe\temp\_ssl.py to _ssl.
byte-compiling C:\Python27\build\bdist.win32\winexe\temp\bz2.py to bz2.py
byte-compiling C:\Python27\build\bdist.win32\winexe\temp\select.py to sel
byte-compiling C:\Python27\build\bdist.win32\winexe\temp\unicodedata.py t
byte-compiling C:\Python27\lib\StringIO.py to StringIO.pyc
byte-compiling C:\Python27\lib\UserDict.py to UserDict.pyc
byte-compiling C:\Python27\lib\__future__.py to __future__.pyc
byte-compiling C:\Python27\lib\_abcoll.py to _abcoll.pyc
byte-compiling C:\Python27\lib\_strptime.py to _strptime.pyc
byte-compiling C:\Python27\lib\_threading_local.py to _threading_local.py
byte-compiling C:\Python27\lib\_weakrefset.py to _weakrefset.pyc
byte-compiling C:\Python27\lib\abc.py to abc.pyc
byte-compiling C:\Python27\lib\atexit.py to atexit.pyc
byte-compiling C:\Python27\lib\base64.py to base64.pyc
byte-compiling C:\Python27\lib\bdb.py to bdb.pyc
byte-compiling C:\Python27\lib\bisect.py to bisect.pyc
byte-compiling C:\Python27\lib\calendar.py to calendar.pyc
byte-compiling C:\Python27\lib\cmd.py to cmd.pyc
byte-compiling C:\Python27\lib\codecs.py to codecs.pyc
byte-compiling C:\Python27\lib\collections.py to collections.pyc
byte-compiling C:\Python27\lib\copy.py to copy.pyc
byte-compiling C:\Python27\lib\copy_reg.py to copy_reg.pyc
byte-compiling C:\Python27\lib\difflib.py to difflib.pyc
byte-compiling C:\Python27\lib\dis.py to dis.pyc
byte-compiling C:\Python27\lib\doctest.py to doctest.pyc
byte-compiling C:\Python27\lib\dummy_thread.py to dummy_thread.pyc
byte-compiling C:\Python27\lib\email\__init__.py to email\__init__.pyc
creating C:\Python27\build\bdist.win32\winexe\collect-2.7\email
byte-compiling C:\Python27\lib\email\_parseaddr.py to email\_parseaddr.py
byte-compiling C:\Python27\lib\email\base64mime.py to email\base64mime.py
byte-compiling C:\Python27\lib\email\charset.py to email\charset.pyc
byte-compiling C:\Python27\lib\email\encoders.py to email\encoders.pyc
byte-compiling C:\Python27\lib\email\errors.py to email\errors.pyc
byte-compiling C:\Python27\lib\email\feedparser.py to email\feedparser.py
byte-compiling C:\Python27\lib\email\generator.py to email\generator.pyc
byte-compiling C:\Python27\lib\email\header.py to email\header.pyc
byte-compiling C:\Python27\lib\email\iterators.py to email\iterators.pyc
byte-compiling C:\Python27\lib\email\message.py to email\message.pyc
byte-compiling C:\Python27\lib\email\mime\__init__.py to email\mime\__ini
creating C:\Python27\build\bdist.win32\winexe\collect-2.7\email\mime
byte-compiling C:\Python27\lib\email\parser.py to email\parser.pyc
byte-compiling C:\Python27\lib\email\quoprimime.py to email\quoprimime.py
byte-compiling C:\Python27\lib\email\utils.py to email\utils.pyc
byte-compiling C:\Python27\lib\encodings\__init__.py to encodings\__init_
creating C:\Python27\build\bdist.win32\winexe\collect-2.7\encodings
byte-compiling C:\Python27\lib\encodings\aliases.py to encodings\aliases.
byte-compiling C:\Python27\lib\encodings\ascii.py to encodings\ascii.pyc
byte-compiling C:\Python27\lib\encodings\base64_codec.py to encodings\bas
byte-compiling C:\Python27\lib\encodings\big5.py to encodings\big5.pyc
byte-compiling C:\Python27\lib\encodings\big5hkscs.py to encodings\big5hk
byte-compiling C:\Python27\lib\encodings\bz2_codec.py to encodings\bz2_co
byte-compiling C:\Python27\lib\encodings\charmap.py to encodings\charmap.
byte-compiling C:\Python27\lib\encodings\cp037.py to encodings\cp037.pyc
byte-compiling C:\Python27\lib\encodings\cp1006.py to encodings\cp1006.py
byte-compiling C:\Python27\lib\encodings\cp1026.py to encodings\cp1026.py
byte-compiling C:\Python27\lib\encodings\cp1140.py to encodings\cp1140.py
byte-compiling C:\Python27\lib\encodings\cp1250.py to encodings\cp1250.py
byte-compiling C:\Python27\lib\encodings\cp1251.py to encodings\cp1251.py
byte-compiling C:\Python27\lib\encodings\cp1252.py to encodings\cp1252.py
byte-compiling C:\Python27\lib\encodings\cp1253.py to encodings\cp1253.py
byte-compiling C:\Python27\lib\encodings\cp1254.py to encodings\cp1254.py
byte-compiling C:\Python27\lib\encodings\cp1255.py to encodings\cp1255.py
byte-compiling C:\Python27\lib\encodings\cp1256.py to encodings\cp1256.py
byte-compiling C:\Python27\lib\encodings\cp1257.py to encodings\cp1257.py
byte-compiling C:\Python27\lib\encodings\cp1258.py to encodings\cp1258.py
byte-compiling C:\Python27\lib\encodings\cp424.py to encodings\cp424.pyc
byte-compiling C:\Python27\lib\encodings\cp437.py to encodings\cp437.pyc
byte-compiling C:\Python27\lib\encodings\cp500.py to encodings\cp500.pyc
byte-compiling C:\Python27\lib\encodings\cp720.py to encodings\cp720.pyc
byte-compiling C:\Python27\lib\encodings\cp737.py to encodings\cp737.pyc
byte-compiling C:\Python27\lib\encodings\cp775.py to encodings\cp775.pyc
byte-compiling C:\Python27\lib\encodings\cp850.py to encodings\cp850.pyc
byte-compiling C:\Python27\lib\encodings\cp852.py to encodings\cp852.pyc
byte-compiling C:\Python27\lib\encodings\cp855.py to encodings\cp855.pyc
byte-compiling C:\Python27\lib\encodings\cp856.py to encodings\cp856.pyc
byte-compiling C:\Python27\lib\encodings\cp857.py to encodings\cp857.pyc
byte-compiling C:\Python27\lib\encodings\cp858.py to encodings\cp858.pyc
byte-compiling C:\Python27\lib\encodings\cp860.py to encodings\cp860.pyc
byte-compiling C:\Python27\lib\encodings\cp861.py to encodings\cp861.pyc
byte-compiling C:\Python27\lib\encodings\cp862.py to encodings\cp862.pyc
byte-compiling C:\Python27\lib\encodings\cp863.py to encodings\cp863.pyc
byte-compiling C:\Python27\lib\encodings\cp864.py to encodings\cp864.pyc
byte-compiling C:\Python27\lib\encodings\cp865.py to encodings\cp865.pyc
byte-compiling C:\Python27\lib\encodings\cp866.py to encodings\cp866.pyc
byte-compiling C:\Python27\lib\encodings\cp869.py to encodings\cp869.pyc
byte-compiling C:\Python27\lib\encodings\cp874.py to encodings\cp874.pyc
byte-compiling C:\Python27\lib\encodings\cp875.py to encodings\cp875.pyc
byte-compiling C:\Python27\lib\encodings\cp932.py to encodings\cp932.pyc
byte-compiling C:\Python27\lib\encodings\cp949.py to encodings\cp949.pyc
byte-compiling C:\Python27\lib\encodings\cp950.py to encodings\cp950.pyc
byte-compiling C:\Python27\lib\encodings\euc_jis_2004.py to encodings\euc
byte-compiling C:\Python27\lib\encodings\euc_jisx0213.py to encodings\euc
byte-compiling C:\Python27\lib\encodings\euc_jp.py to encodings\euc_jp.py
byte-compiling C:\Python27\lib\encodings\euc_kr.py to encodings\euc_kr.py
byte-compiling C:\Python27\lib\encodings\gb18030.py to encodings\gb18030.
byte-compiling C:\Python27\lib\encodings\gb2312.py to encodings\gb2312.py
byte-compiling C:\Python27\lib\encodings\gbk.py to encodings\gbk.pyc
byte-compiling C:\Python27\lib\encodings\hex_codec.py to encodings\hex_co
byte-compiling C:\Python27\lib\encodings\hp_roman8.py to encodings\hp_rom
byte-compiling C:\Python27\lib\encodings\hz.py to encodings\hz.pyc
byte-compiling C:\Python27\lib\encodings\idna.py to encodings\idna.pyc
byte-compiling C:\Python27\lib\encodings\iso2022_jp.py to encodings\iso20
byte-compiling C:\Python27\lib\encodings\iso2022_jp_1.py to encodings\iso
byte-compiling C:\Python27\lib\encodings\iso2022_jp_2.py to encodings\iso
byte-compiling C:\Python27\lib\encodings\iso2022_jp_2004.py to encodings\
byte-compiling C:\Python27\lib\encodings\iso2022_jp_3.py to encodings\iso
byte-compiling C:\Python27\lib\encodings\iso2022_jp_ext.py to encodings\i
byte-compiling C:\Python27\lib\encodings\iso2022_kr.py to encodings\iso20
byte-compiling C:\Python27\lib\encodings\iso8859_1.py to encodings\iso885
byte-compiling C:\Python27\lib\encodings\iso8859_10.py to encodings\iso88
byte-compiling C:\Python27\lib\encodings\iso8859_11.py to encodings\iso88
byte-compiling C:\Python27\lib\encodings\iso8859_13.py to encodings\iso88
byte-compiling C:\Python27\lib\encodings\iso8859_14.py to encodings\iso88
byte-compiling C:\Python27\lib\encodings\iso8859_15.py to encodings\iso88
byte-compiling C:\Python27\lib\encodings\iso8859_16.py to encodings\iso88
byte-compiling C:\Python27\lib\encodings\iso8859_2.py to encodings\iso885
byte-compiling C:\Python27\lib\encodings\iso8859_3.py to encodings\iso885
byte-compiling C:\Python27\lib\encodings\iso8859_4.py to encodings\iso885
byte-compiling C:\Python27\lib\encodings\iso8859_5.py to encodings\iso885
byte-compiling C:\Python27\lib\encodings\iso8859_6.py to encodings\iso885
byte-compiling C:\Python27\lib\encodings\iso8859_7.py to encodings\iso885
byte-compiling C:\Python27\lib\encodings\iso8859_8.py to encodings\iso885
byte-compiling C:\Python27\lib\encodings\iso8859_9.py to encodings\iso885
byte-compiling C:\Python27\lib\encodings\johab.py to encodings\johab.pyc
byte-compiling C:\Python27\lib\encodings\koi8_r.py to encodings\koi8_r.py
byte-compiling C:\Python27\lib\encodings\koi8_u.py to encodings\koi8_u.py
byte-compiling C:\Python27\lib\encodings\latin_1.py to encodings\latin_1.
byte-compiling C:\Python27\lib\encodings\mac_arabic.py to encodings\mac_a
byte-compiling C:\Python27\lib\encodings\mac_centeuro.py to encodings\mac
byte-compiling C:\Python27\lib\encodings\mac_croatian.py to encodings\mac
byte-compiling C:\Python27\lib\encodings\mac_cyrillic.py to encodings\mac
byte-compiling C:\Python27\lib\encodings\mac_farsi.py to encodings\mac_fa
byte-compiling C:\Python27\lib\encodings\mac_greek.py to encodings\mac_gr
byte-compiling C:\Python27\lib\encodings\mac_iceland.py to encodings\mac_
byte-compiling C:\Python27\lib\encodings\mac_latin2.py to encodings\mac_l
byte-compiling C:\Python27\lib\encodings\mac_roman.py to encodings\mac_ro
byte-compiling C:\Python27\lib\encodings\mac_romanian.py to encodings\mac
byte-compiling C:\Python27\lib\encodings\mac_turkish.py to encodings\mac_
byte-compiling C:\Python27\lib\encodings\mbcs.py to encodings\mbcs.pyc
byte-compiling C:\Python27\lib\encodings\palmos.py to encodings\palmos.py
byte-compiling C:\Python27\lib\encodings\ptcp154.py to encodings\ptcp154.
byte-compiling C:\Python27\lib\encodings\punycode.py to encodings\punycod
byte-compiling C:\Python27\lib\encodings\quopri_codec.py to encodings\quo
byte-compiling C:\Python27\lib\encodings\raw_unicode_escape.py to encodin
byte-compiling C:\Python27\lib\encodings\rot_13.py to encodings\rot_13.py
byte-compiling C:\Python27\lib\encodings\shift_jis.py to encodings\shift_
byte-compiling C:\Python27\lib\encodings\shift_jis_2004.py to encodings\s
byte-compiling C:\Python27\lib\encodings\shift_jisx0213.py to encodings\s
byte-compiling C:\Python27\lib\encodings\string_escape.py to encodings\st
byte-compiling C:\Python27\lib\encodings\tis_620.py to encodings\tis_620.
byte-compiling C:\Python27\lib\encodings\undefined.py to encodings\undefi
byte-compiling C:\Python27\lib\encodings\unicode_escape.py to encodings\u
byte-compiling C:\Python27\lib\encodings\unicode_internal.py to encodings
byte-compiling C:\Python27\lib\encodings\utf_16.py to encodings\utf_16.py
byte-compiling C:\Python27\lib\encodings\utf_16_be.py to encodings\utf_16
byte-compiling C:\Python27\lib\encodings\utf_16_le.py to encodings\utf_16
byte-compiling C:\Python27\lib\encodings\utf_32.py to encodings\utf_32.py
byte-compiling C:\Python27\lib\encodings\utf_32_be.py to encodings\utf_32
byte-compiling C:\Python27\lib\encodings\utf_32_le.py to encodings\utf_32
byte-compiling C:\Python27\lib\encodings\utf_7.py to encodings\utf_7.pyc
byte-compiling C:\Python27\lib\encodings\utf_8.py to encodings\utf_8.pyc
byte-compiling C:\Python27\lib\encodings\utf_8_sig.py to encodings\utf_8_
byte-compiling C:\Python27\lib\encodings\uu_codec.py to encodings\uu_code
byte-compiling C:\Python27\lib\encodings\zlib_codec.py to encodings\zlib_
byte-compiling C:\Python27\lib\fnmatch.py to fnmatch.pyc
byte-compiling C:\Python27\lib\ftplib.py to ftplib.pyc
byte-compiling C:\Python27\lib\functools.py to functools.pyc
byte-compiling C:\Python27\lib\genericpath.py to genericpath.pyc
byte-compiling C:\Python27\lib\getopt.py to getopt.pyc
byte-compiling C:\Python27\lib\getpass.py to getpass.pyc
byte-compiling C:\Python27\lib\gettext.py to gettext.pyc
byte-compiling C:\Python27\lib\hashlib.py to hashlib.pyc
byte-compiling C:\Python27\lib\heapq.py to heapq.pyc
byte-compiling C:\Python27\lib\httplib.py to httplib.pyc
byte-compiling C:\Python27\lib\inspect.py to inspect.pyc
byte-compiling C:\Python27\lib\keyword.py to keyword.pyc
byte-compiling C:\Python27\lib\linecache.py to linecache.pyc
byte-compiling C:\Python27\lib\locale.py to locale.pyc
byte-compiling C:\Python27\lib\logging\__init__.py to logging\__init__.py
creating C:\Python27\build\bdist.win32\winexe\collect-2.7\logging
byte-compiling C:\Python27\lib\mimetools.py to mimetools.pyc
byte-compiling C:\Python27\lib\mimetypes.py to mimetypes.pyc
byte-compiling C:\Python27\lib\ntpath.py to ntpath.pyc
byte-compiling C:\Python27\lib\nturl2path.py to nturl2path.pyc
byte-compiling C:\Python27\lib\opcode.py to opcode.pyc
byte-compiling C:\Python27\lib\optparse.py to optparse.pyc
byte-compiling C:\Python27\lib\os.py to os.pyc
byte-compiling C:\Python27\lib\os2emxpath.py to os2emxpath.pyc
byte-compiling C:\Python27\lib\pdb.py to pdb.pyc
byte-compiling C:\Python27\lib\pickle.py to pickle.pyc
byte-compiling C:\Python27\lib\posixpath.py to posixpath.pyc
byte-compiling C:\Python27\lib\pprint.py to pprint.pyc
byte-compiling C:\Python27\lib\quopri.py to quopri.pyc
byte-compiling C:\Python27\lib\random.py to random.pyc
byte-compiling C:\Python27\lib\re.py to re.pyc
byte-compiling C:\Python27\lib\repr.py to repr.pyc
byte-compiling C:\Python27\lib\rfc822.py to rfc822.pyc
byte-compiling C:\Python27\lib\shlex.py to shlex.pyc
byte-compiling C:\Python27\lib\socket.py to socket.pyc
byte-compiling C:\Python27\lib\sre.py to sre.pyc
byte-compiling C:\Python27\lib\sre_compile.py to sre_compile.pyc
byte-compiling C:\Python27\lib\sre_constants.py to sre_constants.pyc
byte-compiling C:\Python27\lib\sre_parse.py to sre_parse.pyc
byte-compiling C:\Python27\lib\ssl.py to ssl.pyc
byte-compiling C:\Python27\lib\stat.py to stat.pyc
byte-compiling C:\Python27\lib\string.py to string.pyc
byte-compiling C:\Python27\lib\stringprep.py to stringprep.pyc
byte-compiling C:\Python27\lib\struct.py to struct.pyc
byte-compiling C:\Python27\lib\subprocess.py to subprocess.pyc
byte-compiling C:\Python27\lib\tempfile.py to tempfile.pyc
byte-compiling C:\Python27\lib\textwrap.py to textwrap.pyc
byte-compiling C:\Python27\lib\threading.py to threading.pyc
byte-compiling C:\Python27\lib\token.py to token.pyc
byte-compiling C:\Python27\lib\tokenize.py to tokenize.pyc
byte-compiling C:\Python27\lib\traceback.py to traceback.pyc
byte-compiling C:\Python27\lib\types.py to types.pyc
byte-compiling C:\Python27\lib\unittest\__init__.py to unittest\__init__.
creating C:\Python27\build\bdist.win32\winexe\collect-2.7\unittest
byte-compiling C:\Python27\lib\unittest\case.py to unittest\case.pyc
byte-compiling C:\Python27\lib\unittest\loader.py to unittest\loader.pyc
byte-compiling C:\Python27\lib\unittest\main.py to unittest\main.pyc
byte-compiling C:\Python27\lib\unittest\result.py to unittest\result.pyc
byte-compiling C:\Python27\lib\unittest\runner.py to unittest\runner.pyc
byte-compiling C:\Python27\lib\unittest\signals.py to unittest\signals.py
byte-compiling C:\Python27\lib\unittest\suite.py to unittest\suite.pyc
byte-compiling C:\Python27\lib\unittest\util.py to unittest\util.pyc
byte-compiling C:\Python27\lib\urllib.py to urllib.pyc
byte-compiling C:\Python27\lib\urlparse.py to urlparse.pyc
byte-compiling C:\Python27\lib\uu.py to uu.pyc
byte-compiling C:\Python27\lib\warnings.py to warnings.pyc
byte-compiling C:\Python27\lib\weakref.py to weakref.pyc
*** copy extensions ***
copying C:\Python27\DLLs\_hashlib.pyd -> C:\Python27\dist
copying C:\Python27\DLLs\_socket.pyd -> C:\Python27\dist
copying C:\Python27\DLLs\_ssl.pyd -> C:\Python27\dist
copying C:\Python27\DLLs\bz2.pyd -> C:\Python27\dist
copying C:\Python27\DLLs\select.pyd -> C:\Python27\dist
copying C:\Python27\DLLs\unicodedata.pyd -> C:\Python27\dist
*** copy dlls ***
copying C:\Python27\w9xpopen.exe -> C:\Python27\dist
copying C:\WINDOWS\system32\python27.dll -> C:\Python27\dist
setting sys.winver for 'C:\Python27\dist\python27.dll' to 'py2exe'
copying C:\Python27\lib\site-packages\py2exe\run.exe -> C:\Python27\dist\
The following modules appear to be missing
['_scproxy']

*** binary dependencies ***
Your executable(s) also depend on these dlls which are not included,
you may or may not need to distribute them.

Make sure you have the license if you distribute any of them, and
make sure you don't distribute files belonging to the operating system.

   WS2_32.dll - C:\WINDOWS\system32\WS2_32.dll
   SHELL32.dll - C:\WINDOWS\system32\SHELL32.dll
   USER32.dll - C:\WINDOWS\system32\USER32.dll
   ADVAPI32.dll - C:\WINDOWS\system32\ADVAPI32.dll
   KERNEL32.dll - C:\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.

Test your executable

Now that the package has been created it is ready to test: (the exe file will be in dist folder)
C:\Tutorial>cd dist

C:\Tutorial\dist>hello.exe
Hello World
Excellent, it works!!!

Providing the Microsoft Visual C runtime DLL

The Python interpreter was compiled using Microsoft Visual C, so your new program needs the Microsoft Visual C runtime DLL to run. If you have installed appropriate versions of Python or Visual Studio, then you will already have this DLL on your computer. If some of your users might not already have this DLL, then they will not be able to run your program. The methods you may use to solve this depend on the version of Python you are using:

 Python 2.4 or 2.5

If you are using Python 2.4 or 2.5, then the DLL you need is called MSVCR71.dll. This DLL will probably already have been included in your dist directory, in which case you need do nothing more.
However, the copyright on this file is owned by Microsoft, and you need to check whether you have the legal right to redistribute it. If you have a copy of Visual Studio, check the file redist.txt provided within the installation to see whether you have redistribution rights for this DLL. Generally you have the right to redistribute it if you own a license for Microsoft Visual C++, but not if you use the Express Editions.
If you do not have the rights to redistribute MSVCR71.dll, then your users must install it for themselves, using the Microsoft Visual C++ 2005 Redistributable Package (vcredist_x86.exe).
Either you can instruct your users to download and run this themselves, or you could create an installer for your application (see Step 6 below), that includes vcredist_x86.exe (which is itself redistributable by anyone), and then run that as part of your application installation.

 Python 2.6, 2.7, 3.0, 3.1

For Python 2.6, the DLL you need is called MSVCR90.dll. Py2exe is not able to automatically include this DLL in your dist directory, so you must provide it yourself.
To complicate things, there is more than one version of this DLL in existance, each with the same filename. You need the same version that the Python interpreter was compiled with, which is version 9.0.21022.8. Through the remainder of these instructions, hover 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.

 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:
  from glob import glob
  data_files = [("Microsoft.VC90.CRT", glob(r'C:\Program Files\Microsoft Visual Studio 9.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:\\Program Files\\Microsoft Visual Studio 9.0\\VC\\redist\\x86\\Microsoft.VC90.CRT")

win32ui special case

win32ui needs MFC DLLs to run .exe, see Py2exeAndWin32ui for extra informations

Running the redistributable C runtime installer

If you don't have rights to redistribute MSVCR90.dll, then your users may install it on their machine by running the Microsoft Visual C++ 2008 Redistributable Package (vcredist_x86.exe). It is important not to use the SP1 version of this installer, which contains the wrong version of MSVCR90.dll.
Either you can instruct  users to download and run this themselves, or you could create an installer for your application , that includes vcredist_x86.exe (which is itself redistributable by anyone), and then run that as part of your application installation.
The installer puts a copy of the DLLs in the directory C:\WINDOWS\WinSxS (XP), inside subdirectories with mangled names. The manifest file is in the 'Manifests' subdirectory, again this will have a mangled filename. You can still discern the text 'Microsoft.VC90.CRT' and '9.0.21022.8' within the mangled file and directory names, to find the files. It is possible to take a copy of these files and remove the filename mangling, to embed them in your application as described in 5.2.1.

6. Build an installer if applicable

py2exe is not an installer builder - it merely assembles the files needed to run your Python program. There are plenty of good installer builders out there including some that are open source (e.g., NSIS) and some that are free (e.g., Inno Setup).

Reactions:

0 comments:

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More