Monday, 17 July 2017

convert python programs to standalone windows exe file

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 youve installed third party Python modules then theres a good chance youve 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. Heres 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. Lets go through it line by line...
  1. When working with py2exe the only part of Distutils well typically need to reference directly is the setup function, so thats all well 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 uild
creating C:Python27 uild dist.win32
creating C:Python27 uild dist.win32winexe
creating C:Python27 uild dist.win32winexecollect-2.7
creating C:Python27 uild dist.win32winexe undle-2.7
creating C:Python27 uild dist.win32winexe emp
creating C:Python27dist
*** searching for required modules ***
*** parsing results ***
creating python loader for extension unicodedata (C:Python27DLLsunic
creating python loader for extension select (C:Python27DLLsselect.py
creating python loader for extension _socket (C:Python27DLLs_socket.
creating python loader for extension _hashlib (C:Python27DLLs_hashli
creating python loader for extension _ssl (C:Python27DLLs_ssl.pyd ->
creating python loader for extension bz2 (C:Python27DLLs z2.pyd -> b
*** finding dlls needed ***
*** create binaries ***
*** byte compile python files ***
byte-compiling C:Python27 uild dist.win32winexe emp_hashlib.py to _
byte-compiling C:Python27 uild dist.win32winexe emp_socket.py to _s
byte-compiling C:Python27 uild dist.win32winexe emp_ssl.py to _ssl.
byte-compiling C:Python27 uild dist.win32winexe emp z2.py to bz2.py
byte-compiling C:Python27 uild dist.win32winexe empselect.py to sel
byte-compiling C:Python27 uild dist.win32winexe empunicodedata.py t
byte-compiling C:Python27libStringIO.py to StringIO.pyc
byte-compiling C:Python27libUserDict.py to UserDict.pyc
byte-compiling C:Python27lib__future__.py to __future__.pyc
byte-compiling C:Python27lib_abcoll.py to _abcoll.pyc
byte-compiling C:Python27lib_strptime.py to _strptime.pyc
byte-compiling C:Python27lib_threading_local.py to _threading_local.py
byte-compiling C:Python27lib_weakrefset.py to _weakrefset.pyc
byte-compiling C:Python27libabc.py to abc.pyc
byte-compiling C:Python27libatexit.py to atexit.pyc
byte-compiling C:Python27lib ase64.py to base64.pyc
byte-compiling C:Python27lib db.py to bdb.pyc
byte-compiling C:Python27lib isect.py to bisect.pyc
byte-compiling C:Python27libcalendar.py to calendar.pyc
byte-compiling C:Python27libcmd.py to cmd.pyc
byte-compiling C:Python27libcodecs.py to codecs.pyc
byte-compiling C:Python27libcollections.py to collections.pyc
byte-compiling C:Python27libcopy.py to copy.pyc
byte-compiling C:Python27libcopy_reg.py to copy_reg.pyc
byte-compiling C:Python27libdifflib.py to difflib.pyc
byte-compiling C:Python27libdis.py to dis.pyc
byte-compiling C:Python27libdoctest.py to doctest.pyc
byte-compiling C:Python27libdummy_thread.py to dummy_thread.pyc
byte-compiling C:Python27libemail__init__.py to email__init__.pyc
creating C:Python27 uild dist.win32winexecollect-2.7email
byte-compiling C:Python27libemail_parseaddr.py to email_parseaddr.py
byte-compiling C:Python27libemail ase64mime.py to email ase64mime.py
byte-compiling C:Python27libemailcharset.py to emailcharset.pyc
byte-compiling C:Python27libemailencoders.py to emailencoders.pyc
byte-compiling C:Python27libemailerrors.py to emailerrors.pyc
byte-compiling C:Python27libemailfeedparser.py to emailfeedparser.py
byte-compiling C:Python27libemailgenerator.py to emailgenerator.pyc
byte-compiling C:Python27libemailheader.py to emailheader.pyc
byte-compiling C:Python27libemailiterators.py to emailiterators.pyc
byte-compiling C:Python27libemailmessage.py to emailmessage.pyc
byte-compiling C:Python27libemailmime__init__.py to emailmime__ini
creating C:Python27 uild dist.win32winexecollect-2.7emailmime
byte-compiling C:Python27libemailparser.py to emailparser.pyc
byte-compiling C:Python27libemailquoprimime.py to emailquoprimime.py
byte-compiling C:Python27libemailutils.py to emailutils.pyc
byte-compiling C:Python27libencodings__init__.py to encodings__init_
creating C:Python27 uild dist.win32winexecollect-2.7encodings
byte-compiling C:Python27libencodingsaliases.py to encodingsaliases.
byte-compiling C:Python27libencodingsascii.py to encodingsascii.pyc
byte-compiling C:Python27libencodings ase64_codec.py to encodings as
byte-compiling C:Python27libencodings ig5.py to encodings ig5.pyc
byte-compiling C:Python27libencodings ig5hkscs.py to encodings ig5hk
byte-compiling C:Python27libencodings z2_codec.py to encodings z2_co
byte-compiling C:Python27libencodingscharmap.py to encodingscharmap.
byte-compiling C:Python27libencodingscp037.py to encodingscp037.pyc
byte-compiling C:Python27libencodingscp1006.py to encodingscp1006.py
byte-compiling C:Python27libencodingscp1026.py to encodingscp1026.py
byte-compiling C:Python27libencodingscp1140.py to encodingscp1140.py
byte-compiling C:Python27libencodingscp1250.py to encodingscp1250.py
byte-compiling C:Python27libencodingscp1251.py to encodingscp1251.py
byte-compiling C:Python27libencodingscp1252.py to encodingscp1252.py
byte-compiling C:Python27libencodingscp1253.py to encodingscp1253.py
byte-compiling C:Python27libencodingscp1254.py to encodingscp1254.py
byte-compiling C:Python27libencodingscp1255.py to encodingscp1255.py
byte-compiling C:Python27libencodingscp1256.py to encodingscp1256.py
byte-compiling C:Python27libencodingscp1257.py to encodingscp1257.py
byte-compiling C:Python27libencodingscp1258.py to encodingscp1258.py
byte-compiling C:Python27libencodingscp424.py to encodingscp424.pyc
byte-compiling C:Python27libencodingscp437.py to encodingscp437.pyc
byte-compiling C:Python27libencodingscp500.py to encodingscp500.pyc
byte-compiling C:Python27libencodingscp720.py to encodingscp720.pyc
byte-compiling C:Python27libencodingscp737.py to encodingscp737.pyc
byte-compiling C:Python27libencodingscp775.py to encodingscp775.pyc
byte-compiling C:Python27libencodingscp850.py to encodingscp850.pyc
byte-compiling C:Python27libencodingscp852.py to encodingscp852.pyc
byte-compiling C:Python27libencodingscp855.py to encodingscp855.pyc
byte-compiling C:Python27libencodingscp856.py to encodingscp856.pyc
byte-compiling C:Python27libencodingscp857.py to encodingscp857.pyc
byte-compiling C:Python27libencodingscp858.py to encodingscp858.pyc
byte-compiling C:Python27libencodingscp860.py to encodingscp860.pyc
byte-compiling C:Python27libencodingscp861.py to encodingscp861.pyc
byte-compiling C:Python27libencodingscp862.py to encodingscp862.pyc
byte-compiling C:Python27libencodingscp863.py to encodingscp863.pyc
byte-compiling C:Python27libencodingscp864.py to encodingscp864.pyc
byte-compiling C:Python27libencodingscp865.py to encodingscp865.pyc
byte-compiling C:Python27libencodingscp866.py to encodingscp866.pyc
byte-compiling C:Python27libencodingscp869.py to encodingscp869.pyc
byte-compiling C:Python27libencodingscp874.py to encodingscp874.pyc
byte-compiling C:Python27libencodingscp875.py to encodingscp875.pyc
byte-compiling C:Python27libencodingscp932.py to encodingscp932.pyc
byte-compiling C:Python27libencodingscp949.py to encodingscp949.pyc
byte-compiling C:Python27libencodingscp950.py to encodingscp950.pyc
byte-compiling C:Python27libencodingseuc_jis_2004.py to encodingseuc
byte-compiling C:Python27libencodingseuc_jisx0213.py to encodingseuc
byte-compiling C:Python27libencodingseuc_jp.py to encodingseuc_jp.py
byte-compiling C:Python27libencodingseuc_kr.py to encodingseuc_kr.py
byte-compiling C:Python27libencodingsgb18030.py to encodingsgb18030.
byte-compiling C:Python27libencodingsgb2312.py to encodingsgb2312.py
byte-compiling C:Python27libencodingsgbk.py to encodingsgbk.pyc
byte-compiling C:Python27libencodingshex_codec.py to encodingshex_co
byte-compiling C:Python27libencodingshp_roman8.py to encodingshp_rom
byte-compiling C:Python27libencodingshz.py to encodingshz.pyc
byte-compiling C:Python27libencodingsidna.py to encodingsidna.pyc
byte-compiling C:Python27libencodingsiso2022_jp.py to encodingsiso20
byte-compiling C:Python27libencodingsiso2022_jp_1.py to encodingsiso
byte-compiling C:Python27libencodingsiso2022_jp_2.py to encodingsiso
byte-compiling C:Python27libencodingsiso2022_jp_2004.py to encodings
byte-compiling C:Python27libencodingsiso2022_jp_3.py to encodingsiso
byte-compiling C:Python27libencodingsiso2022_jp_ext.py to encodingsi
byte-compiling C:Python27libencodingsiso2022_kr.py to encodingsiso20
byte-compiling C:Python27libencodingsiso8859_1.py to encodingsiso885
byte-compiling C:Python27libencodingsiso8859_10.py to encodingsiso88
byte-compiling C:Python27libencodingsiso8859_11.py to encodingsiso88
byte-compiling C:Python27libencodingsiso8859_13.py to encodingsiso88
byte-compiling C:Python27libencodingsiso8859_14.py to encodingsiso88
byte-compiling C:Python27libencodingsiso8859_15.py to encodingsiso88
byte-compiling C:Python27libencodingsiso8859_16.py to encodingsiso88
byte-compiling C:Python27libencodingsiso8859_2.py to encodingsiso885
byte-compiling C:Python27libencodingsiso8859_3.py to encodingsiso885
byte-compiling C:Python27libencodingsiso8859_4.py to encodingsiso885
byte-compiling C:Python27libencodingsiso8859_5.py to encodingsiso885
byte-compiling C:Python27libencodingsiso8859_6.py to encodingsiso885
byte-compiling C:Python27libencodingsiso8859_7.py to encodingsiso885
byte-compiling C:Python27libencodingsiso8859_8.py to encodingsiso885
byte-compiling C:Python27libencodingsiso8859_9.py to encodingsiso885
byte-compiling C:Python27libencodingsjohab.py to encodingsjohab.pyc
byte-compiling C:Python27libencodingskoi8_r.py to encodingskoi8_r.py
byte-compiling C:Python27libencodingskoi8_u.py to encodingskoi8_u.py
byte-compiling C:Python27libencodingslatin_1.py to encodingslatin_1.
byte-compiling C:Python27libencodingsmac_arabic.py to encodingsmac_a
byte-compiling C:Python27libencodingsmac_centeuro.py to encodingsmac
byte-compiling C:Python27libencodingsmac_croatian.py to encodingsmac
byte-compiling C:Python27libencodingsmac_cyrillic.py to encodingsmac
byte-compiling C:Python27libencodingsmac_farsi.py to encodingsmac_fa
byte-compiling C:Python27libencodingsmac_greek.py to encodingsmac_gr
byte-compiling C:Python27libencodingsmac_iceland.py to encodingsmac_
byte-compiling C:Python27libencodingsmac_latin2.py to encodingsmac_l
byte-compiling C:Python27libencodingsmac_roman.py to encodingsmac_ro
byte-compiling C:Python27libencodingsmac_romanian.py to encodingsmac
byte-compiling C:Python27libencodingsmac_turkish.py to encodingsmac_
byte-compiling C:Python27libencodingsmbcs.py to encodingsmbcs.pyc
byte-compiling C:Python27libencodingspalmos.py to encodingspalmos.py
byte-compiling C:Python27libencodingsptcp154.py to encodingsptcp154.
byte-compiling C:Python27libencodingspunycode.py to encodingspunycod
byte-compiling C:Python27libencodingsquopri_codec.py to encodingsquo
byte-compiling C:Python27libencodings aw_unicode_escape.py to encodin
byte-compiling C:Python27libencodings ot_13.py to encodings ot_13.py
byte-compiling C:Python27libencodingsshift_jis.py to encodingsshift_
byte-compiling C:Python27libencodingsshift_jis_2004.py to encodingss
byte-compiling C:Python27libencodingsshift_jisx0213.py to encodingss
byte-compiling C:Python27libencodingsstring_escape.py to encodingsst
byte-compiling C:Python27libencodings is_620.py to encodings is_620.
byte-compiling C:Python27libencodingsundefined.py to encodingsundefi
byte-compiling C:Python27libencodingsunicode_escape.py to encodingsu
byte-compiling C:Python27libencodingsunicode_internal.py to encodings
byte-compiling C:Python27libencodingsutf_16.py to encodingsutf_16.py
byte-compiling C:Python27libencodingsutf_16_be.py to encodingsutf_16
byte-compiling C:Python27libencodingsutf_16_le.py to encodingsutf_16
byte-compiling C:Python27libencodingsutf_32.py to encodingsutf_32.py
byte-compiling C:Python27libencodingsutf_32_be.py to encodingsutf_32
byte-compiling C:Python27libencodingsutf_32_le.py to encodingsutf_32
byte-compiling C:Python27libencodingsutf_7.py to encodingsutf_7.pyc
byte-compiling C:Python27libencodingsutf_8.py to encodingsutf_8.pyc
byte-compiling C:Python27libencodingsutf_8_sig.py to encodingsutf_8_
byte-compiling C:Python27libencodingsuu_codec.py to encodingsuu_code
byte-compiling C:Python27libencodingszlib_codec.py to encodingszlib_
byte-compiling C:Python27libfnmatch.py to fnmatch.pyc
byte-compiling C:Python27libftplib.py to ftplib.pyc
byte-compiling C:Python27libfunctools.py to functools.pyc
byte-compiling C:Python27libgenericpath.py to genericpath.pyc
byte-compiling C:Python27libgetopt.py to getopt.pyc
byte-compiling C:Python27libgetpass.py to getpass.pyc
byte-compiling C:Python27libgettext.py to gettext.pyc
byte-compiling C:Python27libhashlib.py to hashlib.pyc
byte-compiling C:Python27libheapq.py to heapq.pyc
byte-compiling C:Python27libhttplib.py to httplib.pyc
byte-compiling C:Python27libinspect.py to inspect.pyc
byte-compiling C:Python27libkeyword.py to keyword.pyc
byte-compiling C:Python27liblinecache.py to linecache.pyc
byte-compiling C:Python27liblocale.py to locale.pyc
byte-compiling C:Python27liblogging__init__.py to logging__init__.py
creating C:Python27 uild dist.win32winexecollect-2.7logging
byte-compiling C:Python27libmimetools.py to mimetools.pyc
byte-compiling C:Python27libmimetypes.py to mimetypes.pyc
byte-compiling C:Python27lib tpath.py to ntpath.pyc
byte-compiling C:Python27lib turl2path.py to nturl2path.pyc
byte-compiling C:Python27libopcode.py to opcode.pyc
byte-compiling C:Python27liboptparse.py to optparse.pyc
byte-compiling C:Python27libos.py to os.pyc
byte-compiling C:Python27libos2emxpath.py to os2emxpath.pyc
byte-compiling C:Python27libpdb.py to pdb.pyc
byte-compiling C:Python27libpickle.py to pickle.pyc
byte-compiling C:Python27libposixpath.py to posixpath.pyc
byte-compiling C:Python27libpprint.py to pprint.pyc
byte-compiling C:Python27libquopri.py to quopri.pyc
byte-compiling C:Python27lib andom.py to random.pyc
byte-compiling C:Python27lib e.py to re.pyc
byte-compiling C:Python27lib epr.py to repr.pyc
byte-compiling C:Python27lib fc822.py to rfc822.pyc
byte-compiling C:Python27libshlex.py to shlex.pyc
byte-compiling C:Python27libsocket.py to socket.pyc
byte-compiling C:Python27libsre.py to sre.pyc
byte-compiling C:Python27libsre_compile.py to sre_compile.pyc
byte-compiling C:Python27libsre_constants.py to sre_constants.pyc
byte-compiling C:Python27libsre_parse.py to sre_parse.pyc
byte-compiling C:Python27libssl.py to ssl.pyc
byte-compiling C:Python27libstat.py to stat.pyc
byte-compiling C:Python27libstring.py to string.pyc
byte-compiling C:Python27libstringprep.py to stringprep.pyc
byte-compiling C:Python27libstruct.py to struct.pyc
byte-compiling C:Python27libsubprocess.py to subprocess.pyc
byte-compiling C:Python27lib empfile.py to tempfile.pyc
byte-compiling C:Python27lib extwrap.py to textwrap.pyc
byte-compiling C:Python27lib hreading.py to threading.pyc
byte-compiling C:Python27lib oken.py to token.pyc
byte-compiling C:Python27lib okenize.py to tokenize.pyc
byte-compiling C:Python27lib raceback.py to traceback.pyc
byte-compiling C:Python27lib ypes.py to types.pyc
byte-compiling C:Python27libunittest__init__.py to unittest__init__.
creating C:Python27 uild dist.win32winexecollect-2.7unittest
byte-compiling C:Python27libunittestcase.py to unittestcase.pyc
byte-compiling C:Python27libunittestloader.py to unittestloader.pyc
byte-compiling C:Python27libunittestmain.py to unittestmain.pyc
byte-compiling C:Python27libunittest esult.py to unittest esult.pyc
byte-compiling C:Python27libunittest unner.py to unittest unner.pyc
byte-compiling C:Python27libunittestsignals.py to unittestsignals.py
byte-compiling C:Python27libunittestsuite.py to unittestsuite.pyc
byte-compiling C:Python27libunittestutil.py to unittestutil.pyc
byte-compiling C:Python27liburllib.py to urllib.pyc
byte-compiling C:Python27liburlparse.py to urlparse.pyc
byte-compiling C:Python27libuu.py to uu.pyc
byte-compiling C:Python27libwarnings.py to warnings.pyc
byte-compiling C:Python27libweakref.py to weakref.pyc
*** copy extensions ***
copying C:Python27DLLs_hashlib.pyd -> C:Python27dist
copying C:Python27DLLs_socket.pyd -> C:Python27dist
copying C:Python27DLLs_ssl.pyd -> C:Python27dist
copying C:Python27DLLs z2.pyd -> C:Python27dist
copying C:Python27DLLsselect.pyd -> C:Python27dist
copying C:Python27DLLsunicodedata.pyd -> C:Python27dist
*** copy dlls ***
copying C:Python27w9xpopen.exe -> C:Python27dist
copying C:WINDOWSsystem32python27.dll -> C:Python27dist
setting sys.winver for C:Python27distpython27.dll to py2exe
copying C:Python27libsite-packagespy2exe un.exe -> C:Python27dist
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 dont distribute files belonging to the operating system.

WS2_32.dll - C:WINDOWSsystem32WS2_32.dll
SHELL32.dll - C:WINDOWSsystem32SHELL32.dll
USER32.dll - C:WINDOWSsystem32USER32.dll
ADVAPI32.dll - C:WINDOWSsystem32ADVAPI32.dll
KERNEL32.dll - C:WINDOWSsystem32KERNEL32.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:Tutorialdist>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 youve got. Youll 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 dont 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 edistx86Microsoft.VC90.CRT. Since Visual Studio 2008, you cant just copy this DLL file - you also need the manifest file that youll 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 projects dist directory, in a subdirectory called Microsoft.VC90.CRT. To achieve this, add a data_files option to your projects setup.py:
 from glob import glob
data_files = [("Microsoft.VC90.CRT", glob(rC:Program FilesMicrosoft Visual Studio 9.0VC edistx86Microsoft.VC90.CRT*.*))]
setup(
data_files=data_files,
etc
)
download
alternative link download

Like the Post? Do share with your Friends.