Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6702311
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T07:02:22+00:00 2026-05-26T07:02:22+00:00

It should display path to executable and version of Python for scripts run with

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T07:02:22+00:00Added an answer on May 26, 2026 at 7:02 am

    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:

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

    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!
                )
            )
        )
    )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I should display the bookmarks as hyperlink.so on clicking the bookmark will take me
I have some validation code which should display the max / min of a
I know that the reminder application should display a badge or sound or kind
Scenario: You have an ASP.Net webpage that should display the next image in a
I created a div(search result term bar) which should display only when user enters
I'm writing an iPhone app with a UIWebView which should display various html files
As far as I can tell, this code is fine, and should display some
In firefox, the error messages display as should. Just to the right of the
C# Which Event should I use to display data in a textbox when I
What command line should I write to display the memory used by process as

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.