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
?
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.
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*.
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.
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 pathPYTHONPATH
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.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")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With