ubuntu install python3.9

1. Related background

I have been using python3.8 in the dockerfile before (I forgot why I chose this version). I want to use python3.9 because I think 3.8 is a bit old, and an important feature of 3.9 is to use list as the default type, which is not required. List is imported as the data type from typing.
Now the Python installation command in the dockerfile based on python3.8 is as follows

apt-get install -y python3.8 python3-pip && ln -s /usr/bin/python3.8 /usr/bin/python

But if you directly replace python 3.8 with python 3.9, you will find that it cannot be installed and the python3.9 package cannot be found. The reason is that the official repo of Ubuntu does not provide the 3.9 version of python.

2. Solution

  1. Install python 3.9
apt-get update
apt-get install software-properties-common #
#  "software-properties-common" 是一个软件包,
# 该软件包提供了一些常用的工具和功能,用于管理软件源(repositories)和 PPA(Personal Package Archive)。
add-apt-repository ppa:deadsnakes/ppa
# Install py39 from deadsnakes repository
apt-get install python3.9 python3-pip
ln -s /usr/bin/python3.9 python
  1. Post-processing
    I don’t know why the final built image has a default python of 3.8. The python packages installed in the dockerfile are all bound to python 3.8, resulting in python 3.9 actually not being bound to the installed package. .
    The solution is to add the following two lines of code to force both python and python3 to point to python3.9
rm /usr/bin/python3
ln -s /usr/bin/python3.9 python3

Guess you like

Origin blog.csdn.net/qq_29007291/article/details/132282911
Recommended