12

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

I also have a C# application digging in the registry (64 and 32 bit) to get the install path of the most appropriate python version to use depending on various conditions.

I have a script called Code.py, which is run by the C# application using the python version it selected.

In the Code.py script, I want to run another script located in either C:\Python32\Scripts or C:\Python27\Scripts, depending on which python version was used. However, I want to know what is the install path of that python.exe file used to run the script I am currently in. Is there a way to do that or I'll have to communicate the install path selected by the C# application as an argument when I run the script (which I would want 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)

1 Answer 1

21

Use sys.executable.

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

os.path.split() removes the last component for you if all you need is the path:

>>> import os.path
>>> os.path.split(sys.executable)
('/usr/bin', 'python')
Sign up to request clarification or add additional context in comments.

1 Comment

or just sys.exec_prefix

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.