Debian11编译安装升级Python311

写在前面:

备份pip模块:

1
pip freeze > requirements.txt

1、安装编译所需文件

1
sudo apt-get install libncursesw5-dev libssl-dev libsqlite3-dev tk-dev libgdbm-dev libc6-dev libbz2-dev libffi-dev zlib1g-dev libreadline-gplv2-dev

2、下载并解压python3.11.0

①下载

Python3.10.0 XZ compressed source tarball下载网址
【2022年11月19日编译安装python311版本】
Python3.11.0 TGZ compressed source tarball下载网址

②解压并编译安装

卸载之前安装的版本,因为没有指定安装路径,所以需要手动删除相关文件
重新编译安装,按相关路径删除相应文件

1
2
3
4
5
cd ~/local/Python-3.9.9/
# 根据以前编译版本解压路径
sudo ./configure --prefix=/tmp/to_remove && sudo make install
# 进入/tmp/to_remove
# 删除对应文件夹路径文件(因为/tmp/to_remove里的目录结构就是没有配置--prefix选项时的目录结构)。

重新编译安装最新版本python3.11.0

<1>编译安装openssl
下载地址penssl-1.1.1s

1
2
3
4
5
wget -P ~/local/ https://www.openssl.org/source/openssl-1.1.1s.tar.gz
tar -zxvf openssl-1.1.1s.tar.gz
cd openssl-1.1.1s
./config --prefix=/usr/local/openssl
sudo make && sudo make install

<2>编译安装python3.11

1
2
3
4
5
6
7
8
# $ xz -dk Python-3.11.0.tar.xz  // -k保留原压缩包
# $ tar -xvf Python-3.11.0.tar
wget -P ~/local/ https://www.python.org/ftp/python/3.11.0/Python-3.11.0.tgz
tar -zxvf Python-3.11.0.tar.tgz
cd Python-3.11.0/
cd Python-3.11.0
./configure --prefix=/usr/local/python/python3.11 --enable-optimizations --with-openssl=/usr/local/openssl --enable-shared
sudo make && sudo make altinstall

一些说明:
--enable-optimizations 启用优化
--with-openssl=/usr/local/openssl让编译生成的python支持ssl。务必带上这个,不然后面使用编译后的pip去下载包的时候,如果是公网,比如清华的,那就必须得支持ssl【带此编译可解决pip源https开头安装或者更新软件报错,即本文后面解决ssl问题】
--enable-shared是编译后,会生成python的库。如果需要使用pyinstall来编译可执行程序,那么需要带上这个选项。

注意,这里要用sudo,把python3.9安装到了/usr/local/lib下面,可执行文件在/usr/local/bin下面。
如果不用altinstall,就会安装到/usr/bin/下面,可能会直接覆盖系统原来的文件。
在当前最新的python3.10版中,用make install也是会安装在/usr/local/bin/下。

<3>将编译后python/lib下文件放入默认库/usr/lib/lib
因为带--enable-shared编译的,会自动生成一些.so结尾的库文件,将这些文件复制到系统默认库路径中。

1
sudo cp /usr/local/python/python3.11/lib/*.so* /usr/lib/

3、update-alternatives版本管理

可直接跳到2022年11月19日编译安装python311版本后切换版本

①为系统自带python2.7创建软连接

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
$ sudo update-alternatives --install /usr/bin/python python /usr/bin/python2.7 1
```
`--install` 选项使用了多个参数用于创建符号链接。

`update-alternatives`: 使用 `/usr/bin/python2.7` 来在自动模式中提供 `/usr/bin/python (python)`
```bash
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.9 2
# update-alternatives: 使用 /usr/bin/python3.9 来在自动模式中提供 /usr/bin/python (python)
whereis python3.10
# python3: /usr/bin/python3.9 /usr/bin/python3 /usr/lib/python3.9 /usr/lib/python3 /etc/python3.9 /etc/python3 /usr/local/bin/python3 /usr/local/bin/python3.10-config /usr/local/bin/python3.10 /usr/local/lib/python3.9 /usr/local/lib/python3.10 /usr/share/python3 /usr/share/man/man1/python3.1.gz
sudo update-alternatives --install /usr/bin/python python /usr/local/bin/python3.10 3
# update-alternatives: 使用 /usr/local/bin/python3.10 来在自动模式中提供 /usr/bin/python (python)
sudo update-alternatives --list python
# /usr/bin/python2.7
# /usr/bin/python3.9
# /usr/local/bin/python3.10
sudo update-alternatives --config python
# 有 3 个候选项可用于替换 python (提供 /usr/bin/python)。

选择 路径 优先级 状态
------------------------------------------------------------
* 0 /usr/local/bin/python3.10 3 自动模式
1 /usr/bin/python2.7 1 手动模式
2 /usr/bin/python3.9 2 手动模式
3 /usr/local/bin/python3.10 3 手动模式

# 要维持当前值[*]请按<回车键>,或者键入选择的编号:

②以下为切换3.9及切换为3.10实例:

1
2
3
4
5
6
7
8
9
10
11
12
sudo update-alternatives --config python
# 有 3 个候选项可用于替换 python (提供 /usr/bin/python)。

选择 路径 优先级 状态
------------------------------------------------------------
* 0 /usr/local/bin/python3.10 3 自动模式
1 /usr/bin/python2.7 1 手动模式
2 /usr/bin/python3.9 2 手动模式
3 /usr/local/bin/python3.10 3 手动模式

# 要维持当前值[*]请按<回车键>,或者键入选择的编号:2
# update-alternatives: 使用 /usr/bin/python3.9 来在手动模式中提供 /usr/bin/python (python)

查看所使用python版本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
python
# Python 3.9.2 (default, Feb 28 2021, 17:03:44)
# [GCC 10.2.1 20210110] on linux
#Type "help", "copyright", "credits" or "license" for more information.
#>>>
#[6]+ 已停止 python
sudo update-alternatives --config python
# 有 3 个候选项可用于替换 python (提供 /usr/bin/python)。

选择 路径 优先级 状态
------------------------------------------------------------
0 /usr/local/bin/python3.10 3 自动模式
1 /usr/bin/python2.7 1 手动模式
* 2 /usr/bin/python3.9 2 手动模式
3 /usr/local/bin/python3.10 3 手动模式

# 要维持当前值[*]请按<回车键>,或者键入选择的编号:0
# update-alternatives: 使用 /usr/local/bin/python3.10 来在自动模式中提供 /usr/bin/python (python)

再次查看版本:
1
2
3
4
5
python
# Python 3.10.0 (default, Oct 17 2021, 10:46:43) [GCC 10.2.1 20210110] on linux
# Type "help", "copyright", "credits" or "license" for more information.
#>>> ^Z
#[7]+ 已停止 python

修改pip版本【用update-alternatives切换版本会自动切换pip版本】
1
2
3
4
5
sudo vim /usr/bin/pip
# 修改:#!/usr/local/bin/python2.7
# 为: #!/usr/local/bin/python3.10
pip -V
# pip 21.3 from /home/你的电脑名/.local/lib/python3.10/site-packages/pip (python 3.10)


【2022年11月19日编译安装python311版本后切换版本】
如选择3.10版本:
1
sudo update-alternatives --config python

1
2
3
4
5
6
7
8
9
10
11
# 输出:
# 有 2 个候选项可用于替换 python (提供 /usr/bin/python)。

选择 路径 优先级 状态
------------------------------------------------------------
0 /usr/local/bin/python3.10 3 自动模式
1 /usr/local/bin/python3.10 3 手动模式
* 2 /usr/local/python/python3.11/bin/python3.11 1 手动模式

# 要维持当前值[*]请按<回车键>,或者键入选择的编号:1
# update-alternatives: 使用 /usr/local/bin/python3.10 来在手动模式中提供 /usr/bin/python (python)

查看pip版本
1
2
pip -V                                  
pip 22.3.1 from /home/你的电脑名/.local/lib/python3.10/site-packages/pip (python 3.10)

之后切换到3.11版本
1
2
3
4
5
6
7
8
9
10
11
sudo update-alternatives --config python
# 有 2 个候选项可用于替换 python (提供 /usr/bin/python)。

选择 路径 优先级 状态
------------------------------------------------------------
0 /usr/local/bin/python3.10 3 自动模式
* 1 /usr/local/bin/python3.10 3 手动模式
2 /usr/local/python/python3.11/bin/python3.11 1 手动模式

# 要维持当前值[*]请按<回车键>,或者键入选择的编号:2
# update-alternatives: 使用 /usr/local/python/python3.11/bin/python3.11 来在手动模式中提供 /usr/bin/python (python)

查看pip版本:
1
2
pip -V                                  
# pip 22.3 from /usr/local/python/python3.11/lib/python3.11/site-packages/pip (python 3.11)

③其他命令

更换pip

1
2
mkdir ~/.pip
vim ~/.pip/pip.conf

添加如下内容:

1
2
3
4
[global]
index-url = https://pypi.tuna.tsinghua.edu.cn/simple
[install]
trusted-host = pypi.tuna.tsinghua.edu.cn

临时使用安装源命令: pip install wheel -i https://pypi.tuna.tsinghua.edu.cn/simple

移除版本命令

sudo update-alternatives --remove python /usr/bin/python2.7

4、将模块路径添加到path环境并使其生效

1
2
echo 'export PATH=/home/你用户名/.local/bin:$PATH' >> ~/.bashrc
source ~/.bashrc

5、恢复pip模块

1
pip install -r /your/path/to/requirements.txt

更新模块

1
2
pip install pip-review
pip-review --local --interactive

注意python3.10的pip配置在~/.pip/pip.conf,若提示源ssl问题,注意url修改为https前缀。

一些错误的解决

安装源报错

python -m pip install --upgrade pip提示WARNING: pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available.缺少openssl模块错误。
解决方法:

<1>通过参数临时更换pip源:

1
pip install pipenv -i http://pypi.douban.com/simple --trusted-host pypi.douban.com

<2>配置源【以下方法是配置多个镜像源平衡负载】

1
2
3
pip config set global.extra-index-url "https://pypi.tuna.tsinghua.edu.cn/simple/ http://mirrors.aliyun.com/pypi/simple/"
pip config set install.extra-trusted-host "pypi.tuna.tsinghua.edu.cn mirrors.aliyun.com"
# 输出 Writing to /home/你用户名/.config/pip/pip.conf

<3> 编译时候带上--with-openssl=/usr/local/openssl参数【注意openssl是用编译安装软件的那个路径】

其他几个国内源地址【注意httphttps】:
(1)http://mirrors.aliyun.com/pypi/simple/ 阿里云(可以带https
(2)https://pypi.mirrors.ustc.edu.cn/simple/ 中国科技大学
(3)http://pypi.douban.com/simple/ 豆瓣
(4)https://pypi.tuna.tsinghua.edu.cn/simple/ 清华大学
(5)http://pypi.mirrors.ustc.edu.cn/simple/ 中国科学技术大学

apt_pkg报错

报错如下

1
2
3
  File "/usr/bin/apt-listchanges", line 29, in <module>
import apt_pkg
ModuleNotFoundError: No module named 'apt_pkg'

解决方法:
1
2
3
4
cd /usr/lib/python3/dist-packages/
# sudo apt install python3-apt
# sudo aptitude install python3-apt
sudo aptitude remove python3-dev # 用aptitude可以删除系统原有的相关的依赖包

复制
1
2
sudo cp apt_pkg.cpython-39-x86_64-linux-gnu.so  apt_pkg.cpython-311m-x86_64-linux-gnu.so
sudo ln -s apt_pkg.cpython-311m-x86_64-linux-gnu.so apt_pkg.so

如果报错,则执行下方命令,强制添加
1
sudo ln -fs apt_pkg.cpython-311m-x86_64-linux-gnu.so apt_pkg.so

6、具体参照:

update-alternatives将Linux下python默认版本切换成替代版本
python升级命令debian_更改Ubuntu或者Debian默认python版本的方法
Python3 ssl模块不可用的问题