python程序改exe_使用PyInstaller——Python程序转换为EXE

下载对应已安装的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程序路径以及输出文件路径。

更详细的解释如下:

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

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

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

运行: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=

add file.ico to the executable's resources. (Windows only)

--icon=

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文件细节,点这里。

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

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

【完】

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值