批处理文件检查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,安装程序也会启动。这里有什么问题呢?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
对于那些只想简单检查 Python 是否已安装并且可以在批处理文件中执行而无需进入注册表的人:
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:
注册表查询完成后,您的代码不会分支。无论第一个
if ERRORLEVEL
的计算结果是什么,下一步始终是进入:NOPYTHON
标签。Ed:这是一个如何使其发挥作用的示例。我们的想法是添加另一个 goto 语句,如果需要,该语句将跳过
:NOPYTHON
标签。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.这是我的方法。
python -V
命令将返回版本号,而 find 命令借助/v
开关将搜索Python
的省略项还有一种没有那个开关的普通的。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 ofPython
and there's also a normal one without that switch.