第1.2.1章 树莓派python编程环境

时间:2024-11-12 12:14:48

树莓派3B+,默认的python版本是2.7.13,但是操作系统自带了python3.5.3的IDE
12
因为现在主流都是用python3,那么我怎么讲python的环境切换到3呢?
1 python环境切换
参考将树莓派内置的 Python2.7 升级成 Python3

# 卸载 python2.7
sudo apt remove python
# 自动清理 python2.7 的依赖
sudo apt autoremove

因为树莓派3B+自带python3,所以只需要执行,即可。

sudo ln -s /usr/bin/python3.5 /usr/bin/python
sudo ln -s /usr/bin/pip3 /usr/bin/pip

如果没有python3,则执行sudo apt install python3进行安装
2 安装其他插件
直接安装文件可能会出错,比如pip install pandas,就会提示,这个时候参考章节3.1更换pypi国内源就可以了。
3 异常
3.1 更换pypi国内源
下面这个异常OpenSSL的错误,解决方案参考解决pip安装出现sslerror的问题
执行命令pip install pandas -i /simple --trusted-host
安装成功,pandas就是想安装的软件.
更换pycharm源,这上面说豆瓣的源是最好用的。
那么也就是说,我需要先更改源,再安装就不用后面带那么一长串了。
配置豆瓣源,参考PyPI使用国内源,树莓派的配置文件在/etc/

nano /etc/ 
# 添加如下内容
[global]
#extra-index-url=/simple
index-url = /simple
[install]
trusted-host=              
root@raspberrypi:/usr/bin# pip install pandas
Collecting pandas
Exception:
Traceback (most recent call last):
  File "/usr/share/python-wheels/urllib3-1.19.1-py2./urllib3/contrib/", line 417, in wrap_socket
    cnx.do_handshake()
  File "/usr/lib/python3/dist-packages/OpenSSL/", line 1426, in do_handshake
    self._raise_ssl_error(self._ssl, result)
  File "/usr/lib/python3/dist-packages/OpenSSL/", line 1149, in _raise_ssl_error
    raise WantReadError()


During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/share/python-wheels/urllib3-1.19.1-py2./urllib3/", line 350, in _make_request
    self._validate_conn(conn)
  File "/usr/share/python-wheels/urllib3-1.19.1-py2./urllib3/", line 837, in _validate_conn
    ()
  File "/usr/share/python-wheels/urllib3-1.19.1-py2./urllib3/", line 323, in connect
    ssl_context=context)
  File "/usr/share/python-wheels/urllib3-1.19.1-py2./urllib3/util/ssl_.py", line 324, in ssl_wrap_socket
    return context.wrap_socket(sock, server_hostname=server_hostname)
  File "/usr/share/python-wheels/urllib3-1.19.1-py2./urllib3/contrib/", line 421, in wrap_socket
    raise timeout('select timed out')
: select timed out

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/share/python-wheels/urllib3-1.19.1-py2./urllib3/", line 594, in urlopen
    chunked=chunked)
  File "/usr/share/python-wheels/urllib3-1.19.1-py2./urllib3/", line 353, in _make_request
    self._raise_timeout(err=e, url=url, timeout_value=)
  File "/usr/share/python-wheels/urllib3-1.19.1-py2./urllib3/", line 313, in _raise_timeout
    raise ReadTimeoutError(self, url, "Read timed out. (read timeout=%s)" % timeout_value)
.: HTTPSConnectionPool(host='', port=443): Read timed out. (read timeout=15)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/pip/", line 215, in main
    status = (options, args)
  File "/usr/lib/python3/dist-packages/pip/commands/", line 353, in run
    (autobuilding=True)
  File "/usr/lib/python3/dist-packages/pip/", line 749, in build
    self.requirement_set.prepare_files()
  File "/usr/lib/python3/dist-packages/pip/req/req_set.py", line 380, in prepare_files
    ignore_dependencies=self.ignore_dependencies))
  File "/usr/lib/python3/dist-packages/pip/req/req_set.py", line 554, in _prepare_file
    require_hashes
  File "/usr/lib/python3/dist-packages/pip/req/req_install.py", line 278, in populate_link
     = finder.find_requirement(self, upgrade)
  File "/usr/lib/python3/dist-packages/pip/", line 465, in find_requirement
    all_candidates = self.find_all_candidates()
  File "/usr/lib/python3/dist-packages/pip/", line 423, in find_all_candidates
    for page in self._get_pages(url_locations, project_name):
  File "/usr/lib/python3/dist-packages/pip/", line 568, in _get_pages
    page = self._get_page(location)
  File "/usr/lib/python3/dist-packages/pip/", line 683, in _get_page
    return HTMLPage.get_page(link, session=)
  File "/usr/lib/python3/dist-packages/pip/", line 792, in get_page
    "Cache-Control": "max-age=600",
  File "/usr/share/python-wheels/requests-2.12.4-py2./requests/", line 501, in get
    return ('GET', url, **kwargs)
  File "/usr/lib/python3/dist-packages/pip/", line 386, in request
    return super(PipSession, self).request(method, url, *args, **kwargs)
  File "/usr/share/python-wheels/requests-2.12.4-py2./requests/", line 488, in request
    resp = (prep, **send_kwargs)
  File "/usr/share/python-wheels/requests-2.12.4-py2./requests/", line 609, in send
    r = (request, **kwargs)
  File "/usr/share/python-wheels/CacheControl-0.11.7-py2./cachecontrol/", line 47, in send
    resp = super(CacheControlAdapter, self).send(request, **kw)
  File "/usr/share/python-wheels/requests-2.12.4-py2./requests/", line 423, in send
    timeout=timeout
  File "/usr/share/python-wheels/urllib3-1.19.1-py2./urllib3/", line 643, in urlopen
    _stacktrace=sys.exc_info()[2])
  File "/usr/share/python-wheels/urllib3-1.19.1-py2./urllib3/util/", line 315, in increment
    total -= 1
