如何编写一个批处理文件,显示可执行文件的路径和 Python 版本,在 Windows 上处理 Python 脚本?

发布于 2024-12-11 00:19:40 字数 474 浏览 0 评论 0原文

对于直接调用 Python 运行的脚本 (python myscript.py) 以及直接运行的脚本 (myscript.py),它应该显示可执行文件的路径和 Python 版本。脚本不应该对系统的配置做出太多假设。例如,它应该处理可能没有可用的 Python 的情况。

基本原理
我正在尝试设置运行 Python 脚本的环境的不同方法,我认为让一个脚本告诉我当前的配置是什么会很有帮助。我关心操作系统提供的标准方法 - PATH 环境变量以及文件类型与处理程序的关联(assocftype 命令以及 PATHEXT 环境变量)。这使得 pylauncher 超出了这个问题的范围。

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.

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.

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

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

发布评论

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

评论(2

舂唻埖巳落 2024-12-18 00:19:40

这可能就是您正在寻找的:

python -c "import sys; print sys.executable"

This might be what you're looking for:

python -c "import sys; print sys.executable"
酷炫老祖宗 2024-12-18 00:19:40

这是我的第一个解决方案:

解决方案 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 文件关联的有效 Python 解释器时,尝试使用 some_script.py 打开 Python 文件时,会弹出打开方式系统对话框。解决这个问题需要对批处理文件有很好的了解。因此,试图提出一个解决方案,我提出了以下问题:

原始批处理文件的改进版本现在如下所示:

解决方案 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%

这是避免上述两个问题的另一种方法:

解决方案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

...以及改进版本:

解决方案 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!
            )
        )
    )
)

Here's my first solution:

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=

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:

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

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

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

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