批处理文件检查Python是否已安装

发布于 2024-10-15 23:56:15 字数 607 浏览 5 评论 0原文

我编写了一个批处理脚本,用于检查 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,安装程序也会启动。这里有什么问题呢?

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 starts up even if Python is installed. What is the problem here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

┊风居住的梦幻卍 2024-10-22 23:56:15

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

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
满身野味 2024-10-22 23:56:15

注册表查询完成后,您的代码不会分支。无论第一个 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  

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  
孤独患者 2024-10-22 23:56:15

这是我的方法。

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文