1.在家目录新建目录python3
cd ~
mkdir python3
cd python3/
2.安装依赖包,不然编译会报:Failed to build these modules: _ctypes
sudo apt-get install libffi-dev
3.下载python版本
wget https://www.python.org/ftp/python/3.9.0/Python-3.9.0.tar.xz tar -zxvf Python-3.9.0.tar.xz cd Python-3.9.0/
这里也可以从官网下载自己需要的版本:https://www.python.org/downloads/
可以选择第二个xz压缩格式的
https://www.python.org/downloads/release/python-390/
4.编译安装
# 注意以下命令不使用sudo ./configure --prefix=/home/brian/python3 make && make install
5.创建软连接
sudo ln -s /home/test/python3/bin/python3 /usr/bin/python39
sudo ln -s /home/test/python3/bin/pip3 /usr/bin/pip3
6.测试
~ python39 Python 3.9.0 (default, Dec 17 2020, 20:45:58) [GCC 9.3.0] on linux Type "help", "copyright", "credits" or "license" for more information. >>> ^Z [2] + 734415 suspended python39 ➜ ~ ll /usr/bin/python39 lrwxrwxrwx 1 root root 31 12月 18 22:01 /usr/bin/python39 -> /home/brian/python3/bin/python3 ➜ ~ ls ~/python3/lib/python3.9/site-packages dateutil numpy-1.19.4.dist-info pandas-1.1.5.dist-info pkg_resources pytz setuptools six.py easy_install.py numpy.libs pip __pycache__ pytz-2020.4.dist-info setuptools-49.2.1.dist-info numpy pandas pip-20.2.3.dist-info python_dateutil-2.8.1.dist-info README.txt six-1.15.0.dist-info ➜ ~
pip3安装的包的位置:
/home/test/python3/lib/python3.9/site-packages
题外:上面创建的软连接没有用/usr/bin/python3而是用的python39是防止和系统原来已有python3相冲突
否则会导致像lsb_release -a命令不能用,因为它用的是旧版本python3,可能你新安装的版本python3.9.0上不能用
sudo vi /usr/bin/lsb_release 命令可以看到它使用的python路径:#!/usr/bin/python3 -Es