Check if module exists, if not install it

前端 未结 8 1652
北海茫月
北海茫月 2020-12-08 07:41

I want to check if a module exists, if it doesn\'t I want to install it.

How should I do this?

So far I have this code which correctly prints f

8条回答
  •  生来不讨喜
    2020-12-08 08:15

    You can use os.system as follows:

    import os
    
    package = "package_name"
    
    try:
        __import__package
    except:
        os.system("pip install "+ package)
    

提交回复
热议问题