pythonwindowsversionmultiple-versionsinstalled-applications

How to list all Python versions installed in the system?


I need to present the user a list of Python installations to choose from for executing something. I suppose in Windows I could get this information from registry. Don't know about Linux and Mac.

Any hints? Or maybe you even know a place where I could find Python code for this?

EDIT: it is not important that I find really all interpreters. Finding interpreters from standard locations would be actually fine. Agreed, it's not something too difficult, but I was just hoping that maybe someone has code for this lying around or that I've overlooked a function for that in stdlib.


Solution

  • Not sure this is entirely possible, but here's a dirty fix:

    from subprocess import *
    from time import sleep
    
    for i in range(2, 4):
        x = Popen('python' + str(i) + ' --version', shell=True, stdout=PIPE, stdin=PIPE, stderr=STDOUT)
        while x.poll() == None:
            sleep(0.025)
        print('Exit code of ' + str(i) + ' is:',x.poll())
        x.stdout.close()
        x.stdin.close()
    

    The exit code will tell you if Python2 or Python3 is installed. You could add a second iterator for python versions 2.4, 2.4, 3.1, 3.2 etc etc. Or just keep them in a list, whichever you prefer for this already dirty fix.