batch file来检查是否安装了Python

我已经写了一个批处理脚本来检查是否安装了Python,如果没有安装,它会启动包含在同一文件夹中的Python安装程序

我使用下面的代码:

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 

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

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

埃德:这是一个如何使它工作的例子。 这个想法是添加另一个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 

对于那些只想简单检查一下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