TypeError: unsupported operand type(s) for -=: 'Retry' and 'int'

3.2 更新树莓派的源
树莓派安装lxml包后报错(.1: cannot open shared object file),但是执行sudo apt-get install python3-lxml提示错误,莫不是要我把树莓派的源给更改了,于是尝试
参考树莓派3B+ 软件源更改
更换树莓派软件源(stretch 版本的) 但是提示下面的异常,查看配置并没有错误,而且互联网上面的资料都是这个中国科技大学的源,那么问题出在哪里呢?既然提示网络需要认证,那么我尝试将源改成https的,结果果然成功

sudo nano /etc/apt/
# 添加下面的源
deb /raspbian/raspbian/ stretch main contrib non-free rpi

sudo nano /etc/apt//
# 添加下面的源
deb // stretch main ui

sudo apt-get update -y 
sudo apt-get dist-upgrade -y

1

root@raspberrypi:~# sudo apt update && sudo apt -y upgrade
获取:1 /raspbian/raspbian stretch InRelease [2,281 B]
错误:1 /raspbian/raspbian stretch InRelease                   
  明文签署文件不可用,结果为‘NOSPLIT’(您的网络需要认证吗?)
获取:2 / stretch InRelease [2,281 B]   
错误:2 / stretch InRelease
  明文签署文件不可用,结果为‘NOSPLIT’(您的网络需要认证吗?)
已下载 4,562 B,耗时 5秒 (836 B/s)
正在读取软件包列表... 完成
E: 无法下载 /raspbian/raspbian/dists/stretch/InRelease  明文签署文件不可用,结果为‘NOSPLIT’(您的网络需要认证吗?)
E: 无法下载 //dists/stretch/InRelease  明文签署文件不可用,结果为‘NOSPLIT’(您的网络需要认证吗?)
E: 部分索引文件下载失败。如果忽略它们,那将转而使用旧的索引文件
root@raspberrypi:~# sudo apt-get install python3-lxml
正在读取软件包列表... 完成
正在分析软件包的依赖关系树       
正在读取状态信息... 完成       
将会同时安装下列软件:
  python3-bs4 python3-html5lib python3-webencodings
建议安装:
  python3-genshi python3-lxml-dbg python-lxml-doc
下列【新】软件包将被安装:
  python3-bs4 python3-html5lib python3-lxml python3-webencodings
升级了 0 个软件包,新安装了 4 个软件包,要卸载 0 个软件包,有 0 个软件包未被升级。
需要下载 975 kB 的归档。
解压缩后会消耗 4,077 kB 的额外空间。
您希望继续执行吗? [Y/n] y
获取:1 /raspbian stretch/main armhf python3-bs4 all 4.5.3-1 [86.6 kB]
获取:2 /raspbian stretch/main armhf python3-webencodings all 0.5-2 [10.4 kB]
错误:1 /raspbian stretch/main armhf python3-bs4 all 4.5.3-1     
  Hash 校验和不符
  Hashes of expected file:
   - SHA256:01cc4be4b93254c986f9f08dc331b212dd2d69ac9399115490d82056c510666c
   - SHA1:d9a1b3bedbc58fc40c9b54f86b025a3117d16f8a [weak]
   - MD5Sum:88cc1efca381156284b161f74652669b [weak]
   - Filesize:86584 [weak]
  Hashes of received file:
   - SHA256:138422fcd39b082e6defe9212cd3e4f9bf0ab2414cd8067082e472e859c77d69
   - SHA1:862fe9be5c55082c173de5888fa1aca481e18bbf [weak]
   - MD5Sum:25bd48a6da28d0583c181a4abcba6a4a [weak]
   - Filesize:2281 [weak]
  Last modification reported: Wed, 12 Oct 2016 01:25:02 +0000

3.3 make sure the development packages of libxml2 and libxslt are installed
pip install lxml的时候,提示下面的异常,这个问题需要配置树莓派的源,参见章节3.2

Building wheels for collected packages: lxml, Twisted, PyDispatcher, 
  Running  bdist_wheel for lxml ... error
  Complete output from command /usr/bin/python3 -u -c "import setuptools, tokenize;__file__='/tmp/pip-build-on0bo_yv/lxml/';f=getattr(tokenize, 'open', open)(__file__);code=().replace('\r\n', '\n');();exec(compile(code, __file__, 'exec'))" bdist_wheel -d /tmp/tmpgp7_bktvpip-wheel- --python-tag cp35:
  Building lxml version 4.3.1.
  Building without Cython.
  ERROR: b'/bin/sh: 1: xslt-config: not found\n'
  ** make sure the development packages of libxml2 and libxslt are installed **

3.4 error: command ‘arm-linux-gnueabihf-gcc’ failed with exit status 1
pip install lxml的时候,提示下面的异常,解决方案是参考’arm-linux-gnueabihf-gcc’ failed就高兴早了,最终参考 解决使用pip安装lxml包报错问题才得以解决

sudo apt-get install python3-dev libxml2-dev libxslt1-dev zlib1g-dev
mat-security -Wdate-time -D_FORTIFY_SOURCE=2 -fPIC -DCYTHON_CLINE_IN_TRACEBACK=0 -Isrc -Isrc/lxml/includes -I/usr/include/python3.5m -c src/lxml/ -o build/-armv7l-3.5/src/lxml/ -w
    In file included from src/lxml/:687:0:
    src/lxml/includes/etree_defs.h:14:31: fatal error: libxml/: 没有那个文件或目录
     #include "libxml/"
                                   ^
    compilation terminated.
    Compile failed: command 'arm-linux-gnueabihf-gcc' failed with exit status 1
    cc -I/usr/include/libxml2 -c /tmp/ -o tmp/
    /tmp/:1:26: fatal error: libxml/: 没有那个文件或目录
     #include "libxml/"
                              ^
    compilation terminated.
    *********************************************************************************
    Could not find function xmlCheckVersion in library libxml2. Is libxml2 installed?
    *********************************************************************************
    error: command 'arm-linux-gnueabihf-gcc' failed with exit status 1

3.5 sudo apt-get install libevent-dev 安装失败
出现这个原因,是因为没有执行sudo apt-get dist-upgrade -y

root@raspberrypi:~# sudo apt-get install libevent-dev
正在读取软件包列表... 完成
正在分析软件包的依赖关系树       
正在读取状态信息... 完成       
将会同时安装下列软件:
  libevent-core-2.0-5 libevent-extra-2.0-5 libevent-openssl-2.0-5 libevent-pthreads-2.0-5
下列【新】软件包将被安装:
  libevent-core-2.0-5 libevent-dev libevent-extra-2.0-5 libevent-openssl-2.0-5 libevent-pthreads-2.0-5
升级了 0 个软件包,新安装了 5 个软件包,要卸载 0 个软件包,有 88 个软件包未被升级。
需要下载 492 kB 的归档。
解压缩后会消耗 1,622 kB 的额外空间。
您希望继续执行吗? [Y/n] y
错误:1 /raspbian/raspbian stretch/main armhf libevent-core-2.0-5 armhf 2.0.21-stable-3
  Could not resolve host: 
错误:2 /raspbian/raspbian stretch/main armhf libevent-extra-2.0-5 armhf 2.0.21-stable-3
  Could not resolve host: 
获取:3 /raspbian/raspbian stretch/main armhf libevent-pthreads-2.0-5 armhf 2.0.21-stable-3 [43.6 kB]
获取:4 /raspbian/raspbian stretch/main armhf libevent-openssl-2.0-5 armhf 2.0.21-stable-3 [48.1 kB]
获取:5 /raspbian/raspbian stretch/main armhf libevent-dev armhf 2.0.21-stable-3 [221 kB]
已下载 313 kB,耗时 46秒 (6,689 B/s)
E: 无法下载 /raspbian/raspbian/pool/main/libe/libevent/libevent-core-2.0-5_2.0.21-stable-3_armhf.deb  Could not resolve host: 
E: 无法下载 /raspbian/raspbian/pool/main/libe/libevent/libevent-extra-2.0-5_2.0.21-stable-3_armhf.deb  Could not resolve host: 
E: 有几个软件包无法下载,要不运行 apt-get update 或者加上 --fix-missing 的选项再试试?