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
Answer from Maxime Lorant on Stack OverflowI 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)
You would probably also want this:
import sys
print(sys.path)
Or as a one liner from the terminal:
python -c "import sys; print('\n'.join(sys.path))"
Caveat: If you have multiple versions of Python installed you should use a corresponding command python2 or python3.
sys.path might include items that aren't specifically in your PYTHONPATH environment variable. To query the variable directly, use:
import os
try:
user_paths = os.environ['PYTHONPATH'].split(os.pathsep)
except KeyError:
user_paths = []
To print python's sys.path from the command line - Stack Overflow
debian - $PYTHONPATH and sys.path don't seems to match - Unix & Linux Stack Exchange
Python 3.6 and Windows 8: Adding path (PYTHONPATH/SYS.PATH) to include own modules in project/file.
[ELI5] What is sys.path?
How do I add PYTHONPATH inside a Python script?
Is PYTHONPATH required to import installed packages?
How do I set multiple directories in PYTHONPATH?
Hello! I'm already getting tired of including the same things in each file and re-writing code, so I created a folder for my own modules and put my own 'tools' in there. I added a PYTHONPATH variable to windows variable list (as well as put the path in the PATH variable), I have a (blank) 'init.py' in the directory of my module, but still when I type "import ..." it isn't showing up still. I've also appended it to sys.path via the sys.path.append('c:/users/..../myModules/') but still, nothing. ???? wtf? I've read about 20 pages on it, and tried everything, but nothing is working, so it's probably something really stupid.
What is sys.path? And exactly, in which directory I can find it and what does it do?