如何编写出在Windows处理Python脚本的Python的路径,可执行文件和版本的批处理文件? [英] How to write a batch file showing path to executable and version of Python handling Python scripts on Windows?

查看:129
本文介绍了如何编写出在Windows处理Python脚本的Python的路径,可执行文件和版本的批处理文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有关脚本与Python(蟒蛇myscript.py )的直接调用以及为脚本直接运行(<$运行它应该显示的Python路径可执行文件和版本C $ C> myscript.py )。脚本不应该对系统的配置作太多的假设。比如,它应该处理情况有可能是没有可用的Python。

It should display path to executable and version of Python for scripts run with direct invocation of Python (python myscript.py) as well as for scripts run directly (myscript.py). Script should not make too many assumptions on the configuration of the system. For instance it should handle situation where there might be no available Python.

理结果
我玩运行Python脚本设置环境的不同方式,我认为这将是有帮助的脚本告诉我目前的配置是什么。我关心的是由操作系统提供的标准方式 - PATH 与处理(环境变量和关联文件的类型 assoc命令 FTYPE 命令以及 PATHEXT 环境变量)。这使得 pylauncher 这个问题的范围之外。

Rationale
I'm playing with different ways of setting environment for running Python scripts and I thought it would be helpful to have a script telling me what the current configuration is. I'm concerned with the standard means provided by OS - the PATH environment variable and association of files' types with handlers (assoc and ftype commands as well as PATHEXT environment variable). This leaves pylauncher outside of the scope of this question.

推荐答案

下面是我的第一个解决方案:

Here's my first solution:

解决方案1 ​​

@echo off
set test_script=.pyexe.py
rem Let's create temporary Python script which prints info we need
echo from __future__ import print_function; import sys; print(sys.executable); print(sys.version) > %test_script%
echo Python accessible through system PATH:
python %test_script%
echo ---
echo Python set as handler for Python files:
%test_script%
del %test_script%
set test_script=

我然而碰到这个问题,。如果没有有效的Python间$ P $与Python文件试图打开与 some_script.py Python的文件会弹出相关PTER的打开方式的系统对话框。解决这个问题需要批处理文件很好的认识。因此,试图拿出一个解决方案,我问过以下问题:

I run into problem with this, however. When there's no valid Python interpreter associated with Python files trying to open Python file with some_script.py pops up Open With system dialog. Solving this problem requires very good knowledge of batch files. Therefore trying to come up with a solution I've asked the following questions:

  • How to prevent "Open With" dialog window when opening file from command window?
  • How to escape variables with parentheses inside if-clause in a batch file?
  • Why is delayed expansion in a batch file not working in this case?
  • How to split double quoted strings with embedded spaces deliminated with spaces in a batch file?

改进了原来的批处理文件的版本,现在看起来是这样的:

Improved version of the original batch file looks now like this:

解决方案1B

@echo off
setlocal
set test_script=.pyexe.py
rem Let's create temporary Python script which prints info we need
echo from __future__ import print_function; import sys; print(sys.executable); print(sys.version) > %test_script%
echo Python accessible through the system PATH:
python %test_script%
echo ---
echo Python set as a handler for Python files:
rem We need to check if a handler set in the registry exists to prevent "Open With"
rem dialog box in case it doesn't exist
rem ftype Python.File hypothetical return value:
rem Python.File="%PYTHON_HOME%\python.exe" "%1" %*
for /f "tokens=2 delims==" %%i in ('ftype Python.File') do set reg_entry=%%i
rem ...now in 'reg_entry' variable we have everything after equal sign:
rem "%PYTHON_HOME%\python.exe" "%1" %*
set "handler="
setlocal enableDelayedExpansion
for %%A in (!reg_entry!) do if not defined handler endlocal & set handler=%%A
rem ...now in 'handler' variable we have the first token:
rem "%PYTHON_HOME%\python.exe"
rem Now we expand any environment variables that might be present
rem in the handler's path
for /f "delims=" %%i in ('echo %handler%') do set expanded_handler=%%i
if exist "!expanded_handler!" (
    "%test_script%"
) else (
  if not "!handler!" == "!expanded_handler!" (
    set "handler=!expanded_handler! ^(!handler!^)"
  )
  echo Handler is set to !handler! which does not exist
)
del %test_script%

这是另一个采取回避矛盾上述两个:

This is another take avoiding problems of the above two:

解决方案2

@echo off
setlocal
echo Python accessible through the system PATH:
where python
echo ---
echo Python set as a handler for Python source files (.py):
for /f "skip=2 tokens=1,2*" %%i in ('reg query HKCR\.py /ve') do set "file_type=%%k"
for /f "skip=2 tokens=1,2*" %%i in ('reg query HKCR\%file_type%\shell\open\command /ve') do echo %%k

...和改进的版本:

... and improved version:

解决方案2B

@echo off
setlocal EnableDelayedExpansion
echo Python interpreter accessible through the system PATH:
where python
if not errorlevel 1 (
    python -c "from __future__ import print_function; import sys; print(sys.version)"
)
echo ---
echo Python interpreter registered as a handler for Python source files (.py):
reg query HKCR\.py /ve >nul 2>&1
if errorlevel 1 (
    echo No "HKEY_CLASSES_ROOT\.py" registry key found
) else (
    for /f "skip=2 tokens=1,2*" %%i in ('reg query HKCR\.py /ve 2^>nul') do set "file_type=%%k"
    if "!file_type!"=="(value not set)" (
        echo "No file type set for .py extension"
    ) else (
        reg query HKCR\!file_type!\shell\open\command /ve >nul 2>&1
        if errorlevel 1 (
            echo No "HKEY_CLASSES_ROOT\!file_type!\shell\open\command" registry key found
        ) else (
            for /f "skip=2 tokens=1,2*" %%i in ('reg query HKCR\!file_type!\shell\open\command /ve 2^>nul') do set "handler=%%k"
            if "!handler!"=="(value not set)" (
                echo No command set for !file_type!
            ) else (
                echo !handler!
            )
        )
    )
)

这篇关于如何编写出在Windows处理Python脚本的Python的路径,可执行文件和版本的批处理文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