头图

我发现python的静默安装,在win11有些版本上会有问题,导致Path不能写上环境变量。

  • 所以我做了两个.bat文件用来静默的安装python。
@echo off
:: Set download URL and target path
set PYTHON_URL=https://mirrors.aliyun.com/python-release/windows/python-3.12.6-embed-amd64.zip
set TARGET_DIR=D:\Python312_6
set ZIP_FILE=%TARGET_DIR%\python-3.12.6-embed-amd64.zip

:: Create target directory
if not exist "%TARGET_DIR%" (
    echo Creating target directory: %TARGET_DIR%
    mkdir "%TARGET_DIR%"
) else (
    echo Target directory already exists: %TARGET_DIR%
)

:: Download Python embedded version
echo Downloading Python 3.12.6 embedded version...
powershell -Command "Invoke-WebRequest -Uri '%PYTHON_URL%' -OutFile '%ZIP_FILE%'"

:: Check if download was successful
if not exist "%ZIP_FILE%" (
    echo Download failed, please check your network connection or the download link!
    pause
    exit /b 1
)

:: Extract ZIP file to target directory
echo Extracting to %TARGET_DIR%...
powershell -Command "Expand-Archive -Path '%ZIP_FILE%' -DestinationPath '%TARGET_DIR%' -Force"

:: Check if extraction was successful
if exist "%TARGET_DIR%\python.exe" (
    echo Python has been successfully extracted to %TARGET_DIR%
) else (
    echo Extraction failed, please check the ZIP file or target path!
    pause
    exit /b 1
)

echo Script execution completed!
@echo off
setlocal

REM Specify the path to add
set "new_path=D:\Python312_6"

REM Get the current user PATH variable
for /f "skip=2 tokens=3*" %%a in ('reg query HKCU\Environment /v PATH') do (
    set "current_path=%%a %%b"
)

REM Check if the path already exists
echo %current_path% | find /i "%new_path%" >nul 2>&1
if %errorlevel% equ 0 (
    echo The path already exists in PATH.
    goto end
)

REM Append the new path to PATH
set "updated_path=%current_path%;%new_path%"

REM Update the user PATH variable
setx PATH "%updated_path%"

echo The path has been successfully added to PATH.

:end
endlocal
  1. 第一个是GetPython文件,从镜像下载二进制源文件,直接在指定目录解压
  2. 第二个是SetPath文件,用来将路径写入 用户变量

    注意,没有在win10上做过测试,不保证能运行
  3. 运行结果:
    image.png

Hxic
1 声望0 粉丝

在读学生