在 Ubuntu 16.04.6 LTS 系统上安装 Python 3.6.3

自己的阿里云一个 VPS 用的是系统 Ubuntu 16.04.6 LTS,自带的python版本是 2.7.123.5.2,有时候要用到 python 3.6,又不想卸掉原来版本。下面介绍安装 python 3.6.3 的过程,因为版本较旧,遇到一些坑,这里记录一下。

在 Ubuntu 16.04.6 LTS 系统上安装 Python 3.6.3

从 PPA 安装

安装必要的依赖

1
apt-get install software-properties-common python-software-properties

添加

1
add-apt-repository ppa:jonathonf/python-3.6

会有提示:

1
2
3
4
5
6
7
This PPA has been removed from public access as part of a protest against the abuse of open-source projects by large companies. For more detail visit the main page here: https://launchpad.net/~jonathonf

If you are a company and you would like this PPA to continue then let me know your preferred route for contributions and I will arrange something.

If we have already been in contact then ping me your Launchpad ID and I will add you to a private PPA in the meantime.
 More info: https://launchpad.net/~jonathonf/+archive/ubuntu/python-3.6
Press [ENTER] to continue or ctrl-c to cancel adding it

按回车继续

由于该系统比较旧, apt-get update 时发现有好多 404

apt-get update

尝试安装

Go:
1
2
3
4
5
6
7
# apt-get install python3.6
Reading package lists... Done
Building dependency tree       
Reading state information... Done
E: Unable to locate package python3.6
E: Couldn't find any package by glob 'python3.6'
E: Couldn't find any package by regex 'python3.6'

补充

下面适合 Ubuntu 18.04, 16.04

1
add-apt-repository ppa:deadsnakes/ppa

从源码安装

下载源码

1
2
cd /opt
wget https://www.python.org/ftp/python/3.6.3/Python-3.6.3.tgz

解压缩

1
tar -xvf Python-3.6.3.tgz

编译安装

1
2
3
4
cd Python-3.6.3
./configure
make
make install

安装过程中如果遇到下面错误

1
zipimport.ZipImportError: can't decompress data; zlib not available

就安装依赖包

1
apt-get install zlib1g-dev

再次 make && make install

验证版本

1
2
# python3.6 -V
Python 3.6.3

版本替换

现在系统中已经有3个 Python 版本

1
2
3
python -V
python3 -V
python3.6 -V

需要把 python3 命令转向 python3.6

1
2
3
update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.5 1
update-alternatives --install /usr/bin/python3 python3 /usr/local/bin/python3.6 2
update-alternatives --config python3

会有下面提示:

1
2
3
4
5
6
7
8
9
There are 2 choices for the alternative python3 (providing /usr/bin/python3).

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

Press <enter> to keep the current choice[*], or type selection number: 

按回车就可以,验证版本

1
2
# python3 -V
Python 3.6.3

pip3 问题

升级后使用 pip3 安装软件,会有下面错误

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
Exception:
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/site-packages/pip/basecommand.py", line 215, in main
    status = self.run(options, args)
  File "/usr/local/lib/python3.6/site-packages/pip/commands/install.py", line 272, in run
    with self._build_session(options) as session:
  File "/usr/local/lib/python3.6/site-packages/pip/basecommand.py", line 72, in _build_session
    insecure_hosts=options.trusted_hosts,
  File "/usr/local/lib/python3.6/site-packages/pip/download.py", line 329, in __init__
    self.headers["User-Agent"] = user_agent()
  File "/usr/local/lib/python3.6/site-packages/pip/download.py", line 93, in user_agent
    from pip._vendor import distro
  File "/usr/local/lib/python3.6/site-packages/pip/_vendor/distro.py", line 1050, in <module>
    _distro = LinuxDistribution()
  File "/usr/local/lib/python3.6/site-packages/pip/_vendor/distro.py", line 594, in __init__
    if include_lsb else {}
  File "/usr/local/lib/python3.6/site-packages/pip/_vendor/distro.py", line 931, in _get_lsb_release_info
    raise subprocess.CalledProcessError(code, cmd, stdout, stderr)
subprocess.CalledProcessError: Command 'lsb_release -a' returned non-zero exit status 1.

重点是 lsb_release,查找 lsb_release.py 并把它复制到 python3.6 的 lib 里即可

1
2
3
4
5
6
7
8
# find / -name 'lsb_release.py'
/usr/lib/python3/dist-packages/lsb_release.py
/usr/lib/python2.7/dist-packages/lsb_release.py
/usr/share/pyshared/lsb_release.py
# find /usr -name python3.6
/usr/local/bin/python3.6
/usr/local/lib/python3.6
# cp /usr/lib/python3/dist-packages/lsb_release.py /usr/local/lib/python3.6/

pip3 install 正常

本文网址: https://pylist.com/topic/193.html 转摘请注明来源

Suggested Topics

python 半角全角的相互转换

全角与半角在中文输入法里经常要接触到,后台在处理用户输入数据时需要对半角全角的相互转换。下面是python 实现的半角全角的相互转换功能。...

3行 Python 代码解简单的一元一次方程

一元一次方程:只含有一个未知数(即“元”),并且未知数的最高次数为1(即“次”)的整式方程叫做一元一次方程(英文名:`linear equation with one unknown`)。...

python 解析电子书的信息

epub 书是可供人们下载的开放性资源格式的电子图书。epub 文件通常与类似亚马逊Kindle 这样的电子阅读器不兼容。...

Leave a Comment

2 thoughts on "在 Ubuntu 16.04.6 LTS 系统上安装 Python 3.6.3"

#1 hldh214 says:

This PPA has been removed from public access as part of a protest against the abuse of open-source projects by large companies. For more detail visit the main page here: https://launchpad.net/~jonathonf

其实提示写的很清楚了

#2 pylist says:

@hldh214 有个项目叫 deadsnakes ,收集各版 python,从2.3到3.9,而且还紧随 Python 官方进行更新

https://github.com/deadsnakes

1
add-apt-repository ppa:deadsnakes/ppa