Python 文件打包成exe

2018-06-03  本文已影响0人  大梦想家_0317

两种工具将.py文件打包成exe:

  1. PyInstaller
  2. cx_Freeze
Tool Windows Linux MacOS Python2 Python3 One file mode
PyInstaller Yes Yes Yes Yes Yes Yes
cx_Freeze Yes Yes Yes Yes Yes No

一. PyInstaller (Windows)

  1. 适用版本
    python2 & python3
  2. 安装方法

安装pywin32:
下载安装PyInstaller运行前需要安装windows拓展---pywin32
pywin32下载地址: http://sourceforge.net/projects/pywin32/files/pywin32/

安装PyInstaller:

pip install PyInstaller

验证安装成功与否:

# python 环境下导入PyInstaller成功,则安装成功
import PyInstaller

# 或者CMD环境中
pyinstaller --version
  1. 使用方法
# CMD环境中同目录下
# 生成的test.exe文件在当前dist文件夹中
pyinstaller test.py

# 生成单文件模式
pyinstaller -F test.py

# 改变生成test.exe的图标
# test.ico是与test.py同目录下的图标文件名
pyinstaller -F --icon=test.ico test.py
  1. 参数介绍
    PyInstaller的语法:pyinstaller [options] script [script...] | specfile
参数 含义
-F 指定打包后只生成一个exe格式的文件
-D –onedir 创建一个目录,包含exe文件,但会依赖很多文件(默认选项)
-c –console, –nowindowed 使用控制台,无界面(默认)
-w –windowed, –noconsole 使用窗口,无控制台
-i 改变生成程序的icon图标
-p 添加搜索路径,让其找到对应的库(一般用不到)
  1. 参考
    https://blog.csdn.net/zengxiantao1994/article/details/76578421
    https://www.pyinstaller.org/

二. cx_Freeze (Windows)

  1. 适用版本
    python2 & python3
  2. 安装方法
# step 1:
pip install cx_Freeze

# step 2: CMD环境中 cd 到python\scripts目录下 run以下脚本
python cxfreeze-postinstall

验证安装成功与否:

# CMD 环境中 cd 到python\scripts目录下
# 如果出现help等信息则安装成功
cxfreeze -h

# python 环境下导入cx_Freeze成功,则安装成功
import cx_Freeze

# CMD 环境中 cd 到python\scripts目录下
# 如果出现版本信息则安装成功 
cxfreeze --version
  1. 使用方法
# CMD 环境中
cxfreeze "C:Test\test.py" --target-dir="C:\Test" --target-name="test.exe" --include-modules="test" --icon="test.ico"
  1. 参数介绍
    cx_Freeze的语法:cxfreeze [options] script [script...] | specfile
参数 含义
"C:Test\test.py" 需要打包的主文件
--target-dir 打包后的程序路径
--target-name 打包后的程序名
--include-modules 包含的模块或库
--icon 改变生成程序的icon图标
  1. 参考
    http://cx-freeze.readthedocs.io/en/latest/index.html
    https://www.jianshu.com/p/e47d9be6fa96
上一篇下一篇

猜你喜欢

热点阅读