Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get python install path from running script

Tags:

python

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)
like image 978
Amaranth Avatar asked Sep 14 '12 15:09

Amaranth


Video Answer


1 Answers

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')
like image 112
Martijn Pieters Avatar answered Oct 01 '22 13:10

Martijn Pieters