Installing python module within code

前端 未结 7 1332
孤独总比滥情好
孤独总比滥情好 2020-11-21 23:31

I need to install a package from PyPi straight within my script. Maybe there\'s some module or distutils (distribute, pip etc.) featur

7条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-11-22 00:14

    You can also use something like:

    import pip
    
    def install(package):
        if hasattr(pip, 'main'):
            pip.main(['install', package])
        else:
            pip._internal.main(['install', package])
    
    # Example
    if __name__ == '__main__':
        install('argh')
    

提交回复
热议问题