I was trying to find an API for pip but I wasn’t able to find a one. So started to look at the code and I realised that I could just emulate what the pip command was doing. It turned out to be really easy.
import pip
pip.main(['install', 'django'])
This is equivalent to
pip install django
That was easy!