简体   繁体   English

Python安装位置(Windows)

[英]Python Installation Location (Windows)

I need to find out if there is Python installed on the computer. 我需要找出计算机上是否安装了Python。

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). 我的具体问题是,我正在分发一个带有自己的解释器和标准库的程序(最终用户可能没有Python)。 In the installation, I am giving the option to use the user's own installed Python interpreter + library if they have it. 在安装中,如果有用户,我可以选择使用用户自己安装的Python解释器+库。 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). 由于我的安装程序使用了我包含的解释器,因此sys.prefix引用了包含的解释器(我知道这一点是因为我尝试了,我安装了Python 2.7和3.3)。

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. 我也尝试使用subprocess.callsubprocess.call(['py', '-c', '"import sys; print sys.prefix"'])如果有一个这将使用标准的Python解释器,但我我不确定如何捕获此输出。

Thus, are there any other ways to find out if there is a default Python version installed on the user's computer and where? 因此,还有其他方法可以确定用户计算机上是否安装了默认的Python版本,以及在哪里?

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. 实际上,根据我的其他答案,找到Python安装目录的一种更好的方法可能是检查Windows注册表,因为Python安装程序会在其中放置一些信息。

Take a look at this answer and this Python module . 看一下这个答案这个Python模块

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. 一些用户可能已将其本地Python目录放置在系统的PATH环境变量中,而某些用户甚至可能已设置了PYTHONPATH环境变量。

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. 至于subprocess.call(...) ,请为传递给文件对象的内容设置stdout参数,然后.read()为您提供的文件对象.read()来查看调用的输出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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