简体   繁体   中英

Ubuntu how to change from python3.10 to python3.9?

I have python 3.10 installed. But I am trying to use a python script which is only compatible with python 3.9.

So I have installed 3.9.

wget https://www.python.org/ftp/python/3.9.0/Python-3.9.0.tar.xz
tar -xf Python-3.9.0.tar.xz

cd Python-3.9.0

./configure

sudo make altinstall

Python 3.9 is now installed, so I have bother 3.9 and 3.10 installed. When I search how to use 3.9 most results say to delete 3.10. Is there an option to temprarily switch to 3.9? So after can switch back to 3.10?

Thanks

When you have:

$ ls -l /usr/bin/python3.10
-rwxr-xr-x 1 root root 5921160 Nov 14 17:10 /usr/bin/python3.10
$ ls -l /usr/local/bin/python3.9
-rwxr-xr-x 1 root root 15651728 Dec 31 15:58 /usr/local/bin/python3.9

Then you can do:

sudo update-alternatives --install /usr/bin/python3 python /usr/local/bin/python3.10 0
sudo update-alternatives --install /usr/bin/python3 python /usr/local/bin/python3.9 0

After this being done, this should work:

$ python3
Python 3.10.6 (main, Nov 14 2022, 16:10:14) [GCC 11.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> quit()
$ sudo update-alternatives --config python
There are 2 choices for the alternative python (providing /usr/bin/python3).

  Selection    Path                      Priority   Status
------------------------------------------------------------
* 0            /usr/bin/python3.10        0         auto mode
  1            /usr/bin/python3.10        0         manual mode
  2            /usr/local/bin/python3.9   0         manual mode

Press <enter> to keep the current choice[*], or type selection number: 2
update-alternatives: using /usr/local/bin/python3.9 to provide /usr/bin/python3 (python) in manual mode
$ python3
Python 3.9.0 (default, Dec 31 2022, 15:24:44)
[GCC 11.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> quit()
$

After the update-alternatives process, you should find this link:

$ ls -l /usr/bin/python3
lrwxrwxrwx 1 root root 24 Dec 31 16:17 /usr/bin/python3 -> /etc/alternatives/python

and in '/etc/alternatives' a link, named python which points to:

python -> /usr/local/bin/python3.9

or

python -> /usr/bin/python3.10

NOTE: You need to do the same for pip (or pip3 ), and other tools that relate to python3

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM