PyInstaller 打包 Python 源码为 exe 可执行文件

最近用 Python 写的一个爬虫运行在 Windows 系统中,为了方便启动程序,考虑将 Python 源文件打包成 exe 可执行文件。
目前发现的工具主要有两种,一是 PyInstaller ,另一个是 py2exe 。当然如果不怕麻烦还可也选择手动编译的方式。
软件环境:Windows 7, Python 2.7。


1.PyInstaller:


1.1 简介

通过 PyInstaller 官网 http://www.pyinstaller.org/ 的介绍可以知道,它能够将 Python 程序转化成为可以在 Windows, Linux, Mac OS X, Solaris 和 AIX 下独立运行的程序。与同类的转换工具相比,它的主要优势在于可以支持从 Python 2.4 以后的所有版本,通过透明压缩技术创建更小的可执行文件,完整的支持多平台,并使用操作系统支持来载入动态库以确保完好的兼容。

1.2 安装

目前 PyInstaller 的发布版是 2.1,可以通过 https://pypi.python.org/packages/source/P/PyInstaller/PyInstaller-2.1.zip 下载。
将下载后的文件拷贝到系统的任意位置,解压即可。

在 Windows 中,PyInstaller 需要 PyWin32 或者 pypiwin32 Python extension。如果使用 pip 安装 PyInstaller 并且缺少 PyWin32 ,pypiwin32 会被自动安装。PyWin32 下载:http://sourceforge.net/projects/pywin32/

1.3 使用

使用 PyInstaller 时,在 cmd 下进入解压出来的目录,执行如下命令。

python pyinstaller [options] script [script ...] | specfile

General Options

参数   说明
-h –help Print a summary of the options and exit.
-v –version Print the version of the program as 2.n.

Options for the Executable Output:

参数   说明
-n name –name=name Give a name for the specfile and the executable output. The default is the basename of the first script.
-D –onedir Create a folder name containing an executable name and all support files. This is the default.
创建一个目录包含 exe 文件和所有支持文件。该参数默认。
-F –onefile Create a single executable file name (or name .exe or name .app).
打包成一个可执行的 exe 或 app 文件。
-c –console,–nowindowed Set up a console subsystem for standard input/output at run time. This is the default for both one-file and one-folder modes.
-w –windowed, –noconsole On Windows and Mac OS X, do not create a console window at run time for standard input/output. (This option is ignored for other operating systems.) On Mac OS X, this option triggers the creation of an OS X application bundle.
-d –debug Cause the bootloader to issue progress messages as it initializes at run time. See Getting Debug Messages below.
-s –strip Apply strip (symbol table stripper to reduce file size) to the executable and all shared libraries. Not recommended with the cygwin package on Windows, which tends to render normal Win32 dlls unusable.

更详细的使用方法请参考解压包里 doc 目录下的 Manual 文件。

1.4 说明

使用 PyInstaller 的 Windows 系统是需要带有 Python 环境的,打包后的 exe 程序就不需要了哦。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值