Skip to content

Instantly share code, notes, and snippets.

@caglartoklu
Last active July 2, 2019 16:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save caglartoklu/72c28c0d59d5256e4fde to your computer and use it in GitHub Desktop.
Save caglartoklu/72c28c0d59d5256e4fde to your computer and use it in GitHub Desktop.
upgrade all #python packages using #pip
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Traverses all the Python packages and upgrades them.
"""
from __future__ import print_function
from __future__ import unicode_literals
import os
import pip
def get_exceptions():
"""
Returns the list of exceptions, that is, packages that are
not be upgraded.
"""
# Add the exceptions into the following variable,
# each to a separate line.
exceptions = """
Django
""".strip()
lst_exceptions = exceptions.split("\n")
lst_exceptions = [x.lower().strip()
for x in lst_exceptions if len(x.strip()) > 0]
return lst_exceptions
def main():
"""
Main module.
"""
lst_exceptions = get_exceptions()
try:
# check if we have pip.get_installed_distributions()
print(pip.get_installed_distributions)
packages = [dist.project_name
for dist in pip.get_installed_distributions()]
except AttributeError:
# AttributeError:
# module 'pip' has no attribute 'get_installed_distributions'
# This pip version does not support get_installed_distributions()
# So, use the one in newer versions:
import pkg_resources
packages = [dist.project_name for dist in pkg_resources.working_set]
print(pkg_resources.working_set)
print(type(pkg_resources.working_set))
for package in packages:
if package.lower() not in lst_exceptions:
command = "pip install --upgrade " + package
print(command)
os.system(command)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment