Check if Python Package is installed

前端 未结 16 1045
刺人心
刺人心 2020-12-02 05:59

What\'s a good way to check if a package is installed while within a Python script? I know it\'s easy from the interpreter, but I need to do it within a script.

I g

16条回答
  •  一整个雨季
    2020-12-02 06:28

    As of Python 3.3, you can use the find_spec() method

    import importlib.util
    
    # For illustrative purposes.
    package_name = 'pandas'
    
    spec = importlib.util.find_spec(package_name)
    if spec is None:
        print(package_name +" is not installed")
    

提交回复
热议问题