我有这个 powershell 脚本,我想在其中检查我的计算机上安装的每个 python:它的版本(例如 3.11.0)和体系结构(32 位或 64 位)。基本上,我首先发出以下命令:
where.exe python
,它返回在计算机上找到的所有 python 可执行文件的列表。然后,对于每个可执行路径,我使用参数运行它:-VV
,如下所示:
function Get-InstalledPythons{
$InstalledPythons= (where.exe python) | Out-String
return $InstalledPythons
}
if ([Environment]::Is64BitOperatingSystem) {
Write-Host "64 bit architecture system. Checking for Python installation..."
$InstalledPythons= Get-InstalledPythons
Write-Host $InstalledPythons
foreach ($pythonPath in $InstalledPythons -split "`n") {
Write-Host $pythonPath
if ($pythonPath -like "*WindowsApps*"){
Write-Host "This is not a Python, skipping..."
} else {
$PythonVersion = (Start-Process -FilePath $pythonPath -ArgumentList "-VV" -NoNewWindow -PassThru).StandardOutput.ReadToEnd()
Write-Host "Python version for $pythonPath : $PythonVersion"
}
}
} else {
Write-Host "32 bit architecture system. Checking for Python-32bit installation..."
}
当我运行脚本时,我可以打印版本变量(其中包含Python版本和体系结构)。但是,我收到此错误,但不知道如何修复它们:
E:\Anaconda3\python.exe
You cannot call a method on a null-valued expression.
At C:\Users\achohra\OneDrive - PSP Investments\Documents\ada_install_scripts\install_python.ps1:17 char:13
+ $PythonVersion = (Start-Process -FilePath $pythonPath -Ar ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
: hon version for E:\Anaconda3\python.exe
C:\Users\achohra\AppData\Local\Programs\Python\Python38-32\python.exe
Python 3.9.12 (main, Apr 4 2022, 05:22:27) [MSC v.1916 64 bit (AMD64)]
You cannot call a method on a null-valued expression.
At C:\Users\achohra\OneDrive - PSP Investments\Documents\ada_install_scripts\install_python.ps1:17 char:13
+ $PythonVersion = (Start-Process -FilePath $pythonPath -Ar ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
: hon version for C:\Users\achohra\AppData\Local\Programs\Python\Python38-32\python.exe
C:\Users\achohra\AppData\Local\Microsoft\WindowsApps\python.exe
This is not a Python, skipping...
我需要帮助来修复这些错误。
之后,我还想分割输出消息:
Python 3.9.12 (main, Apr 4 2022, 05:22:27) [MSC v.1916 64 bit (AMD64)]
以提取版本(本例中为3.9.12)和架构(64位)。
我的最终目标是:
鉴于操作系统架构检查结果是64位
if ([Environment]::Is64BitOperatingSystem) {
我想首先检查Microsoft Excel
是否已安装。如果是,检查其架构(64位或32位);并根据Excel的架构,检查是否安装了具有相同架构的Python。例如,如果我们发现Excel是64位的,而所有安装的Python都是32位的,那么我们需要安装一个新的Python-64位。如果不是,请安装具有相同系统架构(if 语句中的 64 位)的 Excel,并在 Python 上执行与之前相同的检查。这里的目标是让 Excel 和 Python 使用相同的架构。
现在,如果操作系统是32位(其他),那么我们只需检查是否安装了excel(无需检查其体系结构,因为32位操作系统不允许安装64位软件)。然后,如果缺少 python32,我们就安装它。
预先感谢您的帮助。
谢谢。
在评论中有人询问是否可以找到所有Python可执行文件已安装!
所以
where python
的初始方法需要资格。
要找到默认安装,请尝试
where $path:python
很可能它只是一个占位符!!
>C:\Users\lez\AppData\Local\Microsoft\WindowsApps\python.exe /?
Python was not found; run without arguments to install from the Microsoft Store, or disable this shortcut from Settings > Manage App Execution Aliases.
但对于完整系统,您需要
where /r c:\ python.exe
,对于任何应用程序可能调用的任何其他虚拟驱动器也是如此。
因此,为了测试这些版本,您可能需要重定向到listing.txt以运行获取版本命令。
where /r c:\ python > pythons.txt&& for /f %v in (pythons.txt) do @echo %v&&@%v -V
这样您将获得错误反馈并准确查看哪些可能是可执行的
例如这里只有 2 个在工作。
:\Users\lez\AppData\Local\Microsoft\WindowsApps\python.exe
Python was not found; run without arguments to install from the Microsoft Store, or disable this shortcut from Settings > Manage App Execution Aliases.
c:\Users\lez\AppData\Local\Microsoft\WindowsApps\Microsoft.DesktopAppInstaller_8wekyb3d8bbwe\python.exe
Python was not found; run without arguments to install from the Microsoft Store, or disable this shortcut from Settings > Manage App Execution Aliases.
c:\Users\lez\Downloads\Apps\Graphics\Inkscape\App\Inkscape\bin\python.exe
Python 3.10.8
c:\Users\lez\Downloads\Apps\Graphics\Inkscape\App\Inkscape\lib\python3.10\venv\scripts\nt\python.exe
c:\Users\lez\Downloads\Apps\Office\LibreOffice\App\libreoffice\program\python.exe
Python 3.8.16
c:\Users\lez\Downloads\Apps\Office\LibreOffice\App\libreoffice\program\python-core-3.8.16\bin\python.exe
c:\Users\lez\Downloads\Apps\PDF\em36\WPLOConverter\program\python.exe