How to list all versions of Python installed on a system?

I need to present the user with a list of Python installations to choose from to do something. I suppose that on Windows I could get this information from the registry. I do not know about Linux and Mac.

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

EDIT: It doesn't matter that I really find all the translators. Finding interpreters from standard locations would be really wonderful. You must admit that this is not too complicated, but I was just hoping that maybe someone has code for this lying around, or that I missed the function for this in stdlib.

+6
source share
2 answers

Not sure if 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 can add a second iterator for versions 2.4, 2.4, 3.1, 3.2, etc. Etc. Or just keep them on the list, depending on what you prefer for this already dirty fix.

0
source

I am writing a Python IDE, and I want the user to be able to choose an interpreter to execute the program.

Just do it, like other IDEs, and just put a dialog box in which users can add interpreters with which they can run code.

Eclipse does this, for example, for Java Runtimes, and is perfectly fine to do so. Especially for languages ​​like Python, where virtual environments are an important thing, each of which has its own exectutable.

You can certainly come up with a one-time discovery that checks for some common locations. For Windows, this will obviously be a registry, since the py.exe launcher requires translators to be registered there - at least system-wide. On Unix machines, you can check the bin/ shared folders, most notably /usr/local/bin/ , which is the standard place where Python installs itself. You can also check PATH for Python executables. But all these things should be carefully considered and only initial setup should be offered. There are always extreme cases where the user has not done the β€œstandard thing” where your discovery will fail. For example, I do not have my Python interpreters in my path, and I am accessing a Linux server. I installed Python in a non-standard folder in my home directory. And finally, just because it looks like Python does not mean that it is Python.

Yes, you can make some guesses to come up with the original set of interpreters, but don't really waste too much time on it. In the end, you cannot fully discover everything. And you will miss virtual environments that can be very important for a project that a user is working on in your IDE.

Therefore, instead of wasting time on poor detection, spend more time creating a manual dialogue for registering interpreters. In any case, you will need it, and a good interface can make it very simple - even for beginners - to use it.

+4
source

All Articles