how to print contents of PYTHONPATH

前端 未结 2 1361
情书的邮戳
情书的邮戳 2020-12-29 19:50

I have set path using

sys.path.insert(1, mypath)

Then, I tried to print contents of PYTHONPATH variable using os.environ as below

相关标签:
2条回答
  • 2020-12-29 20:23

    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

    0 讨论(0)
  • 2020-12-29 20:26

    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)
    
    0 讨论(0)
提交回复
热议问题