Installing python module within code

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

    i added some exception handling to @Aaron's answer.

    import subprocess
    import sys
    
    try:
        import pandas as pd
    except ImportError:
        subprocess.check_call([sys.executable, "-m", "pip", "install", 'pandas'])
    finally:
        import pandas as pd
    

提交回复
热议问题