HOWTO · Python

檢查 Windows 中是否安裝了 Python

本教程演示如何檢查 Windows 中是否安裝了 Python

本頁內容

本教程將研究檢查 Python 是否安裝在 Windows 中的多種方法。我們可以使用下面解釋的三個命令檢查 Python 是否安裝在 Windows 中。

使用 py 命令檢查 Windows 中是否安裝了 Python

當我們在 Windows 命令提示符中執行 py 命令時,它首先顯示計算機上安裝的最新版本的詳細資訊,然後執行/啟動已安裝的版本。

如果我們需要檢查是否安裝了某個版本的 Python,即 Python 2 或 3,我們可以使用以下命令來完成。

對於 Python 2.x,

py -2

對於 Python 3.7,

py -3.7

如果未安裝指定版本的 Python,該命令將首先返回 not found 錯誤,然後返回由 py 啟動器安裝的所有已安裝的 Python 版本。

C:\WINDOWS\system32>py -2.5
Python 2.5 not found!
Installed Pythons found by py Launcher for Windows
 -3.8-32 *
 -3.7-64
 -2.7-64

Requested Python version (2.5) not installed, use -0 for available pythons

如果未安裝 py 啟動器,將返回以下錯誤。

'py' is not recognized as an internal or external command,
operable program or batch file.

使用 py --version 命令檢查 Windows 中是否安裝了 Python

我們還可以使用以下命令檢查安裝了哪個版本的 Python 2 或 3。

py --version

上面的命令會返回電腦上安裝的最新版本,不會啟動。我們還可以通過以下方式檢查是否安裝了某些特定版本的 Python。

py -2 --version

它將返回計算機上安裝的最新版本的 Python 2。如果未安裝命令中指定的 Python 版本,如 py 命令,它將返回計算機上安裝的所有版本。

使用 where 命令檢查 Windows 中是否安裝了 Python

Windows where 命令用於在目錄樹中搜尋和顯示檔案。該命令在當前目錄和 PATH 環境變數中指定的路徑中搜尋給定的 pattern

where 命令可以方便地在 PATH 環境變數中定位同一命令的多個版本。因此,如果我們使用 where 命令查詢 python,它將返回 python.exe 的所有副本的路徑。

C:\WINDOWS\system32>where python
C:\Python27\python.exe
C:\Users\Waiz\AppData\Local\Programs\Python\Python37\python.exe
C:\Users\Waiz\AppData\Local\Programs\Python\Python38-32\python.exe

如果計算機上未安裝 Python,where 命令將返回以下錯誤。

INFO: Could not find files for the given pattern(s).