Skip to content

Instantly share code, notes, and snippets.

@SamuelMarks
Last active February 8, 2016 06:24
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SamuelMarks/7885f2e8e5f0562b1063 to your computer and use it in GitHub Desktop.
Save SamuelMarks/7885f2e8e5f0562b1063 to your computer and use it in GitHub Desktop.
Upgrade all packages using pip
#!/usr/bin/env python
from __future__ import print_function
from sys import stderr
from pip import get_installed_distributions
from pip.commands import install
from pip.exceptions import InstallationError
print_error = lambda error: print(
'{0}: {1}'.format(error.__class__.__name__, error.message),
file=stderr
)
if __name__ == '__main__':
print('Trying to upgrade all python packages (using pip)')
install_cmd = install.InstallCommand()
for package in get_installed_distributions():
options, args = install_cmd.parse_args([package.project_name])
options.upgrade = True
print('Attempting to upgrade: {project_name!r}'.format(project_name=package.project_name))
try:
install_cmd.run(options, args)
except OSError as e:
if e.errno == 13: # permissions error
raise
print_error(e)
except (InstallationError, IOError) as e:
print_error(e)
@SamuelMarks
Copy link
Author

Note that this is a response to a StackOverflow question: Upgrading all packages with pip

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment