pyinstaller打包python

有一个很好用,而且免费的工具pyinstaller,支持windows, linux, mac os, 并且支持32bit, 64bit。

官网地址:

http://www.pyinstaller.org/


在windows下面,这个工具依赖pywin32,所以使用这个工具之前需要先行安装pywin32。如果是在linux或者macos下面就不需要了。

可以从下面的连接下载:

http://sourceforge.net/projects/pywin32/files/pywin32/Build%20217/


pywin32是python package,所以需要安装到site-packages这个文件中。


安装完成之后,我们下载pyinstaller,这是个压缩文件,下载连接:

https://github.com/downloads/pyinstaller/pyinstaller/pyinstaller-2.0.zip


因为这不是python package,所以不需要安装,直接解压的任何目录就可以工作了。解压之后需要配置pyinstaller,cd到你解压的文件夹,然后运行这个命令:

python Configure.py

其实就是写config.dat这个文件。


这个命令一般只需要执行一次,每次打包的时候不需要执行它。


更详细的资料请参考

http://www.pyinstaller.org/export/d3398dd79b68901ae1edd761f3fe0f4ff19cfb1a/project/doc/Manual.html


2个命令完成一个简单的打包:

python Makespec.py [--onefile] yourprogram.py
python Build.py specfile


第一个命令是根据你提供的选项来生成一个工程的表述文件,包括编译选项,非常想Makefile文件,这里的--onefile就是告诉pyinstaller,我们想最终生成一个exe文件(windows 下面)。specfile就是第一条命令生成spec文件,这里需要指定全路径。

pyinstaller还提供其他的一些选项:

-F, --onefileproduce a single file deployment (see below).
-D, --onedirproduce a single directory deployment (default).
-K, --tkinclude TCL/TK in the deployment.
-a, --asciido not include encodings. The default (on Python versions with unicodesupport) is now to include all encodings.
-d, --debuguse debug (verbose) versions of the executables.
-w, --windowed, --noconsole
 Use the Windows subsystem executable, which does not openthe console when the program is launched.(Windows only)
-c, --nowindowed, --console
 Use the console subsystem executable. This is the default. (Windows only)
-s, --stripthe executable and all shared libraries will be run through strip. Notethat cygwin's strip tends to render normal Win32 dlls unusable.
-X, --upxif you have UPX installed (detected by Configure), this will use it tocompress your executable (and, on Windows, your dlls). See note below.
-o DIR,--out=DIR
 create the spec file in directory. If not specified, and the currentdirectory is Installer's root directory, an output subdirectory will becreated. Otherwise the current directory is used.
-p DIR,--paths=DIR
 set base path for import (like using PYTHONPATH). Multiple directories areallowed, separating them with the path separator (';' under Windows, ':'under Linux), or using this option multiple times.
--icon=<FILE.ICO>
 add file.ico to the executable's resources. (Windows only)
--icon=<FILE.EXE,N>
 add the n-th incon in file.exe to the executable's resources.(Windowsonly)
-v FILE,--version=FILE
 add verfile as a version resource to the executable. (Windows only)
-n NAME,--name=NAME
 optional name to assign to the project (from which the spec file name isgenerated). If omitted, the basename of the (first) script is used.


第二条命令就是根据刚才的生成工程描述文件,生成最终可执行程序。从某种角度来说,和gcc的编译非常像,第一条命令像是在 configure,第二个命令 像是 make。

第二条命令可以带 -O选项(大写的O),来打开优化选项,进行编译打包。


python -O Build.py specfile


但是会提示你出错,那是因为我们在配置pyinstaller时,没有指定打开优化选项,所以我们配置命令变成:

python -O configure.py


你只需要输入你的程序入点文件,其他pyinstaller会帮你找。这里说入点文件就是你的python工程第一个要执行的文件,就像c/c++工程中含有main函数的文件。在上面的例子中就是

yourprogam.py


这个工具会把所有的依赖的包给你打进去,包括python解释器,因此打成包,可以在没有安装python解释器的环境中运行。



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值