Update All Python Modules Through Pip

I want to update all the installed python modules through pip. Here below is one recipe I found.

1
2
3
4
5
import pip
from subprocess import call

for dist in pip.get_installed_distributions():
    call("pip install --upgrade " + dist.project_name, shell=True)