Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to print contents of PYTHONPATH

Tags:

python

People also ask

How do I find my Pythonpath value?

Use sys. path for that. By simple experiment, I found Vanuan's answer below (printing sys. path) just prints PYTHONPATH.

How do you print a path in Python?

To get current file's full path, you can use the os. path. abspath function. If you want only the directory path, you can call os.

Where is Pythonpath stored?

Environment variables influence the behavior of Python. PYTHONPATH is one such environment variable; that is, it is a key-value pair stored in a computer's memory.


I suggest not to rely on the raw PYTHONPATH because it can vary depending on the OS.

Instead of the PYTHONPATH value in the os.environ dict, use sys.path from the sys module. This is preferrrable, because it is platform independent:

import sys
print(sys.path)

The value of sys.path is initialized from the environment variable PYTHONPATH, plus an installation-dependent default (depending on your OS). For more info see

https://docs.python.org/2/library/sys.html#sys.path

https://docs.python.org/3/library/sys.html#sys.path


If PYTHONPATH hasn't been set then that's expected, maybe default it to an empty string:

import os
print(os.environ.get('PYTHONPATH', ''))

You may also be after:

import sys
print(sys.path)