For pip < 10.0.1:
import pip
from subprocess import call
packages = [dist.project_name for dist in pip.get_installed_distributions()]
for package in packages:
try:
call("pip install --upgrade " + package, shell=True)
except Exception as e:
print("Error: " + str(e))
print('Update finished')
For pip >= 10.0.1:
import pkg_resources
from subprocess import call
packages = [dist.project_name for dist in pkg_resources.working_set]
for package in packages:
try:
call("pip install --upgrade " + package, shell=True)
except Exception as e:
print("type error: " + str(e))