Win32使用PyInstaller打包Python程序

PyInstaller可以将Python程序打包成Windows(当然也包括Linux, Mac OS X, Solaris and AIX)下可执行的EXE文件,目前支持python2.2-2.7版本,点击这里下载。

 

【解压/安装】

1、使用PyInstaller需要安装PyWin32

可到这里下载相应的版本。(从pywin32的下载量看,还是Python2.7使用更广泛)


2、下载、解压PyInstaller-2.1

从http://www.pyinstaller.org/下载最新的PyInstaller软件包,当前的最新版本是PyInstaller-2.1。

(或者使用pip安装也可,执行pip install pyinstaller)


PyInstaller-2.1是一个可以免安装的软件包,直接解压即可

PyInstaller-2.1.tar.gz

$ tar -zxvf PyInstaller-2.1.tar.gz


下载upx(可选)

到http://upx.sourceforge.net/下载upx,解压后把upx放在PyInstaller目录下,upx的作用是给生成的exe加壳,减小体积。


直接运行下面的命令,查看帮助信息:

cd PyInstaller-2.1

python pyinstaller.py -h

C:\PyInstaller-2.1>python pyinstaller.py -h
Usage: pyinstaller.py [opts] <scriptname> [ <scriptname> ...] | <specfile>

Options:
  -h, --help            show this help message and exit
  -v, --version         Show program version info and exit.
  --distpath=DIR        Where to put the bundled app (default:
                        C:\PyInstaller-2.1\dist)
  --workpath=WORKPATH   Where to put all the temporary work files, .log, .pyz
                        and etc. (default: C:\PyInstaller-2.1\build)
  -y, --noconfirm       Replace output directory (default:
                        SPECPATH\dist\SPECNAME) without asking for
                        confirmation
  --upx-dir=UPX_DIR     Path to UPX utility (default: search the execution
                        path)
  -a, --ascii           Do not include unicode encoding support (default:
                        included if available)
  --clean               Clean PyInstaller cache and remove temporary files
                        before building.
  --log-level=LOGLEVEL  Amount of detail in build-time console messages
                        (default: INFO, choose one of DEBUG, INFO, WARN,
                        ERROR, CRITICAL)

  What to generate:
    -F, --onefile       Create a one-file bundled executable.
    -D, --onedir        Create a one-folder bundle containing an executable
                        (default)
    --specpath=DIR      Folder to store the generated spec file (default:
                        current directory)
    -n NAME, --name=NAME
                        Name to assign to the bundled app and spec file
                        (default: first script's basename)

  What to bundle, where to search:
    -p DIR, --paths=DIR
                        A path to search for imports (like using PYTHONPATH).
                        Multiple paths are allowed, separated by ';', or use
                        this option multiple times
    --hidden-import=MODULENAME
                        Name an import not visible in the code of the
                        script(s). This option can be used multiple times.
    --additional-hooks-dir=HOOKSPATH
                        An additional path to search for hooks. This option
                        can be used multiple times.
    --runtime-hook=RUNTIME_HOOKS
                        Path to a custom runtime hook file. A runtime hook is
                        code that is bundled with the executable and is
                        executed before any other code or module to set up
                        special features of the runtime environment. This
                        option can be used multiple times.

  How to generate:
    -d, --debug         Tell the bootloader to issue progress messages while
                        initializing and starting the bundled app. Used to
                        diagnose problems with missing imports.
    -s, --strip         Apply a symbol-table strip to the executable and
                        shared libs (not recommended for Windows)
    --noupx             Do not use UPX even if it is available (works
                        differently between Windows and *nix)

  Windows and Mac OS X specific options:
    -c, --console, --nowindowed
                        Open a console window for standard i/o (default)
    -w, --windowed, --noconsole
                        Windows and Mac OS X: do not provide a console window
                        for standard i/o. On Mac OS X this also triggers
                        building an OS X .app bundle.This option is ignored in
                        *NIX systems.
    -i FILE.ico or FILE.exe,ID or FILE.icns, --icon=FILE.ico or FILE.exe,ID or FILE.icns
                        FILE.ico: apply that icon to a Windows executable.
                        FILE.exe,ID, extract the icon with ID from an exe.
                        FILE.icns: apply the icon to the .app bundle on Mac OS
                        X

  Windows specific options:
    --version-file=FILE
                        add a version resource from FILE to the exe
    -m FILE or XML, --manifest=FILE or XML
                        add manifest FILE or XML to the exe
    -r FILE[,TYPE[,NAME[,LANGUAGE]]], --resource=FILE[,TYPE[,NAME[,LANGUAGE]]]
                        Add or update a resource of the given type, name and
                        language from FILE to a Windows executable. FILE can
                        be a data file or an exe/dll. For data files, at least
                        TYPE and NAME must be specified. LANGUAGE defaults to
                        0 or may be specified as wildcard * to update all
                        resources of the given TYPE and NAME. For exe/dll
                        files, all resources from FILE will be added/updated
                        to the final executable if TYPE, NAME and LANGUAGE are
                        omitted or specified as wildcard *.This option can be
                        used multiple times.

  Obsolete options (not used anymore):
    -X, -K, -C, -o, --upx, --tk, --configfile, --skip-configure, --out, --buildpath
                        These options do not exist anymore.


可以看到pyinstaller各种封装的功能


【使用方法】

我们主要看--onefile这个选项(即:-F)

例如,我们有一个脚本放在C:\Users\goopand\pyWork\src目录下

genPasswd.py

#!/usr/bin/env python
# -*- coding: utf-8 -*-
__author__ = 'Goopand'


import string
import random

def genPassword(length=8,chars=string.digits+string.ascii_letters):
    return ''.join(random.sample(chars*10,8))

if __name__=="__main__":
    for i in range(20):
        print genPassword(8)

执行以下命令,会在src目录下生成:genPasswd.spec文件、dist目录、build目录:

C:\Users\goopand\pyWork\src> python C:\PyInstaller-2.1\pyinstaller.py -F genPasswd.py

再执行以下命令,会在dist目录下生成genPasswd.exe可执行文件:

C:\Users\goopand\pyWork\src> python C:\PyInstaller-2.1\pyinstaller.py genPasswd.spec


这个可执行文件就可以独立运行了

备注:实际测试发现如果两个环境的python版本不相同的话,可能运行会有问题



【注意事项】

  • 网上教程常见的-X选项启用upx已经失效

  • 如果upx.exe已经复制到PyInstaller文件夹下,会默认使用upx,如果不在文件夹下,可以使用--upx-dir选项,如--upx-dir=upx_dir,如--upx-dir=/usr/local/share/

  • 如果upx.exe复制到了PyInstaller文件夹下,如果不想使用upx,需要添加参数 --noupx


【常用参数】

-F 用于制作独立的可执行程序

--upx-dir 使用upx加壳从而压缩exe文件

--noconsole适用于Windows和Mac OS X,用于创建不显示控制台窗口的程序


【其他】

PyInstaller可以用于多个平台的打包,包括 Windows (32-bit and 64-bit),Linux (32-bit and 64-bit),Mac OS X (32-bit and 64-bit),experimentally Solaris and AIX。

PyInstaller也可以自定义ico文件等,完整使用手册参见http://pythonhosted.org/PyInstaller/

一般在Windows 32位环境下打包Python程序,以便移植到X86-64的Windows环境,保证兼容性。

你可能感兴趣的:(Win32使用PyInstaller打包Python程序)