本文介绍如何使用云助手在Windows实例中安装Python。
前提条件
-
ECS实例的状态必须为运行中(Running)。
-
ECS实例分配了固定公网IP或绑定了EIP。
-
2017年12月01日之前购买的ECS实例,需手动安装云助手Agent。具体操作,请参见安装云助手Agent。
说明
2017年12月01日及之后使用公共镜像创建的ECS实例,默认预装了云助手Agent,无需再次安装。
-
Windows操作系统版本为Window Server 2012/2016/2019及更高版本。
操作步骤
-
登录ECS管理控制台。
-
在左侧导航栏,选择运维与监控 > 云助手。
-
在页面左侧顶部,选择目标资源所在的资源组和地域。
-
在页面右上角,单击创建/执行命令。
-
在创建命令页面,设置如下参数,未提及的参数保持默认即可。
命令
|
说明
|
命令来源
|
选择输入命令内容,即创建一个新命令。
|
|
选择立即执行。
|
命令类型
|
选择PowerShell。
|
命令内容
|
请粘贴以下命令到命令内容框中。
说明
本操作以安装Python 3.9.5为例,您可以根据需要,将$pythonVersion 后的版本号替换成您需要安装的Python版本。
$currentDir = $PSScriptRoot
Set-Location $currentDir
# Check the OS version
$OSversion = [Environment]::OSVersion.Version
if ($OSversion.Major -lt 6 -and $OSversion.Minor -lt 1) {
throw "This scrip is not supported on Windows 2008 or lower"
}
$Arch =([Array](Get-WmiObject -Query "select AddressWidth from Win32_Processor"))[0].AddressWidth
if ($Arch -ne "64") {
throw "Only 64-bit system architecture is supported"
}
function Check-Env() {
$curPath = [environment]::GetEnvironmentVariable('path')
if ($curPath -match $pythonKey) {
Write-Host "System already installed $pythonKey, please check"
exit
}
}
function Download-File($file_url, $file_path) {
if (Test-Path $file_path) { return; }
[System.Net.ServicePointManager]::SecurityProtocol=[System.Net.SecurityProtocolType]::Tls12
Invoke-WebRequest -Uri $file_url -OutFile $file_path -UseBasicParsing
if (! $?) { throw "$file_url download to $file_path error" }
}
function Clear-File($file_path) {
if (Test-Path $file_path) {
Remove-Item -Path $file_path -Force -ErrorAction:SilentlyContinue
if (! $?) {
throw "Clear $file_path error, please check"
}
}
}
# install python for all users
function Install-Python() {
if ($pythonVersion.StartsWith("2")) {
& cmd /c msiexec /i $pythonFile /quiet /norestart ADDLOCAL=ALL
} else {
& cmd /c $pythonFile /quiet InstallAllUsers=1 Include_launcher=0 PrependPath=1
}
if ($LASTEXITCODE -ne 0) {
throw "Install $pythonKey error"
}
}
function main() {
Write-Host "Check system environment"
Check-Env
Write-Host "Download python $pythonVersion install package"
Download-File $pythonUrl $pythonFile
Write-Host "Install python $pythonVersion"
Install-Python
Write-Host "Clear python $pythonVersion install package"
Clear-File $pythonFile
Write-Host "Install python $pythonVersion success"
}
# define python version
$pythonVersion = "3.9.5"
$pythonUrl = "https://npm.taobao.org/mirrors/python/${pythonVersion}/python-${pythonVersion}.amd64.msi"
$pythonKey = "python2"
if ($pythonVersion.StartsWith("3")) {
$pythonUrl = "https://npm.taobao.org/mirrors/python/${pythonVersion}/python-${pythonVersion}-amd64.exe"
$pythonKey = "python3"
}
$pythonFile = [io.path]::Combine($currentDir, $pythonUrl.Split('/')[-1])
###
main
|
执行用户
|
输入system。
|
|
自定义命令的执行路径。如不设置,Windows系统默认路径为:C:\ProgramData\aliyun\assist\$(version)。
|
超时时间
|
设置命令在实例中的超时时间,当执行命令的任务超时后,云助手将强制终止任务进程。
单位为秒,默认为60秒,建议超时时间设置为600,具体时间可根据网络情况调整。
|
-
在选择实例或选择托管实例区域,选中需要执行命令的目标实例。
说明
托管实例是云助手托管的非阿里云服务器,更多信息,请参见混合云托管服务器。
-
单击执行。
在弹出的执行详情面板中,当系统显示如下所示时,说明任务执行成功。

-
查看Python是否安装成功。
-
远程连接Windows实例。
具体操作,请参见通过密码或密钥认证登录Windows实例。
-
打开Windows PowerShell命令终端。
-
执行以下命令,查看Python版本。
python.exe -V
当系统显示Python版本时,说明Python已安装成功。
腾讯云1折,限时活动,即将结束: 马上收藏
同尘科技为腾讯云授权服务中心。
购买腾讯云产品享受折上折,更有现金返利。同意关联立享优惠
转转请注明出处:https://www.yunxiaoer.com/153037.html