1. 程式人生 > >將py生成為exe檔案

將py生成為exe檔案

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.