Python绿色版制作实践总结

一、版本

1. 版本分类

Python 版本分为两大流派,两个流派的版本是不通用的,不仅所使用的编码不同,语法上也发生了变化,因此要注意程序所支持的版本。

  • Python 2.x 版本,2020年1月1日,Python 官方终止了对 Python 2.7 版本(最后一个2.x版本)的支持。
  • Python 3.x 版本,版本还在不断迭代更新。
  • 在同一大版本下,基本通用,除非开发者做出特别说明所支持的最低版本,否则无需最新Python版本。本文的绿化方法适用于 3.x 版本。

    3. 绿色便携版

    Python 的绿色便携版有两种:官方 Embeddable 版本(嵌入式版);安装版制作的绿色版。

  • Embeddable 版适用于需要将 Python 集成到其他应用程序或项目中的情况。它不包含图形界面的安装程序,只提供了 Python 解释器和必要的库文件。
  • 安装版包含了 Python 解释器、标准库和其他一些附加组件。它提供了一个图形化的安装界面,可以让用户选择安装的组件,并自动配置系统路径等设置。
  • Embeddable 版虽然也可以通过移植或安装库以支持正常或者更多的应用,但比较麻烦,因此并不适合普通用户以及日常使用。至少不移植Tkinter的话基于该库开发的GUI工具软件无法显示图形界面。

    安装版则完整的多,支持更多使用场景,更适合普通用户及日常使用,因此适合制作普通用户使用的绿色版。


    二、制作

    1. 下载安装

    官网下载:Python Releases for Windows | Python.org

    选则 Stable Releases 下的 Windows installer (64-bit) (即稳定、64位安装版),具体版本根据需要选择,一般选最新版即可。

    Python 的安装很简单,无需多说,但这里有一个大坑,强烈建议不安装 PIP(原因后面再说)。在功能选则界面取消 PIP 的勾选,如下图:

    其他功能组件根据自己的需要选择安装,简单说明一下各组件的功能:

  • Documentation – 官方文档,全英文,不学习编程的话不用安装。
  • pip – Python 的通用包管理工具,可以很方便地下载软件包。
  • Tcl/TK and IDLE – GUI(图形用户界面)编程库和集成开发环境。这个一定要安装,GUI程序需要用到。
  • Python test suite – Python 自带的测试套件。不编程的话无需安装。
  • py launcher – Windows 的 Python 启动器。这个建议安装,可以双击 .pyw 文件直接打开GUI程序。
  • 安装完成后,复制或打包压缩一份备用。如果有条件的的话,建议安装这一步在虚拟机中进行。

    2. 提取精简

    1)复制 Python 安装目录到合适的地方(下面以 D:\Program\Python 为例)。

    2)到 %LOCALAPPDATA%\Programs\Python 目录下,复制 Launcher 文件夹至 D:\Program\Python 。

    3)可以适当做一些精简,如用于二次开发的SDK目录 include、Libs ,用于编写代码的 IDLE 等。根据个人使用请看酌情删减,也可以等到熟悉 Python 后再精简。

    最后,如果是实体机安装的话,卸载掉之前安装的 Python ,接下来就在 D:\Program\Python 的基础上操作。

    3. 环境变量及文件关联

    Python主要有两个需加入系统环境变量 Path ,最基本的是主程序的路径(Python 目录),其次是应用程序的路径(Python\Scripts 目录)。

    如果是单用户的电脑,只要加入用户变量即可;如果是多用户的电脑,需要加入系统变量中。

    不加入环境变量也可以,但每次运行程序就需要进入 Python 目录手动执行,或用快捷方式/批处理指定 Python 路径,比较麻烦,因此建议设置环境变量。

    绿色版仅配置环境变量,是无法双击运行脚本程序的。因为缺失默认打开方式,需要修改注册表或用工具设置文件关联。后面附有一键执行的批处理,所以此处略过具体操作。

    三、路径问题

    其实上面的操作都完成之后,就是一个绿色版的 Python,如果没有问题的话,就可以正常使用。但最大的问题是,Python 在安装时把绝对路径写入了 PIP 文件和一大堆缓存文件,如下图:

    虽然绝大多数都是缓存文件,可以直接清理掉,但 Scripts 目录下 PIP 相关的文件必须手动修改。修改方法可以参考星沉地动大神的文章,用十六进制编辑器打开 pip.exe ,在文件尾部找到与图中相似的绝对路径,按图索骥修改即可。只要修改 pip.exe 和 wheel.exe,其他两个 pip*.exe 都是 pip.exe 改个名字而已。

    但这种方法一是麻烦,一不小心就会出错;二是更新PIP后就会失效。在多次实验后,找到一个很简单的方法,就是安装时不要安装 PIP(如果安装了,可以删掉),之后通过 get-pip-py 安装即可。这个方法也方便 PIP 出问题(如误删文件导致 PIP 无法运行等)后必须重新安装的情况。

    到 Get-pip 官网(https://pip.pypa.io/en/stable/installation)下载 get-pip.py(右键点击该链接另存为),放到 Python 目录下(其实任意位置都可以),运行后即可自动下载并安装 PIP。这样既无需修改其路径,也可以随时删除后重新安装(后面附有删除批处理)。

    解决路径问题后,Python 的绿化便携基本完成。总结一下过程:

  • 安装 Python,复制安装目录备用;
  • 复制 Launcher 文件夹到备份目录;
  • 卸载 Python;
  • 清理备份目录中的 __pycache__ 文件夹(都在 Lib 目录下);
  • 下载 get-pip.py ,安装 pip;
  • 设置环境变量,添加 Python 和 Python\Scripts 到 PATH;
  • 设置文件关联;
  • 编写批处理文件,自动执行各种操作。
  • 四、批处理

    说明:

  • 为了目录整洁(强迫症),所有批处理和 get-pip.py 都放到 Python\Install_Set 目录下,因此批处理中会使用 cd.. 命令。如果是放在 Python 目录下的话,请删除 cd.. 。
  • 有关环境变量的添加、删除均使用了自动判断,可酌情修改。
  • 文件关联的注册表修改了 Launcher 路径为 Python 目录下,如果有变动,请自行修改。
  • 1. 安装批处理:Inst_Python.cmd

    @echo off & cd /d %~dp0
    cd..
    ::文件关联
    reg add "HKCU\SOFTWARE\Classes\.py" /f /ve /t REG_SZ /d "Python.File"
    reg add "HKCU\SOFTWARE\Classes\.py" /f /v "Content Type" /t REG_SZ /d "text/x-python"
    reg add "HKCU\SOFTWARE\Classes\.pyc" /f /ve /t REG_SZ /d "Python.CompiledFile"
    reg add "HKCU\SOFTWARE\Classes\.pyd" /f /ve /t REG_SZ /d "Python.Extension"
    reg add "HKCU\SOFTWARE\Classes\.pyo" /f /ve /t REG_SZ /d "Python.CompiledFile"
    reg add "HKCU\SOFTWARE\Classes\.pyw" /f /ve /t REG_SZ /d "Python.NoConFile"
    reg add "HKCU\SOFTWARE\Classes\.pyw" /f /v "Content Type" /t REG_SZ /d "text/x-python"
    reg add "HKCU\SOFTWARE\Classes\.pyz" /f /ve /t REG_SZ /d "Python.ArchiveFile"
    reg add "HKCU\SOFTWARE\Classes\.pyz" /f /v "Content Type" /t REG_SZ /d "application/x-zip-compressed"
    reg add "HKCU\SOFTWARE\Classes\.pyzw" /f /ve /t REG_SZ /d "Python.NoConArchiveFile"
    reg add "HKCU\SOFTWARE\Classes\.pyzw" /f /v "Content Type" /t REG_SZ /d "application/x-zip-compressed"
    reg add "HKCU\SOFTWARE\Classes\CLSID\{BEA218D2-6950-497B-9434-61683EC065FE}\InprocServer32" /f /ve /t REG_SZ /d "%CD%\Launcher\pyshellext.amd64.dll"
    reg add "HKCU\SOFTWARE\Classes\CLSID\{BEA218D2-6950-497B-9434-61683EC065FE}\InprocServer32" /f /v "ThreadingModel" /t REG_SZ /d "Apartment"
    reg add "HKCU\SOFTWARE\Classes\Python.ArchiveFile" /f /ve /t REG_SZ /d "Python Zip Application File"
    reg add "HKCU\SOFTWARE\Classes\Python.ArchiveFile\DefaultIcon" /f /ve /t REG_SZ /d "\"%CD%\Launcher\py.exe\",5"
    reg add "HKCU\SOFTWARE\Classes\Python.ArchiveFile\shell\open\command" /f /ve /t REG_SZ /d "\"%CD%\Launcher\py.exe\" \"%%L\" %%*"
    reg add "HKCU\SOFTWARE\Classes\Python.ArchiveFile\shellex\DropHandler" /f /ve /t REG_SZ /d "{BEA218D2-6950-497B-9434-61683EC065FE}"
    reg add "HKCU\SOFTWARE\Classes\Python.CompiledFile" /f /ve /t REG_SZ /d "Compiled Python File"
    reg add "HKCU\SOFTWARE\Classes\Python.CompiledFile\DefaultIcon" /f /ve /t REG_SZ /d "\"%CD%\Launcher\py.exe\",2"
    reg add "HKCU\SOFTWARE\Classes\Python.CompiledFile\shell\open\command" /f /ve /t REG_SZ /d "\"%CD%\Launcher\py.exe\" \"%%L\" %%*"
    reg add "HKCU\SOFTWARE\Classes\Python.CompiledFile\shellex\DropHandler" /f /ve /t REG_SZ /d "{BEA218D2-6950-497B-9434-61683EC065FE}"
    reg add "HKCU\SOFTWARE\Classes\Python.Extension" /f /ve /t REG_SZ /d "Python Extension Module"
    reg add "HKCU\SOFTWARE\Classes\Python.Extension\DefaultIcon" /f /ve /t REG_SZ /d "\"%CD%\Launcher\py.exe\",3"
    reg add "HKCU\SOFTWARE\Classes\Python.File" /f /ve /t REG_SZ /d "Python File"
    reg add "HKCU\SOFTWARE\Classes\Python.File\DefaultIcon" /f /ve /t REG_SZ /d "\"%CD%\Launcher\py.exe\",1"
    reg add "HKCU\SOFTWARE\Classes\Python.File\Shell\editwithidle" /f /v "MUIVerb" /t REG_SZ /d "&Edit with IDLE"
    reg add "HKCU\SOFTWARE\Classes\Python.File\Shell\editwithidle" /f /v "Subcommands" /t REG_SZ /d ""
    reg add "HKCU\SOFTWARE\Classes\Python.File\Shell\editwithidle\shell\edit312" /f /v "MUIVerb" /t REG_SZ /d "Edit with IDLE 3.12 (64-bit)"
    reg add "HKCU\SOFTWARE\Classes\Python.File\Shell\editwithidle\shell\edit312\command" /f /ve /t REG_SZ /d "\"%CD%\pythonw.exe\" -m idlelib \"%%L\" %%*"
    reg add "HKCU\SOFTWARE\Classes\Python.File\Shell\open\command" /f /ve /t REG_SZ /d "\"%CD%\Launcher\py.exe\" \"%%L\" %%*"
    reg add "HKCU\SOFTWARE\Classes\Python.File\shellex\DropHandler" /f /ve /t REG_SZ /d "{BEA218D2-6950-497B-9434-61683EC065FE}"
    reg add "HKCU\SOFTWARE\Classes\Python.NoConArchiveFile" /f /ve /t REG_SZ /d "Python Zip Application File (no console)"
    reg add "HKCU\SOFTWARE\Classes\Python.NoConArchiveFile\DefaultIcon" /f /ve /t REG_SZ /d "\"%CD%\Launcher\py.exe\",5"
    reg add "HKCU\SOFTWARE\Classes\Python.NoConArchiveFile\shell\open\command" /f /ve /t REG_SZ /d "\"%CD%\Launcher\pyw.exe\" \"%%L\" %%*"
    reg add "HKCU\SOFTWARE\Classes\Python.NoConArchiveFile\shellex\DropHandler" /f /ve /t REG_SZ /d "{BEA218D2-6950-497B-9434-61683EC065FE}"
    reg add "HKCU\SOFTWARE\Classes\Python.NoConFile" /f /ve /t REG_SZ /d "Python File (no console)"
    reg add "HKCU\SOFTWARE\Classes\Python.NoConFile\DefaultIcon" /f /ve /t REG_SZ /d "\"%CD%\Launcher\py.exe\",1"
    reg add "HKCU\SOFTWARE\Classes\Python.NoConFile\Shell\editwithidle" /f /v "MUIVerb" /t REG_SZ /d "&Edit with IDLE"
    reg add "HKCU\SOFTWARE\Classes\Python.NoConFile\Shell\editwithidle" /f /v "Subcommands" /t REG_SZ /d ""
    reg add "HKCU\SOFTWARE\Classes\Python.NoConFile\Shell\editwithidle\shell\edit312" /f /v "MUIVerb" /t REG_SZ /d "Edit with IDLE 3.12 (64-bit)"
    reg add "HKCU\SOFTWARE\Classes\Python.NoConFile\Shell\editwithidle\shell\edit312\command" /f /ve /t REG_SZ /d "\"%CD%\pythonw.exe\" -m idlelib \"%%L\" %%*"
    reg add "HKCU\SOFTWARE\Classes\Python.NoConFile\Shell\open\command" /f /ve /t REG_SZ /d "\"%CD%\Launcher\pyw.exe\" \"%%L\" %%*"
    reg add "HKCU\SOFTWARE\Classes\Python.NoConFile\shellex\DropHandler" /f /ve /t REG_SZ /d "{BEA218D2-6950-497B-9434-61683EC065FE}"
    reg add "HKCU\SOFTWARE\Python\PyLauncher" /f /ve /t REG_SZ /d "%CD%\Launcher\py.exe"
    reg add "HKCU\SOFTWARE\Python\PyLauncher" /f /v "AssociateFiles" /t REG_DWORD /d 1
    reg add "HKCU\SOFTWARE\Python\PyLauncher" /f /v "InstallDir" /t REG_SZ /d "%CD%\Launcher\\"
    reg add "HKCU\SOFTWARE\Python\PythonCore" /f /v "DisplayName" /t REG_SZ /d "Python Software Foundation"
    reg add "HKCU\SOFTWARE\Python\PythonCore" /f /v "SupportUrl" /t REG_SZ /d "https://www.python.org/"
    reg add "HKCU\SOFTWARE\Python\PythonCore\3.12" /f /v "DisplayName" /t REG_SZ /d "Python 3.12 (64-bit)"
    reg add "HKCU\SOFTWARE\Python\PythonCore\3.12" /f /v "SupportUrl" /t REG_SZ /d "https://www.python.org/"
    reg add "HKCU\SOFTWARE\Python\PythonCore\3.12" /f /v "SysArchitecture" /t REG_SZ /d "64bit"
    reg add "HKCU\SOFTWARE\Python\PythonCore\3.12" /f /v "SysVersion" /t REG_SZ /d "3.12"
    reg add "HKCU\SOFTWARE\Python\PythonCore\3.12" /f /v "Version" /t REG_SZ /d "3.12.3"
    reg add "HKCU\SOFTWARE\Python\PythonCore\3.12\Idle" /f /ve /t REG_SZ /d "%CD%\Lib\idlelib\idle.pyw"
    reg add "HKCU\SOFTWARE\Python\PythonCore\3.12\IdleShortcuts" /f /ve /t REG_DWORD /d 1
    reg add "HKCU\SOFTWARE\Python\PythonCore\3.12\InstallPath" /f /ve /t REG_SZ /d "%CD%\\"
    reg add "HKCU\SOFTWARE\Python\PythonCore\3.12\InstallPath" /f /v "ExecutablePath" /t REG_SZ /d "%CD%\python.exe"
    reg add "HKCU\SOFTWARE\Python\PythonCore\3.12\InstallPath" /f /v "WindowedExecutablePath" /t REG_SZ /d "%CD%\pythonw.exe"
    reg add "HKCU\SOFTWARE\Python\PythonCore\3.12\PythonPath" /f /ve /t REG_SZ /d "%CD%\Lib\;%CD%\DLLs\\"
    
    ::设置环境变量
    set "add_path=%CD%"
    set current_path=%path%
    echo %current_path% | find "%add_path%" && echo 当前路径已在环境变量中!&& goto end
    ::如果需要版本控制(方便切换版本),可加入 PYTHON_HOME 环境变量,PATH中加入路径变量
    rem setx PYTHON_HOME "%CD%
    rem setx PATH "%PATH%;%%PYTHON_HOME%%;%%PYTHON_HOME%%\Scripts" /M
    ::如果不用版本控制,无需路径变量,PATH中直接加入绝对路径
    setx PATH "%PATH%;%CD%;%CD%\Scripts" /M
    
    :end
    echo.
    python -V
    echo Python 已成功安装!
    pause
    

    注1:如果是单用户使用,无需加入系统环境变量,只加入用户环境变量即可,方法是去掉 setx 的 /M 参数。

    注2:文件关联的注册表是比较完整的(删除了部分无关紧要的内容),可以根据自己对 Python 的精简请看酌情删减。例如不需要 IDLE 的话,即可删掉有关的注册表项。

    2. 卸载批处理:UnInst_Python.cmd

    @echo off & cd /d %~dp0
    cd..
    ::清理注册表
    regsvr32 /u /s Launcher\pyshellext.amd64.dll
    reg delete "HKCU\SOFTWARE\Classes\.py" /f
    reg delete "HKCU\SOFTWARE\Classes\.pyc" /f
    reg delete "HKCU\SOFTWARE\Classes\.pyd" /f
    reg delete "HKCU\SOFTWARE\Classes\.pyo" /f
    reg delete "HKCU\SOFTWARE\Classes\.pyw" /f
    reg delete "HKCU\SOFTWARE\Classes\.pyz" /f
    reg delete "HKCU\SOFTWARE\Classes\.pyzw" /f
    reg delete "HKCU\SOFTWARE\Classes\Python.ArchiveFile" /f
    reg delete "HKCU\SOFTWARE\Classes\Python.CompiledFile" /f
    reg delete "HKCU\SOFTWARE\Classes\Python.Extension" /f
    reg delete "HKCU\SOFTWARE\Classes\Python.File" /f
    reg delete "HKCU\SOFTWARE\Classes\Python.NoConArchiveFile" /f
    reg delete "HKCU\SOFTWARE\Classes\Python.NoConFile" /f
    reg delete "HKCU\SOFTWARE\Python\PyLauncher" /f
    reg delete "HKCU\SOFTWARE\Python\PythonCore" /f
    
    ::清理__pycache__
    set A=%CD%
    for /f "delims=" %%a in ('dir /s /b /ad __pycache__') do (
    	rd /s /q "%%a"
    )
    dir /s /b /ad |findstr "__pycache__" || echo pycache 清理完成 ...
    
    ::删除环境变量 
    set current_path=%path%
    set "del_path=%CD%" 
    :Envi
    if defined current_path (
        for /F "delims=; tokens=1,*" %%a in ("%current_path%") do (
            set "current_path=%%b"
            echo %%a|findstr /i "%del_path%" &&goto Envi
        set "newPath=%newPath%%%a;"
        goto Envi
    ))
    setx PATH "%newPath%" /m >nul
    wmic ENVIRONMENT where "name='PYTHON_HOME'" delete >nul
    echo 环境变量删除完成!
    pause
    

    3. 清理缓存目录 __pycache__:Del_pycache.cmd

    @echo off & cd /d %~dp0
    cd..
    set A=%CD%
    for /f "delims=" %%a in ('dir /s /b /ad __pycache__') do (
    	rd /s /q "%%a"
    )
    dir /s /b /ad |findstr "__pycache__" || echo __pycache__清理完成,按任意键退出 ...
    pause>nul
    

    4. 删除 PIP:Del_pip.cmd

    @echo off  & cd /d %~dp0
    rd /s /q ..\Lib\site-packages\_distutils_hack
    rd /s /q ..\Lib\site-packages\pip
    rd /s /q ..\Lib\site-packages\pip-24.0.dist-info
    rd /s /q ..\Lib\site-packages\pkg_resources
    rd /s /q ..\Lib\site-packages\setuptools
    rd /s /q ..\Lib\site-packages\setuptools-70.0.0.dist-info
    rd /s /q ..\Lib\site-packages\wheel
    rd /s /q ..\Lib\site-packages\wheel-0.43.0.dist-info
    del /q ..\Lib\site-packages\distutils-precedence.pth
    del /q ..\Scripts\pip*.exe
    del /q ..\Scripts\wheel.exe
    rd /s /q "%LOCALAPPDATA%\pip"
    echo. & echo 已删除 PIP 相关文件,按任意键退出 ...
    pause>nul
    

    5. 单独设置环境变量:Inst_Environment.cmd

    @echo off & cd /d %~dp0
    cd..
    set "add_path=%CD%"
    set current_path=%path%
    echo %current_path% |find "%add_path%" &&echo 当前路径已在环境变量中!&&(goto end)
    ::如果需要版本控制(方便切换版本),可加入 PYTHON_HOME 环境变量,PATH中加入路径变量
    rem setx PYTHON_HOME "%CD%
    rem setx PATH "%PATH%;%%PYTHON_HOME%%;%%PYTHON_HOME%%\Scripts" /M
    ::如果不用版本控制,无需路径变量,PATH中直接加入绝对路径
    setx PATH "%PATH%;%CD%;%CD%\Scripts" /M
    
    :end
    pause
    
    

    6. 安装 PIP:Inst_Pip.cmd

    ..\python.exe get-pip.py
    

    该批处理主要是为了在没有设置文件关联和没有安装 Launcher 时方便运行 get-pip.py 。

    作者:molo_king

    物联沃分享整理
    物联沃-IOTWORD物联网 » Python绿色版制作实践总结

    发表回复