使用PyInstaller——Python程序转换为EXE

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

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

 

下载对应已安装的Python版本的PyInstaller版本,解压到任意目录,按照提供的manual文档进行即可。假设要转换的Python代码为txexe\hello.py.进入PyInstaller安装根目录,执行以下命令。

?
python Configure.py #仅第一次执行
python Makespec.py toexe\hello.py #生成的hello.spec文件存默认放在当前目录的hello文件夹下
python Build.py hello\hello.spec #生成的exe文件在当前目录下的hello\dist文件夹下

我们也可以指定Python程序路径以及输出文件路径。

更详细的解释如下:

第一步:Configuring your PyInstaller setup

pyinstaller安装目录下cmd运行:python Configure.py

基于当前系统配置PyInstaller,如果更换了Python版本,需要重新下载相应版本的PyInstaller并重新执行Configure.py

 注意关注运行过程中的警告和错误,最好没有警告和错误,如果出现找不到某dll,可以下载后放到C:\Windows\system32下,一般都能解决。

第二步:Create a spec file for your project

运行:python Makespec.py hello.py

生成要转换的脚本的spec文件,告诉PyInstaller创建一个包含主要的可执行文件和动态库。可以通过-o选项指定输出spec文件的路径:

Makespec.py的可用参数:

-F--onefile produce a single file deployment (see below).
-D--onedir produce a single directory deployment (default).
-K--tk include TCL/TK in the deployment.
-a--ascii do not include encodings. The default (on Python versions with unicode support) is now to include all encodings.
-d--debug use debug (verbose) versions of the executables.
-w--windowed--noconsole
  Use the Windows subsystem executable, which does not open the console when the program is launched. (Windows only)
-c--nowindowed--console
  Use the console subsystem executable. This is the default. (Windows only)
-s--strip the executable and all shared libraries will be run through strip. Note that cygwin's strip tends to render normal Win32 dlls unusable.
-X--upx if you have UPX installed (detected by Configure), this will use it to compress 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 current directory is Installer's root directory, an output subdirectory will be created. Otherwise the current directory is used.
-p DIR--paths=DIR
  set base path for import (like using PYTHONPATH). Multiple directories are allowed, 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. (Windows only)
-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 is generated). If omitted, the basename of the (first) script is used.

常用参数:

-F    制作独立的可执行程序
-D    制作出的档案存放在同一个文件夹下(默认值)

-K    包含TCL/TK(对于使用了TK的,最好加上这个选项,否则在未安装TK的电脑上无法运行)
-w     制作窗口程序
-c    制作命令行程序(默认)
-X    制作使用UPX压缩过的可执行程序(推荐使用这个选项,需要下载UPX包,解压后upx.exe放在Python(非PyInstaller)安装目录下,下载upx308w.zip

-o DIR  指定输出SPEC文件路径(这也决定了最后输出的exe文件路径)
--icon=[ICO文件路径] 指定程序图标
-v [指定文件] 指定程序版本信息
-n [指定程序名] 指定程序名称

关于SPEC文件细节,点这里


第三步:Build your project

运行:python Build.py hello\hello.spec

使用上一步得到的spec文件Build,产生的文件放在spec当前目录,有两个文件夹,build是该步骤建立的工程文件,dist存放最后输出的exe文件:hello.exe.

 这里还有一个关于PyInstaller的介绍,很不错:http://bytes.com/topic/python/insights/579554-simple-guide-using-pyinstaller

【完】

PyInstaller是一个用于将Python应用程序转换为独立可执行文件的工具,它通过spec(specification)文件来控制打包过程。当你想要利用UPX压缩技术进行打包时,可以在spec文件中添加相关的设置。 首先,你需要安装UPX压缩工具。在命令行运行`pip install upx`。 然后,在创建或编辑spec文件(通常命名为`your_script_name.spec`)的过程中,找到`a.datas`部分,这用于指定哪些文件需要包含在最终的可执行文件中。在该部分,你可以添加一行类似这样的内容: ```python datas = [('your_file.py', '.'), ('upx.exe', 'your_package_path')], ``` 这里`'your_file.py'`是你希望包含的Python源文件,`'.'`表示当前目录,`'upx.exe'`是UPX压缩工具的路径,而`'your_package_path'`是UPX放在其中以便在执行时使用的路径(通常是`sys._MEIPASS`,这是PyInstaller为动态库临时提供的位置)。 接下来,为了启用UPX压缩,你需要在`options`字典中添加`--upx-dir`和`--upx-exe`键,它们分别指定了UPX工具的路径: ```python options = { 'upx-dir': '/path/to/upx', # UPX的安装路径 'upx-exe': 'upx.exe', # 如果UPX的名称不是默认的 } ``` 最后,在`Analysis`部分,添加`strip=False`来防止PyInstaller自动删除UPX标志,因为UPX可能会改变可执行文件的结构: ```python analysis = Analysis( ['your_script_name.py'], options=options, ... strip=False, # 这里添加这一行 ) ``` 保存并运行`pyinstaller your_script_name.spec`命令,UPX将会被应用于打包后的可执行文件。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值