How to check if a module is installed in Python and, if not, install it within the code?

后端 未结 7 1965
失恋的感觉
失恋的感觉 2021-01-31 17:35

I would like to install the modules \'mutagen\' and \'gTTS\' for my code, but I want to have it so it will install the modules on every computer that doesn\'t have them, but it

7条回答
  •  南笙
    南笙 (楼主)
    2021-01-31 17:45

    If you want to know if a package installed, you can check it in your terminal using the next command:

    pip list | grep 
    

    How this works:

    pip list
    

    lists all modules installed in your Python.

    The vertical bar | is commonly referred to as a "pipe". It is used to pipe one command into another. That is, it directs the output from the first command into the input for the second command.

    grep 
    

    find the keyword from the list.

    Example:

    pip list| grep quant
    

    Lists all packages which start from "quant" (for example "quantstrats"). If you do not have any output, this means the library is not installed.

提交回复
热议问题