更新程序包并安装变异依赖环境


开始正式安装

安装之前

问题一:安装时报错ModuleNotFoundError: No module named ‘_ctypes’的解决办法
1、执行如下命令:

  1. yum install libffi-devel

2、从”./configure …”重新安装
问题二:pip3 install时报错“pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available.”
先安装openssl-dev,然后重新编译安装,只是在编译的过程中加入 —enable-optimizations

ubuntu:

  1. sudo apt-get install libffi-dev

或者

  1. sudo apt-get update
  2. sudo apt-get upgrade
  3. sudo apt-get dist-upgrade
  4. sudo apt-get install build-essential python-dev python-setuptools python-pip python-smbus
  5. sudo apt-get install build-essential libncursesw5-dev libgdbm-dev libc6-dev
  6. sudo apt-get install zlib1g-dev libsqlite3-dev tk-dev
  7. sudo apt-get install libssl-dev openssl
  8. sudo apt-get install libffi-dev

1.下载Python源码包
  1. cd ~
  2. wget https://www.python.org/ftp/python/3.10.0/Python-3.10.0.tgz

2.解压Python源码
  1. tar xzf Python-3.10.0.tgz

3.编译Python源码
  1. cd Python-3.10.0
  2. ./configure --enable-optimizations
  3. #--enable-optimizations为优化性能选项,其余类似的还有 --prefix=PATH 指定安装目录……,可根据需要进行选择。
  4. #默认安装路径为 /usr/local/bin

4.安装Python 3.10
  1. make altinstall
  2. #altinstall用于防止编译器覆盖默认Python版本

5.验证安装
  1. root@raspberrypi:~ # python3.10
  2. Python 3.10.0 (default, Dec 5 2021, 22:46:09) [GCC 10.2.1 20210110] on linux
  3. Type "help", "copyright", "credits" or "license" for more information.

至此,已完成Python3.10的安装
接下来可以根据需要选择是否需要更改默认Python为Python3.10

切换Python版本

可以使用以下两个命令 whereis或which 确定已安装python的版本和路径:

  1. root@raspberrypi:~ # whereis python
  2. python: /usr/bin/python2.7-config /usr/bin/python /usr/bin/python3.9 /usr/bin/python2.7 /usr/bin/python3.9-config /usr/lib/python3.9 /usr/lib/python2.7 /etc/python3.9 /etc/python2.7 /usr/local/bin/python3.10-config /usr/local/bin/python3.10 /usr/local/lib/python3.9 /usr/local/lib/python2.7 /usr/local/lib/python3.10 /usr/include/python3.9m /usr/include/python3.9 /usr/include/python2.7 /usr/share/man/man1/python.1.gz
  1. root@raspberrypi:~ # which python3.10
  2. /usr/local/bin/python3.10

为单个用户切换Python版本

只需要在该用户home目录下的 .bashrc 文件下新增 Alias 即可

  1. alias python='/usr/local/bin/python3.10'
  2. #python具体版本和路径可根据个人需要确定

修改完毕后,使用source ~/.bashrc命令,重新加载 .bashrc 文件,使其生效

系统级切换Python版本#

使用update-alternatives —list python命令,为整个系统更改Python版本

1.列出所有可用Python替代版本
  1. root@raspberrypi:~ # update-alternatives --list python
  2. /usr/bin/python2.7
  3. /usr/bin/python3.9
  4. /usr/local/bin/python3.10

2.添加替代版本列表

如果运行后出现错误信息:update-alternatives: error:no alternatives for python
则为没有更新替代版本列表,使用以下命令添加:

  1. #注意:update-alternatives --install
  2. #1.一般情况下,直接使用 /usr/bin/python 即可
  3. #2.即为需要更换的python
  4. #3.为需要添加的python版本的安装路径,可以在上文中确定
  5. #4.为优先级。数字越大,优先级越高
  6. root@raspberrypi:~ # update-alternatives --install /usr/bin/python python /usr/bin/python2.7 1
  7. update-alternatives: 使用 /usr/bin/python2.7 来在自动模式中提供 /usr/bin/python (python)
  8. root@raspberrypi:~ # update-alternatives --install /usr/bin/python python /usr/bin/python3.9 2
  9. update-alternatives: 使用 /usr/bin/python3.9 来在自动模式中提供 /usr/bin/python (python)
  10. root@raspberrypi:~ # update-alternatives --install /usr/bin/python python /usr/local/bin/python3.10 3
  11. update-alternatives: 使用 /usr/local/bin/python3.10 来在自动模式中提供 /usr/bin/python (python)

至此,系统已默认Python版本为3.10,验证如下:

  1. root@raspberrypi:~ # python
  2. Python 3.10.0 (default, Dec 5 2021, 22:46:09) [GCC 10.2.1 20210110] on linux
  3. Type "help", "copyright", "credits" or "license" for more information.

3.进行版本切换
使用update-alternatives —config python命令即可
  1. root@raspberrypi:~ # update-alternatives --config python
  2. 3 个候选项可用于替换 python (提供 /usr/bin/python)。
  3. 选择 路径 优先级 状态
  4. 0 /usr/local/bin/python3.10 3 自动模式
  5. 1 /usr/bin/python2.7 1 手动模式
  6. 2 /usr/bin/python3.9 2 手动模式
  7. 3 /usr/local/bin/python3.10 3 手动模式
  8. 要维持当前值[*]请按<回车键>,或者键入选择的编号:2
  9. update-alternatives: 使用 /usr/bin/python3.9 来在手动模式中提供 /usr/bin/python (python)
  10. root@raspberrypi:~ # python
  11. Python 3.9.2 (default, Feb 28 2021, 17:03:44)
  12. [GCC 10.2.1 20210110] on linux
  13. Type "help", "copyright", "credits" or "license" for more information.

参考链接:#
月灯依旧:怎样在Debian 10上安装Python 3.9
weixin39634876:python升级命令debian如何将 Debian Linux 中的默认的 Python 版本切换为替代版本
YanniZhang的博客:linux 查看python安装路径,版本号