Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

which python vs PYTHONPATH

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?

like image 787
gab Avatar asked Feb 11 '20 08:02

gab


People also ask

Does Python use path or Pythonpath?

Python's behavior is greatly influenced by its environment variables. One of those variables is PYTHONPATH. It is used to set the path for the user-defined modules so that it can be directly imported into a Python program. It is also responsible for handling the default search path for Python Modules.

Does python3 use Pythonpath?

They don't use different PYTHONPATH, but rather python2 uses only packages in the $PREFIX/lib/python2*and python3 only those in $PREFIX/lib/python3*.

What is Pythonpath in Python?

PYTHONPATH is an environment variable which you can set to add additional directories where python will look for modules and packages. For most installations, you should not set these variables since they are not needed for Python to run. Python knows where to find its standard library.


2 Answers

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.
like image 129
Jean-François Fabre Avatar answered Oct 12 '22 05:10

Jean-François Fabre


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")
like image 41
wstk Avatar answered Oct 12 '22 07:10

wstk