How to install a missing python package from inside the script that needs it?

后端 未结 3 439
隐瞒了意图╮
隐瞒了意图╮ 2021-02-05 11:27

Assuming that you already have pip or easy_install installed on your python distribution, I would like to know how can I installed a required package in the user directory from

3条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-02-05 11:58

    As of pip version >= 10.0.0, the above solutions will not work because of internal package restructuring. The new way to use pip inside a script is now as follows:

    try: import abc
    except ImportError:
        from pip._internal import main as pip
        pip(['install', '--user', 'abc'])
        import abc
    

提交回复
热议问题