在Windows Server系统中绿色安装python

在Windows Server 2012系统中绿色安装python

1:首先下载
https://www.python.org/ftp/python/3.13.2/python-3.13.2-embed-amd64.zip
然后解压到

D:\Users\%username%\programs\python

2:添加python的Path环境变量

set "newval=D:\Users\%USERNAME%\programs\python"
for /f "tokens=2,*" %A in ('reg query "HKCU\Environment" /v PATH ^| findstr "PATH"') do set userPath=%B
if defined userPath (set "userPath1=;%userPath%;" && echo %userPath1% | findstr /i /c:";%newval%;" >nul || setx PATH "%userPath%;%newval%") else setx PATH "%newval%"

添加完环境变量后请重新打开新的cmd来测试python

3:安装PIP

python -c "import urllib.request; urllib.request.urlretrieve('https://bootstrap.pypa.io/get-pip.py', 'get-pip.py'); exec(open('get-pip.py').read())"

上面可能会报错,可以换下面的方法

powershell -Command "Invoke-WebRequest -Uri https://bootstrap.pypa.io/get-pip.py -OutFile get-pip.py"
python get-pip.py

4:添加pip的环境变量

set "newval=D:\Users\%USERNAME%\programs\python\Scripts"
for /f "tokens=2,*" %A in ('reg query "HKCU\Environment" /v PATH ^| findstr "PATH"') do set userPath=%B
if defined userPath (set "userPath1=;%userPath%;" && echo %userPath1% | findstr /i /c:";%newval%;" >nul || setx PATH "%userPath%;%newval%") else setx PATH "%newval%"

添加完环境变量后请重新打开新的cmd来测试pip

5:修改python313._pth文件
把文件中的“#import site”前面的"#"去掉,保存修改。

如果觉得麻烦,可以使用下面这个“一键安装脚本.bat”

@echo off
cls
title %0
powershell -Command "[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; Invoke-WebRequest -Uri 'https://www.python.org/ftp/python/3.13.2/python-3.13.2-embed-amd64.zip' -OutFile '%APPDATA%\python.zip'"
powershell -Command "Expand-Archive -Path '%APPDATA%\python.zip' -DestinationPath '%APPDATA%\python' -Force"
cd /d %APPDATA%\python
set "newval=%APPDATA%\python;%APPDATA%\python\Scripts"
set PATH=%PATH%;%APPDATA%\python;%APPDATA%\python\Scripts
for /f "tokens=2,*" %%A in ('reg query "HKCU\Environment" /v PATH ^| findstr "PATH"') do set userPath=%%B
if defined userPath (set "userPath1=;%userPath%;" && echo %userPath1% | findstr /i /c:";%newval%;" >nul || setx PATH "%userPath%;%newval%") else setx PATH "%newval%"
python -c "import urllib.request; urllib.request.urlretrieve('https://bootstrap.pypa.io/get-pip.py', 'get-pip.py'); exec(open('get-pip.py').read())"
echo python313.zip>%APPDATA%\python\python313._pth
echo .>>%APPDATA%\python\python313._pth
echo import site>>%APPDATA%\python\python313._pth
echo 成功安装Python3.13.2
pause
posted on 2025-04-06 21:35  项希盛  阅读(113)  评论(0)    收藏  举报