Ubuntu 18.04 Method Steps for Upgrading All Python Libraries at One Time

  • 2021-07-18 09:40:01
  • OfStack

What is pip

pip is an Python package management tool, which provides the functions of finding, downloading, installing and uninstalling Python packages.

Upgrade pip version

The default Ubuntu comes with pip (pip 9.0. 1) based on Python 2.7
We need to reinstall pip based on Python3:


sudo apt-get install python3-pip

= Upgrade pip3 version:


python3 -m pip install --upgrade pip

Check the pip version of Python3 and report the following error:

ImportError: cannot import name main

Solution: Edit usr/bin/pip3

Before modification:


from pip import main
if __name__ == '__main__':
  sys.exit(main())

After modification:


from pip import __main__
if __name__ == '__main__':
  sys.exit(__main__._main())

Verification fix successfully effective: pip3-V

Terminal printing:


pip 19.3.1 from /home/work/.local/lib/python3.6/site-packages/pip (python 3.6)

Next, upgrade all Python packages with one key

Write an Python script to execute, the following is the code:


import pkg_resources
from subprocess import call
 
for packages in [dist.project_name for dist in pkg_resources.working_set]:
  call("pip3 install --upgrade " + ''.join(packages) + ' --user', shell=True)

Because the pip corresponding to my Python3 is pip3, the pip in call ("pip3 install--upgrade" + ''. join (packages) + '--user', shell=True) here is written as pip3

Next, look at the historical packages of Python and those:


pip3 list --outdated

Terminal printing:


Package   Version Latest Type
----------- ------- ------ -----
distro-info 0.0.0  0.10  sdist
pycairo   1.16.2 1.18.1 sdist
pycups   1.9.73 1.9.74 sdist
pygobject  3.26.1 3.34.0 sdist

Then use these unupgraded packages


pip3 install --upgrade  Package name to upgrade 

Command to upgrade one by one


Related articles: