7

If I type in which python I get: /home/USER/anaconda3/bin/python If I type in echo $PYTHONPATH I get: /home/USER/terrain_planning/devel/lib/python2.7/dist-packages:/opt/ros/melodic/lib/python2.7/dist-packages

Should that not be the same? And is it not better to set it: usr/lib/python/ How would I do that? Add it to the PYTHONPATH or set the PYTHONPATH to that? But how to set which python?

martineau
  • 119,623
  • 25
  • 170
  • 301
gab
  • 157
  • 2
  • 9
  • 4
    PYTHONPATH contains nonstandard paths to your python libraries. ˋwhich pythonˋ is the path to your Python executable. The two are not related and you should *not* try to make them the same. – MisterMiyagi Feb 11 '20 at 08:40

2 Answers2

4

You're mixing 2 environment variables:

  • PATH where which looks up for executables when they're accessed by name only. This variable is a list (colon/semi-colon separated depending on the platform) of directories containing executables. Not python specific. which python just looks in this variable and prints the full path
  • PYTHONPATH is python-specific list of directories (colon/semi-colon separated like PATH) where python looks for packages that aren't installed directly in the python distribution. The name & format is very close to system/shell PATH variable on purpose, but it's not used by the operating system at all, just by python.
Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
  • Hi thanks for the good answer. What would happen if I set the PATH to /usr/lib/python ? And how would I do that? – gab Feb 11 '20 at 09:01
  • setting `PYTHONPATH` to the python installation would have no effect as there are no python modules to import at this location. – Jean-François Fabre Feb 11 '20 at 09:12
1

which python is the path to your python interpreter. PYTHONPATH is an environment variable where your Python program can search for modules to import.

See section 1.2

Should that not be the same? And is it not better to set it: usr/lib/python/ How would I do that? Add it to the PYTHONPATH or set the PYTHONPATH to that? But how to set which python?

No they are not the same. You don't really need to modify the path to your Python interpreter. To modify the PYTHONPATH, you can set it in a shell, or from within a Python program by using sys.path

import sys
print(sys.path)
sys.path.append("another/path/to/search")
wstk
  • 1,040
  • 8
  • 14