Python 3 使用的是一种叫做 WiX Toolset 安装包格式。吐个槽:目前只见过 Python 用这种安装包格式
WiX Toolset 提供了一个工具 dark.exe 可以解开这种格式的安装包
将 Python 安装包解压至当前目录
dark python-3.6.0-amd64.exe -x %cd%
生成了两个文件夹:AttachedContainer 和 UX
UX 目录对我们来说没用,Python 的安装文件在 AttachedContainer 目录下的一下几个文件中
core.msi
dev.msi
doc.msi
exe.msi
lib.msi
tcltk.msi
test.msi
tools.msi
.msi 的包可以用 Windows 自带的 msiexec 解开
整个过程重复步骤挺多的,也很无聊,所以写了一个批处理
Python3deploy.cmd
=================
dark %1 -x %cd%
rd /s /q UX
move %cd%\AttachedContainer\core.msi core.msi
move %cd%\AttachedContainer\dev.msi dev.msi
move %cd%\AttachedContainer\doc.msi doc.msi
move %cd%\AttachedContainer\exe.msi exe.msi
move %cd%\AttachedContainer\lib.msi lib.msi
move %cd%\AttachedContainer\tcltk.msi tcltk.msi
move %cd%\AttachedContainer\test.msi test.msi
move %cd%\AttachedContainer\tools.msi tools.msi
rd /s /q AttachedContainer
msiexec /a core.msi /qb TARGETDIR=%cd%\Python
msiexec /a dev.msi /qb TARGETDIR=%cd%\Python
msiexec /a doc.msi /qb TARGETDIR=%cd%\Python
msiexec /a exe.msi /qb TARGETDIR=%cd%\Python
msiexec /a lib.msi /qb TARGETDIR=%cd%\Python
msiexec /a tcltk.msi /qb TARGETDIR=%cd%\Python
msiexec /a test.msi /qb TARGETDIR=%cd%\Python
msiexec /a tools.msi /qb TARGETDIR=%cd%\Python
del /f /s /q core.msi dev.msi doc.msi exe.msi lib.msi tcltk.msi test.msi tools.msi
pause
保存后,直接将 Python 的安装包拖到批处理上就会自动开始解包
Python 3.5.2 解包后安装了 pip 重新打包成 .zip 格式 27.3 MB
Python 3.6.0 解包后 重新打包成 .zip 格式 26.0 MB
Python 2 的安装包是 .msi 格式的,只需一行命令即可
msiexec /a python-2.7.13.amd64.msi /qb TARGETDIR=%cd%\Python2
设置系统环境变量
以下仅对当前运行的命令提示符窗口有效
set path=...\Python;...\Python\Scripts;%path%
set PATHEXT=%PATHEXT%;.PY
以下会直接修改注册表,需要以管理员权限执行
setx Path "...\Python;...\Python\Scripts;%path%" /m
setx PATHEXT "%PATHEXT%;.PY" /m
可以打开新的命令提示符窗口输入
echo %path%
echo %PATHEXT%
查看结果
设置文件关联
以下需要以管理员权限执行
assoc .py=Python.File
assoc .pyc=Python.CompiledFile
assoc .pyo=Python.CompiledFile
assoc .pyw=Python.NoConFile
ftype Python.CompiledFile="...\Python\python.exe" "%1" %*
ftype Python.File="...\Python\python.exe" "%1" %*
ftype Python.NoConFile="...\Python\pythonw.exe" "%1" %*
查看 Python 的文件关联
assoc | findstr -i python
ftype | findstr -i python