Get python installation path from script

I have two versions of python installed on my computer. 3.2 the 64-bit bit set in C: \ Python32 \ and 2.7 the 32-bit bit set in C: \ Python27.

I also have a C # application for registering in the registry (64 and 32 bits) in order to get the installation path of the most suitable version of python for use depending on various conditions.

I have a script called Code.py that is launched by a C # application using the selected python version.

In Code.py script, I want to run another script located in C: \ Python32 \ Scripts or C: \ Python27 \ Scripts, depending on the version of python used. However, I want to know what the installation path for this python.exe file is, which is used to run the script I am currently in. Is there a way to do this, or will I need to tell the installation path selected by the C # application as an argument when I run the script (which I would like to avoid)?

Edit: I call the script inside my script as an external script using this code

p = subprocess.Popen(["python", installPath + "\\Scripts\\Flake8", file], stdout=subprocess.PIPE)
+7
source share
1 answer

Use . sys.executable

>>> import sys
>>> sys.executable
'/usr/bin/python'

os.path.split() removes the last component if you only need the path:

>>> import os.path
>>> os.path.split(sys.executable)
('/usr/bin', 'python')
+12
source

All Articles