如何测试 Python 是否安装在 Windows (10) 上,如果未安装,如何运行 exe 来安装它?

Ahm*_*gdi 5 windows cmd

只有当第一个命令失败时,我才需要在 Windows cmd 上运行第二个命令,在另一个场景中,我想在检查是否安装后打开 python 安装程序。

我用过这个命令

python --version || path/to/python_install.exe

据我所知 || 表示如果最后一个命令失败则运行。但它只运行第一个。

Ahm*_*gdi 10

所有的评论都引导我找到了正确的方法。

我使用了这个很棒的工作代码:

:: Check for Python Installation
python --version 3>NUL
if errorlevel 1 goto errorNoPython

:: Reaching here means Python is installed.
:: Execute stuff...

:: Once done, exit the batch file -- skips executing the errorNoPython section
goto:eof

:errorNoPython
echo.
echo Error^: Python not installed
"C:\Program Files\used\systems\innoventiq\accumanager\required\excutables\python-3.7.3-amd64.exe"
Run Code Online (Sandbox Code Playgroud)

  • 您应该编写“goto :EOF”,而不是“goto:eof”,后者仅因“cmd.exe”自动纠错而起作用。命令是 __GOTO__,第一个参数(参数 1)是 `:EOF`,应使用空格字符与命令(参数 0)分隔。这可以通过打开命令提示符窗口并运行“goto /?”来看到,它会输出该命令的帮助解释“goto :EOF”。无需使用冒号“^”进行转义。所以最后一行也适用于“echo Error: Python notinstalled”。 (2认同)

小智 5

  1. 打开命令提示符 > 键入Pytho n 或py > 按 Enter 如果安装了 Python,它将显示版本详细信息,否则它将打开 Microsoft Store 以从 Microsoft Store 下载

  2. 只需进入 cmd 并输入where python如果安装了它就会打开一个提示。

有时如果环境变量没有设置可能不行,所以你也可以通过cmd中的where python查看。如果 python 向该路径返回一些热的东西并查看 python.exe