参考https://pylist.com/t/1576042471
下面适合 Ubuntu 18.04, 16.04
add-apt-repository ppa:deadsnakes/ppa
下载源码
cd /opt
wget https://www.python.org/ftp/python/3.6.3/Python-3.6.3.tgz
解压缩
tar -xvf Python-3.6.3.tgz
编译安装
cd Python-3.6.3
./configure
make
make install
安装过程中如果遇到下面错误
zipimport.ZipImportError: can't decompress data; zlib not available
就安装依赖包
apt-get install zlib1g-dev
再次 make && make install
验证版本
# python3.6 -V
Python 3.6.3
现在系统中已经有3个 Python 版本
python -V
python3 -V
python3.6 -V
需要把 python3
命令转向 python3.6
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
会有下面提示:
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 to keep the current choice[*], or type selection number:
按回车就可以,验证版本
# python3 -V
Python 3.6.3
升级后使用 pip3
安装软件,会有下面错误
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
_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 里即可
# 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 正常
pip3 install 出现Could not fetch URL https://pypi.python.org/
原因是被强了参考https://www.cnblogs.com/jonnyan/p/9181031.html
过指定国内镜像源来安装: pip --trusted-host 镜像源 install 模块名 -i 镜像源路径
例如:pip --trusted-host pypi.doubanio.com install paramiko -i http://pypi.doubanio.com/simple
命令解释:
--trusted-host 指定可信源(忽略https的安全要求)
-i 指定镜像源路径
pipy国内镜像目前有:
http://pypi.douban.com/ 豆瓣
http://pypi.hustunique.com/ 华中理工大学
http://pypi.sdutlinux.org/ 山东理工大学
http://pypi.mirrors.ustc.edu.cn/ 中国科学技术大学
要配制成默认的话,需要创建或修改配置文件(linux的文件在~/.pip/pip.conf,windows在%HOMEPATH%\pip\pip.ini),修改内容为:
code:
[global]
index-url = http://pypi.douban.com/simple
这样在使用pip来安装时,会默认调用该镜像。