Windows 批处理文件以检查是否安装了Python
我编写了一个批处理脚本,用于检查Python是否已安装,是否未安装—它将启动与自身位于同一文件夹中的Python安装程序 我正在使用以下代码:Windows 批处理文件以检查是否安装了Python,windows,installation,Windows,Installation,我编写了一个批处理脚本,用于检查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>>Out
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
这是我的方法
python-V
命令将返回版本号,而find命令在/V
开关的帮助下将搜索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
假设Python在您的道路上,哪一种跳过了最难的部分,这也不能说明Python 2和Python 3之间的区别,这在许多情况下是必要的。@Michael Mrozek的评论在这里也适用。安装了Python但未将其添加到Path的可能性非常高。因此,非技术性的最终用户将陷入使用脚本进行安装和验证的循环中,而不知道路径问题。