Use sys. path for that. By simple experiment, I found Vanuan's answer below (printing sys. path) just prints PYTHONPATH.
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.
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)
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