Last active
February 8, 2016 06:24
-
-
Save SamuelMarks/7885f2e8e5f0562b1063 to your computer and use it in GitHub Desktop.
Upgrade all packages using pip
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Note that this is a response to a StackOverflow question: Upgrading all packages with pip