1

When I am running a script get the path of Python kernel (not mater if it is Python, IPython, or IPython Notebook with non-standard kernel, where I can switch between Py2 and Py3).

When writting (in IPython)

import sys
sys.argv

I get the path to IPython: ['/usr/local/bin/ipython'].

Is it possible to get the path to Python as well?

I need it so to match versions of Python from IPython (Notebook) to Spark worker (PYSPARK_PYTHON).

Piotr Migdal
  • 11,864
  • 9
  • 64
  • 86
  • The path to `Python`? What do you mean with Python here – Mark Jansen Aug 19 '15 at 10:12
  • E.g. a link `python` or `python2` a full path (e.g. `/usr/local/bin/python2`). – Piotr Migdal Aug 19 '15 at 10:13
  • 1
    This answer might point you in the right direction: [ipython reads wrong python version][1] [1]: http://stackoverflow.com/questions/9386048/ipython-reads-wrong-python-version – Maarten Aug 19 '15 at 10:17
  • @Maarten Not a bit. IPython Notebook can launch different kernels. – Piotr Migdal Aug 19 '15 at 11:37
  • And you want the path to the python excecutable that your current kernel is running? – Maarten Aug 19 '15 at 12:39
  • For me sys. argv will indeed give me the ipyhon executable and, running from the note book, sys.path will give me the system python executable. This is on a windows machine – Maarten Aug 19 '15 at 13:15
  • @Maarten Well, `sys.path` gives access to system paths - they are irrelevant to which Python is in use when executing a particular script. And `sys.argv` for IPython Notebook gives the default kernel, not - one is currently running. – Piotr Migdal Aug 19 '15 at 14:08

1 Answers1

4

A general way to do so (i.e. working both in scripts, ipython shell and IPython Notebook) is:

import sys
sys.executable

(Thx Carreau!)

Piotr Migdal
  • 11,864
  • 9
  • 64
  • 86