How to upgrade all Python libraries in Ubuntu 18.04 at once

How to upgrade all Python libraries in Ubuntu 18.04 at once

What is pip

pip is a Python package management tool that provides the functions of searching, downloading, installing, and uninstalling Python packages.

Upgrade pip version

By default, the pip that comes with Ubuntu (pip 9.0.1) is based on Python 2.7. We need to reinstall pip based on Python 3:

sudo apt-get install python3-pip

=Upgrade pip3 version:

python3 -m pip install --upgrade pip

Check the pip version of Python3. If the following error is reported:

ImportError: cannot import name main

Solution: Edit the usr/bin/pip3 file

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())

Verify that the fix has taken effect successfully: pip3 -V

The terminal prints:

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

Next, upgrade all Python packages with one click

Just write a Python script to execute it. Here 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 the script call("pip3 install --upgrade " + ''.join(packages) + ' --user', shell=True) should be written as pip3

Next, let's check what other packages there are in the historical versions of Python:

pip3 list --outdated

The terminal prints:

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 non-upgraded packages

pip3 install --upgrade package name to be upgraded

Commands can be upgraded one by one

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Detailed explanation of dynamic link library calling C/C++ method in Python in Ubuntu
  • How to connect Python to MySQL database under Ubuntu system

<<:  A brief talk about MySQL pivot tables

>>:  WeChat applet realizes the nine-square grid effect

Recommend

jQuery realizes image highlighting

It is very common to highlight images on a page. ...

Linux Dig command usage

Dig Introduction: Dig is a tool that queries DNS ...

Detailed graphic explanation of how to use svg in vue3+vite project

Today, in the practice of vue3+vite project, when...

CSS web page responsive layout to automatically adapt to PC/Pad/Phone devices

Preface There are many devices nowadays, includin...

MySQL obtains the current date and time function example detailed explanation

Get the current date + time (date + time) functio...

Several ways to clear arrays in Vue (summary)

Table of contents 1. Introduction 2. Several ways...

A brief discussion on browser compatibility issues in JavaScript

Browser compatibility is the most important part ...

One line of CSS code to achieve the integration of avatar and national flag

It’s National Day, and everyone is eager to celeb...

Summary of CSS sibling element floating analysis

float:left/right/none; 1. Same level floating (1)...

MySQL Installer 8.0.21 installation tutorial with pictures and text

1. Reason I just needed to reinstall MySQL on a n...

Vue uses custom instructions to add watermarks to the bottom of the page

Project Scenario Add a custom watermark to the en...

HTTP return code list (Chinese and English explanation)

http return code list (below is an overview) for ...