简体   繁体   中英

Python Installation Location (Windows)

I need to find out if there is Python installed on the computer.

My specific issue is that I am distributing a program that comes with its own interpreter + standard library (the end-user may not have Python). In the installation, I am giving the option to use the user's own installed Python interpreter + library if they have it. However, I need the location of that. I can ask the user to find it manually, but I was hoping there was an automatic way.

Since my installer uses my included interpreter, sys.prefix refers to the included interpreter (I know this because I tried it out, I have Python 2.7 and 3.3 installed).

I also tried using subprocess.call : subprocess.call(['py', '-c', '"import sys; print sys.prefix"']) which would use the standard Python interpreter if there was one, but I'm not sure how to capture this output.

Thus, are there any other ways to find out if there is a default Python version installed on the user's computer and where?

Actually, in the light of my other answer, an even better way to find the Python installation directory would probably be to check the Windows registry, since the Python installer places some information there.

Take a look at this answer and this Python module .

Some users might have placed their local Python directory into the system's PATH environment variable and some might even have set the PYTHONPATH environment variable.

You could try the following:

import os

if "python" in os.environ["PATH"].lower():
    # Confirm that the Python executable actually is there

if "PYTHONPATH" in os.environ.keys():
    # Same as in the last if...

As for the subprocess.call(...) , set the stdout parameter for something that passes for a file object, and afterwards just .read() the file object you gave to see the output from the call.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM