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

后端 未结 7 1967
失恋的感觉
失恋的感觉 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:48

    You can use the command line :

    python -m MyModule
    

    it will say if the module exists

    Else you can simply use the best practice :

    pip freeze > requirements.txt

    That will put the modules you've on you python installation in a file

    and :

    pip install -r requirements.txt

    to load them

    It will automatically you purposes

    Have fun

提交回复
热议问题