windows 用于检查 Python 是否安装的批处理文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4920483/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 16:08:03  来源:igfitidea点击:

batch file to check if Python is installed

windowsinstaller

提问by fixxxer

I've written a batch script that checks if Python is installed, if it's not installed - it initiates the Python installer contained in the same folder as itself.

我编写了一个批处理脚本,用于检查 Python 是否已安装,如果未安装 - 它会启动包含在与其自身相同的文件夹中的 Python 安装程序。

I'm using the following code:

我正在使用以下代码:

reg query "hkcu\software\Python 2.6"

if ERRORLEVEL 1 GOTO NOPYTHON 

:NOPYTHON
ActivePython-2.6.4.8-win32-x86.msi

reg query "hklm\SOFTWARE\ActiveState\ActivePerl\" 1>>Output_%date%_%time%.log 2>&1
if ERRORLEVEL 1 GOTO NOPERL 

reg query "hklm\SOFTWARE\Gtk+"
if ERRORLEVEL 1 GOTO NOPYGTK 


:NOPERL
ActivePerl-5.10.1.1006-MSWin32-x86-291086.msi 1>>Output_%date%_%time%.log 2>&1

:NOPYGTK
pygtk_windows_installer.exe

But in some cases the installer starts up even if Python is installed. What is the problem here?

但在某些情况下,即使安装了 Python,安装程序也会启动。这里有什么问题?

回答by drej

For those who just want a simple check if Python is installed and can be executed without going into the registy, in your batch file:

对于那些只想简单检查 Python 是否已安装并且无需进入注册表即可在批处理文件中执行的人:

:: Check for Python Installation
python --version 2>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

回答by vonPryz

Your code doesn't branch after the registry query is done. No matter what the first if ERRORLEVELevaluates to, the next step is always to step into the :NOPYTHONlabel.

注册表查询完成后,您的代码不会分支。不管第一个if ERRORLEVEL评估结果如何,下一步总是进入:NOPYTHON标签。

Ed: Here is an example how to make it work. The idea is to add another goto statement which will skip the :NOPYTHONlabel if desired.

Ed:这是一个如何让它工作的例子。这个想法是添加另一个 goto 语句,:NOPYTHON如果需要,它将跳过标签。

reg query "hkcu\software\Python 2.6"  
if ERRORLEVEL 1 GOTO NOPYTHON  
goto :HASPYTHON  
:NOPYTHON  
ActivePython-2.6.4.8-win32-x86.msi  

:HASPYTHON  
reg query "hklm\SOFTWARE\ActiveState\ActivePerl\" 1>>Output_%date%_%time%.log 2>&1  

回答by Ste

Here's my method.

这是我的方法。

The python -Vcommand will return the version number and the find command with the aid of the /vswitch will search for the omittance of Pythonand there's also a normal one without that switch.

python -V命令将返回版本号,而借助/v开关的 find 命令将搜索 的省略号,Python也有一个没有该开关的正常版本。

@echo off & title %~nx0 & color 5F

goto :DOES_PYTHON_EXIST

:DOES_PYTHON_EXIST
python -V | find /v "Python" >NUL 2>NUL && (goto :PYTHON_DOES_NOT_EXIST)
python -V | find "Python"    >NUL 2>NUL && (goto :PYTHON_DOES_EXIST)
goto :EOF

:PYTHON_DOES_NOT_EXIST
echo Python is not installed on your system.
echo Now opeing the download URL.
start "" "https://www.python.org/downloads/windows/"
goto :EOF

:PYTHON_DOES_EXIST
:: This will retrieve Python 3.8.0 for example.
for /f "delims=" %%V in ('python -V') do @set ver=%%V
echo Congrats, %ver% is installed...
goto :EOF