3

Is there a way to programmatically find which Python version was used to install the current package?

If you have a package called mypackage and it has in its setup.py something like:

scripts = ["myscript.py"]

suppose install the package with easy_install or pip using a particular Python version, Python 2.x:

/usr/local/bin/python2.x setup.py install

Then in myscript.py, how can you find out that it was /usr/local/bin/python2.x that was used to install mypackage from myscript.py as opposed to some other Python version available on the system? I'd like to know the full path and not just the version info because I want to use /usr/local/bin/python2.x in my script.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131

2 Answers2

2

Use sys.executable.

A string giving the absolute path of the executable binary for the Python interpreter, on systems where this makes sense. If Python is unable to retrieve the real path to its executable, sys.executable will be an empty string or None.

KurzedMetal
  • 12,540
  • 6
  • 39
  • 65
  • will that always retrieve the python used to install, as opposed to the python that's first in the environment variable (like bash's `PATH` variable)? –  Jul 31 '13 at 13:22
  • 1
    AFAIK, It's the absolute path to the interpreter used to run the script that access `sys.executable`, doesn't use `PATH`. The description used there is the one in the [`sys` documentation](http://docs.python.org/2/library/sys.html#sys.executable). – KurzedMetal Jul 31 '13 at 14:41
1

Have a look at sys.version or sys.version_info.

Hans Then
  • 10,935
  • 3
  • 32
  • 51
  • That tells me the version but I need to know the path where it is on the system so that I can invoke it from `myscript.py` –  Jul 31 '13 at 12:46