How to add a Python import path using a .pth file

后端 未结 2 978
醉话见心
醉话见心 2020-12-01 07:18

If I put a *.pth file in site-packages it\'s giving an ImportError. I\'m not getting how to import by creating a *.pth file.

(Refers to importing in

2条回答
  •  旧巷少年郎
    2020-12-01 07:38

    If you put a .pth file in the site-packages directory containing a path, python searches this path for imports. So I have a sth.pth file there that simply contains:

    K:\Source\Python\lib
    

    In that directory there are some normal Python modules:

    logger.py
    fstools.py
    ...
    

    This allows to directly import these modules from other scripts:

    import logger
    
    log = logger.Log()
    ...
    

提交回复
热议问题