2

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?

Rushy Panchal
  • 14,511
  • 14
  • 52
  • 87
  • if its a program why would you want to allow them to use a different python then the one its packaged with? if its just a library then why are you packaging it with an interpreter? how are you packaging your program for distribution? – Joran Beasley Aug 08 '13 at 19:27
  • @JoranBeasley It's not meant solely for Python users, so it's not just a library. It's a program, but if the user has a version of Python 2.7, I will remove the included installation and use their installed one (no point in two interpreters + libraries). – Rushy Panchal Aug 08 '13 at 19:32

2 Answers2

3

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.

Community
  • 1
  • 1
miikkas
  • 748
  • 1
  • 6
  • 25
1

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.

miikkas
  • 748
  • 1
  • 6
  • 25