How can I check that a path is in PYTHONPATH?
I've tried to do:
def path_is_in_pythonpath(path):
return str(path) in sys.path
But it didn't work sometimes when running on Windows. The path was in PYTHONPATH but the check returned False.
It turns out that Windows paths are case insensitive. This applied even to drive letters, which can sometimes be lowercase despite the common sense that they're always uppercase letters.
os.path.normcase does the job of normalizing Windows paths to lowercase appropriately.
This will correctly check if path
is in PYTHONPATH
, OS independently:
def path_is_in_pythonpath(path):
path = os.path.normcase(path)
return any(os.path.normcase(sp) == path for sp in sys.path)