[英]Trying to Install latest python3 version using powershell script
我目前正在通過對腳本中的版本進行硬編碼來安裝 python3。 我試圖弄清楚我能做些什么來始終在 windows 代理上安裝最新的穩定版本 python。
$url = "https://www.python.org/ftp/python/3.9.10/python-3.9.10.exe"
$pythonPath = "C:/Program Files/python/python-3.9.10.exe"
If ((Test-Path C:) -and !(Test-Path "C:\Program Files\python"))
{
New-Item -Path "C:\Program Files\" -Name "python" -ItemType "directory"
}
[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072
Invoke-WebRequest -Uri $url -OutFile $pythonPath
以上是我目前正在做的事情,雖然這工作正常,但是我不能對版本進行硬編碼並安裝最新的穩定 python 版本嗎?
注意:如果您使用的是最新版本的 Windows 10 或者您使用的是 Windows 11,請考慮使用winget.exe
安裝 Python,自動針對最新版本:
winget install python --accept-package-agreements
否則,您將不得不求助於web 抓取,但它本質上是脆弱的(網頁可以更改)。
一種實用的方法是刮https://www.python.org/downloads/
進行第一次下載 URL 以.exe
結尾,假設指向最新的穩定版本:
if (
(Invoke-RestMethod 'https://www.python.org/downloads/') -notmatch
'\bhref="(?<url>.+?\.exe)"\s*>\s*Download Python (?<version>\d+\.\d+\.\d+)'
) { throw "Could not determine latest Python version and download URL" }
# The automatic $Matches variable contains the results of the regex-
# matching operation.
$url = $Matches.url
$pythonPath = "C:/Program Files/python/python-$($Matches.version).exe"
# ...
一個相對更健壯但更麻煩的方法是刮https://www.python.org/ftp/python/
代替:
sh
( bash
) shell 腳本的重點實現。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.