批处理文件检查是否安装了Python
问题描述:
我已经编写了一个批处理脚本来检查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
标签。
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
答
对于那些谁只是想要一个简单的检查是否安装了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
假设Python是在你的路径,哪一种跳过困难的部分 – 2017-02-06 21:48:34