开发者

batch file to check if Python is installed

开发者 https://www.devze.com 2023-02-08 19:16 出处:网络
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.

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.

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 st开发者_运维技巧arts up even if Python is installed. What is the problem here?


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:

:: 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


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

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

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  


Here's my method.

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

@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
0

精彩评论

暂无评论...
验证码 换一张
取 消