簡體   English   中英

使用 cmd 或 powershell 安裝 Python

[英]Install Python with cmd or powershell

我的問題是,您是否可以使用 Windows 內置的 powershell、cmd、vbs 或任何其他語言安裝 python? 如果這已經被問到,請將我重定向到答案。 “如何使用 Windows 命令提示符安裝 Python”解釋了如果您已經安裝了 exe,如何安裝 Python,而不是如何安裝 exe。


編輯:我正在嘗試在沒有安裝 python 的 PC 上安裝帶有文件的 python,唯一受限制的可能是該帳戶不是管理員,如果可能的話,在后台。

您可以下載要安裝的設置,然后在不使用設置的用戶界面的情況下自動安裝它:

[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12

Invoke-WebRequest -Uri "https://www.python.org/ftp/python/3.7.0/python-3.7.0.exe" -OutFile "c:/temp/python-3.7.0.exe"

c:/temp/python-3.7.0.exe /quiet InstallAllUsers=0 PrependPath=1 Include_test=0

我認為沒有管理員權限它不會工作,我嘗試使用InstallAllUsers=0僅為當前用戶安裝它,但它仍然要求提升。

以這種方式安裝時,您可以使用一些選項,這里是文檔: https : //docs.python.org/3.6/using/windows.html#installing-without-ui

通過 Windows 命令提示符安裝 Python 的最佳方式是通過Chocolatey (Windows Package Manager)

安裝python 3的步驟如下:-

  1. 使用“以管理員身份運行”打開 CMD。

  2. 使用以下命令下載並安裝 Chocolatey。

@"%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe" -NoProfile -InputFormat None -ExecutionPolicy Bypass -Command "iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))" && SET "PATH=%PATH%;%ALLUSERSPROFILE%\chocolatey\bin"
  1. 使用以下命令下載並安裝 python。
choco install -y python3
  1. 您可以檢查版本以驗證 Python 是否安裝成功,如下所示。
python --version

沒有管理員權限,您無法安裝它。 我猜這將是缺乏安全性。 例如,您可以在管道中使用的是:

$url = "https://www.python.org/ftp/python/3.7.6/python-3.7.6-amd64.exe"
$output = "C:/tmp/python-3.7.6-amd64.exe"

if (Test-Path $output) {
    Write-Host "Script exists - skipping installation"
    return;
}

New-Item -ItemType Directory -Force -Path C:/tmp

[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
Invoke-WebRequest -Uri $url -OutFile $output


& $output /passive InstallAllUsers=1 PrependPath=1 Include_test=0 

但是仍然需要管理員權限

與問題沒有直接關系,但有些人最終可能會在這里尋找不同的解決方案。

要在 Powershell 中使用 Python:

  • 從 Microsoft Store 安裝 Python(程序列表中的一個應用程序)
  • 打開 Powershell 窗口(可以按住 SHIFT 並右鍵單擊 Python 文件所在的窗口“在此處打開 Powershell 窗口”)
  • 鍵入 'python' 一個空格和文件名 示例:PS C:\\Users...\\Project> python test.py

是的,有一種用cmd下載python的方法。 您所要做的就是編寫“python”,然后您將自動進入 msstore。 我已經為你制作了一些腳本。 它會自動完成,您只需單擊一個批處理文件即可完成。 給你:

@echo off

::Checks if python is installed
:check
python --version 3>NUL
if errorlevel 1 goto errorNoPython

::If python is installed then it runs the program
start <python progam>
exit

::Installes python
:errorNoPython
python
timeout /t 60
goto check

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM