升级Python3.6

Reference:
https://www.jianshu.com/p/4fd4ff793125
https://blog.csdn.net/pengjian444/article/details/53039018
https://www.cnblogs.com/yikemogutou/p/12335186.html
https://www.lxx1.com/3214

切换源

众所周知的,先把源 切换成国内来节约时间,这里使用清华开源镜像站,直接根据帮助文档操作就好。

修改 /etc/apt/sources.list,注释掉原来的所有内容,用下面内容取代:

deb http://mirrors.tuna.tsinghua.edu.cn/raspbian/raspbian/ stretch main non-free contrib rpi
deb-src http://mirrors.tuna.tsinghua.edu.cn/raspbian/raspbian/ stretch main non-free contrib rpi

然后修改 /etc/apt/sources.list.d/raspi.list,用下面内容取代:

deb http://mirrors.tuna.tsinghua.edu.cn/raspberrypi/ stretch main ui
deb-src http://mirrors.tuna.tsinghua.edu.cn/raspberrypi/ stretch main ui

最后执行 sudo apt-get update 更新下软件源列表就好啦。

安装Python3.6

  1. 更新树莓派系统
$ sudo apt-get update
$ sudo apt-get upgrade -y
$ sudo apt-get dist-upgrade
  1. 安装python3.6依赖环境
$ sudo apt-get install build-essential libsqlite3-dev sqlite3 bzip2 libbz2-dev libssl-dev openssl libgdbm-dev liblzma-dev libreadline-dev libncursesw5-dev
  1. 下载python3.6版本源码并解压
$ wget https://www.python.org/ftp/python/3.6.3/Python-3.6.3.tar.xz
$ tar -xvf Python-3.6.3.tar.xz
$ cd Python-3.6.3
  1. 编译安装, 编译的过程比较长。
$ sudo ./configure
$ sudo make && sudo make install

5.建立软连接
安装python3.6后我们可以看一下python的版本

$ python --version
输出:Python 2.7.13
$ python3 --version
输出:Python 3.6.3
$ pip -V
输出:pip 9.0.1 from /usr/lib/python2.7/dist-packages (python 2.7)
$ pip3 -V
输出:pip 9.0.1 from /usr/lib/python3.6/dist-packages/pip (python 3.6)

接下来需要将Python 3.6.3软链接到python上,这样方便使用。
先看一下python3和python命令分别在哪

$ which python
输出:/usr/bin/python
$ which python3.5
输出:/usr/bin/python3.5
$ which python3.6
输出:/usr/local/bin/python3.6

然后建立软连接。使用update-alternatives工具比较方便。

sudo update-alternatives --install /usr/bin/python python /usr/bin/python2.7 1
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.5 2
sudo update-alternatives --install /usr/bin/python python /usr/local/bin/python3.6 3

最后数字为对应python版本优先级,数值越大,优先级越高。

 $ sudo update-alternatives --display python
python - auto mode
  link best version is /usr/local/bin/python3.6
  link currently points to /usr/local/bin/python3.6
  link python is /usr/bin/python
/usr/bin/python2 - priority 1
/usr/bin/python3.5 - priority 2
/usr/local/bin/python3.6 - priority 3

测试是否成功

$ python --version
输出:Python 3.6.3
$ pip -V
输出:pip 9.0.1 from /usr/local/lib/python3.6/site-packages/pip (python 3.6)

选择适当版本,快速切换至其他版本的python:

 $ sudo update-alternatives --config python
There are 3 choices for the alternative python (providing /usr/bin/python).

  Selection    Path                      Priority   Status
------------------------------------------------------------
* 0            /usr/local/bin/python3.6   3       auto mode
  1            /usr/bin/python2           1       manual mode
  2            /usr/bin/python3.5           2       manual mode
  3            /usr/local/bin/python3.6   3       manual mode
Press <enter> to keep the current choice[*], or type selection number:

输入你想要默认的Python版本对应的数字编号1,2或3即可.

sudo apt-get install python3-pip

1 若报 No module named ‘distutils.util’,执行 sudo apt-get install python3-distutils
2 报E: Package python3-distutils has no installation candidate,执行: sudo apt update
3 重新执行:sudo apt-get install python3-distutils
4 执行:sudo apt-get install pyton3-pip
5 pip3 -V 成功!!
————————————————

pip3 安装library出错

Ref : https://www.cnblogs.com/presleyren/p/13518799.html

  1. pip安装任何东西都显示subprocess.CalledProcessError: Command '('lsb_release', '-a')' returned non-zero exit status 1.

解决办法:

$ lsb_release -a   # 使用命令查看报错详情
Traceback (most recent call last):
  File "/usr/bin/lsb_release", line 25, in <module>    # 这个路径很重要
    import lsb_release
ModuleNotFoundError: No module named 'lsb_release'

# 将系统完整的lsb_release.py文件拷贝到报错的目录文件下即可
sudo cp /usr/lib/python3/dist-packages/lsb_release.py /usr/bin/

安装报Could not fetch URL
Ref: https://www.cnblogs.com/eagleking/p/10908608.html

  1. Could not fetch URL https://pypi.org/simple/pytest-pycodestyle/: There was a problem confirming the ssl certificate: HTTPSConnectionPool(host='pypi.org', port=443): Max retries exceeded with url: /simple/pytest-pycodestyle/ (Caused by SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self signed certificate in certificate chain (_ssl.c:1056)'))) - skipping

解决办法:

pip install 插件名 -i 国内镜像地址
举例
pip install pytest -i http://pypi.douban.com/simple --trusted-host pypi.douban.com

常用国内镜像地址

  1. http://mirrors.aliyun.com/pypi/simple/ 阿里云
  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/ 中国科学技术大学

安装 lxml 报错

  1. Error: Please make sure the libxml2 and libxslt development packages are installed.

解决办法:

# 先安装需要的 library
$ sudo apt-get install python-dev libxml2-dev libxslt-dev
# 然后再重新安装 xlml
$ sudo pip3 install lxml -i http://mirrors.aliyun.com/pypi/simple --trusted-host mirrors.aliyun.com
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。