Installing python module within code

前端 未结 7 1308
孤独总比滥情好
孤独总比滥情好 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:04

    for installing multiple packages, i am using a setup.py file with following code:

    import sys
    import subprocess
    import pkg_resources
    
    required = {'numpy','pandas',''} 
    installed = {pkg.key for pkg in pkg_resources.working_set}
    missing = required - installed
    
    
    if missing:
        # implement pip as a subprocess:
        subprocess.check_call([sys.executable, '-m', 'pip', 'install',*missing])
    

提交回复
热议问题