將py產生為exe檔案

來源:互聯網
上載者:User
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.pyfrom distutils.core import setupimport py2exesetup(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 (except the system dlls).

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.pyfrom distutils.core import setupimport globimport py2exesetup(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.pyfrom distutils.core import setupimport py2exesetup(service=["MyService"])

The built service executables 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.pyfrom distutils.core import setupimport py2exesetup(com_server=["win32com.server.interp"])

By default both DLL and EXE servers are built, you should simply delete those you don't need.

The bundle option

By default py2exe creates these files in the dist directory which you must deploy:

  1. One (or more) .exe files
  2. The python##.dll
  3. A couple of .pyd files which are the compiled extensions that the exe files need, plus any other .dll files that the extensions need.
  4. A library.zip file which contains the compiled pure python modules as .pyc or .pyo files (if you have specified 'zipfile=None' in the setup script this file is append to the .exe files and not present in the dist-directory).

The --bundle <level> or -b <level> command line switch will create less files because binary extensions, runtime dlls, and even the Python-dll itself is bundled into the executable itself, or inside the library-archive if you prefer that.

The bundled pyds and dlls are never unpacked to the file system, instead they are transparently loaded at runtime from the bundle. The resulting executable appears to be statically linked.

Specifying a level of 2 includes the .pyd and .dll files into the zip-archive or the executable. Thus, the dist directory will contain your exe file(s), the library.zip file (if you haven't specified 'zipfile=None'), and the python dll. The advantage of this scheme is that the application can still load extension modules from the file system if you extend sys.path at runtime.

Using a level of 1 includes the .pyd and .dll files into the zip-archive or the executable itself, and does the same for pythonXY.dll. The advantage is that you only need to distribute one file per exe, which will however be quite large. Another advantage is that inproc COM servers will run completely isolated from other Python interpreters in the same exe. The disadvantage of this scheme is that it is impossible to load other extensions from the file system, the application will crash with a fatal Python error if you try this. I have still to find a way to prevent this and raise an ImportError instead - any suggestions how this can be implemented would be very welcome.

The bundle-option has been tested with some popular extensions, but of course there's no guarantee that any extension will work in bundled form - be sure to test the executable (which you should do anyway).

The bundle option achieves its magic by code which emulates the Windows LoadLibrary api, it is compiled into the exe-stubs that py2exe uses. For experimentation, it is also installed as a normal Python extension _memimporter.pyd in the lib/site-packages directory. The Python module zipextimported.py in the same directory demonstrates how it can be used to load binary extensions from zip-files.

Samples

The py2exe-installer installs some examples into the lib/site-packages/py2exe/samples directory, demonstrating several simple and advances features.

The singlefile subdirectory contains two samples which are built as single-file executables: a trivial wxPython gui program, and a pywin32 dll COM server module.

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 pyds and dlls 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.6.3.win32-py2.3.exe (for Python 2.3), or py2exe-0.6.3.win32-py2.4.exe (for Python 2.4).

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.

If you use py2exe to build COM clients or servers, win32all build 163 (or later) is strongly recommened - it contains much better support for frozen executables.

本文來自 www.py2exe.org

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.