Normally, it's not nice to download and install things on behalf of somebody running your script. As triplee's answer mentions, using a requirements.txt and a proper setup.py is a standard and far better practice.
At any rate, the following is a hack to get the behavior you want in a script.
import pip
import importlib
modules = ['requests', 'fake_module_name_that_does_not_exist']
for modname in modules:
try:
# try to import the module normally and put it in globals
globals()[modname] = importlib.import_module(modname)
except ImportError as e:
result = pip.main(['install', modname])
if result != 0: # if pip could not install it reraise the error
raise
else:
# if the install was sucessful, put modname in globals
globals()[modname] = importlib.import_module(modname)
If you were to execute this example, you would get output something like as follows
Collecting requests
Using cached requests-2.18.4-py2.py3-none-any.whl
Requirement already satisfied: idna<2.7,>=2.5 in c:\users\spenceryoung\envs\test_venv\lib\site-packages (from requests)
Requirement already satisfied: urllib3<1.23,>=1.21.1 in c:\users\spenceryoung\envs\test_venv\lib\site-packages (from requests)
Requirement already satisfied: chardet<3.1.0,>=3.0.2 in c:\users\spenceryoung\envs\test_venv\lib\site-packages (from requests)
Requirement already satisfied: certifi>=2017.4.17 in c:\users\spenceryoung\envs\test_venv\lib\site-packages (from requests)
Installing collected packages: requests
Successfully installed requests-2.18.4
Collecting fake_module_name_that_does_not_exist
Could not find a version that satisfies the requirement fake_module_name_that_does_not_exist (from versions: )
No matching distribution found for fake_module_name_that_does_not_exist
Traceback (most recent call last):
File "<stdin>", line 3, in <module>
File "C:\Users\spenceryoung\AppData\Local\Programs\Python\Python36-32\lib\importlib\__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 994, in _gcd_import
File "<frozen importlib._bootstrap>", line 971, in _find_and_load
File "<frozen importlib._bootstrap>", line 953, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'fake_module_name_that_does_not_exist'