I saw this answer to check if a module is well installed from within the python script here : How to check if a module is installed in Python and, if not, install it within the code?
Here is the basic gist of it:
The pip module has updated quite a lot since the time I posted this answer. I've updated the snippet with the proper way to install a missing dependency, which is to use subprocess and pkg_resources, and not pip.
To hide the output, you can redirect the subprocess output to devnull:
import sys
import subprocess
import pkg_resources
required = {'mutagen', 'gTTS'}
installed = {pkg.key for pkg in pkg_resources.working_set}
missing = required - installed
if missing:
python = sys.executable
subprocess.check_call([python, '-m', 'pip', 'install', *missing], stdout=subprocess.DEVNULL)
The main issue here for me would be that the dependencies are within the code.You could solve this by having the variable required in the above code that will parse your requirement.txt to extract the right packages that are needed for your program.
I hope that this will help you.