Check if Python Package is installed

前端 未结 16 1094
刺人心
刺人心 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:34

    I would like to comment to @ice.nicer reply but I cannot, so ... My observations is that packages with dashes are saved with underscores, not only with dots as pointed out by @dwich comment

    For example, you do pip3 install sphinx-rtd-theme, but:

    • importlib.util.find_spec(sphinx_rtd_theme) returns an Object
    • importlib.util.find_spec(sphinx-rtd-theme) returns None
    • importlib.util.find_spec(sphinx.rtd.theme) raises ModuleNotFoundError

    Moreover, some names are totally changed. For example, you do pip3 install pyyaml but it is saved simply as yaml

    I am using python3.8

提交回复
热议问题