Permanently add a directory to PYTHONPATH?

前端 未结 19 1763
别那么骄傲
别那么骄傲 2020-11-22 01:12

Whenever I use sys.path.append, the new directory will be added. However, once I close python, the list will revert to the previous (default?) values. How do I

19条回答
  •  猫巷女王i
    2020-11-22 01:28

    In Python 3.6.4 you can persist sys.path across python sessions like this:

    import sys
    import os
    
    print(str(sys.path))
    
    dir_path = os.path.dirname(os.path.realpath(__file__))
    print(f"current working dir: {dir_path}")
    
    root_dir = dir_path.replace("/util", '', 1)
    print(f"root dir: {root_dir}")
    
    sys.path.insert(0, root_dir)
    
    print(str(sys.path))
    

    I strongly suggest you use virtualenv and virtualenvwrapper otherwise you will clutter your path

提交回复
热议问题