Posted By: Anonymous
I need to install a package from PyPi straight within my script.
Maybe there’s some module or
pip etc.) feature which allows me to just execute something like
pypi.install('requests') and requests will be installed into my virtualenv.
The officially recommended way to install packages from a script is by calling pip’s command-line interface via a subprocess. Most other answers presented here are not supported by pip. Furthermore since pip v10, all code has been moved to
pip._internal precisely in order to make it clear to users that programmatic use of pip is not allowed.
sys.executable to ensure that you will call the same
pip associated with the current runtime.
import subprocess import sys def install(package): subprocess.check_call([sys.executable, "-m", "pip", "install", package])