当前位置:   article > 正文

第1.2.1章 树莓派python编程环境

树莓派python编程

树莓派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
  • 1
  • 2
  • 3
  • 4

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

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

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

nano /etc/pip.conf 
# 添加如下内容
[global]
#extra-index-url=https://www.piwheels.org/simple
index-url = http://pypi.douban.com/simple
[install]
trusted-host=pypi.douban.com              
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
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.py3-none-any.whl/urllib3/contrib/pyopenssl.py", line 417, in wrap_socket
    cnx.do_handshake()
  File "/usr/lib/python3/dist-packages/OpenSSL/SSL.py", line 1426, in do_handshake
    self._raise_ssl_error(self._ssl, result)
  File "/usr/lib/python3/dist-packages/OpenSSL/SSL.py", line 1149, in _raise_ssl_error
    raise WantReadError()
OpenSSL.SSL.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.py3-none-any.whl/urllib3/connectionpool.py", line 350, in _make_request
    self._validate_conn(conn)
  File "/usr/share/python-wheels/urllib3-1.19.1-py2.py3-none-any.whl/urllib3/connectionpool.py", line 837, in _validate_conn
    conn.connect()
  File "/usr/share/python-wheels/urllib3-1.19.1-py2.py3-none-any.whl/urllib3/connection.py", line 323, in connect
    ssl_context=context)
  File "/usr/share/python-wheels/urllib3-1.19.1-py2.py3-none-any.whl/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.py3-none-any.whl/urllib3/contrib/pyopenssl.py", line 421, in wrap_socket
    raise timeout('select timed out')
socket.timeout: 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.py3-none-any.whl/urllib3/connectionpool.py", line 594, in urlopen
    chunked=chunked)
  File "/usr/share/python-wheels/urllib3-1.19.1-py2.py3-none-any.whl/urllib3/connectionpool.py", line 353, in _make_request
    self._raise_timeout(err=e, url=url, timeout_value=conn.timeout)
  File "/usr/share/python-wheels/urllib3-1.19.1-py2.py3-none-any.whl/urllib3/connectionpool.py", line 313, in _raise_timeout
    raise ReadTimeoutError(self, url, "Read timed out. (read timeout=%s)" % timeout_value)
requests.packages.urllib3.exceptions.ReadTimeoutError: HTTPSConnectionPool(host='www.piwheels.org', 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/basecommand.py", line 215, in main
    status = self.run(options, args)
  File "/usr/lib/python3/dist-packages/pip/commands/install.py", line 353, in run
    wb.build(autobuilding=True)
  File "/usr/lib/python3/dist-packages/pip/wheel.py", line 749, in build
    self.requirement_set.prepare_files(self.finder)
  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
    self.link = finder.find_requirement(self, upgrade)
  File "/usr/lib/python3/dist-packages/pip/index.py", line 465, in find_requirement
    all_candidates = self.find_all_candidates(req.name)
  File "/usr/lib/python3/dist-packages/pip/index.py", line 423, in find_all_candidates
    for page in self._get_pages(url_locations, project_name):
  File "/usr/lib/python3/dist-packages/pip/index.py", line 568, in _get_pages
    page = self._get_page(location)
  File "/usr/lib/python3/dist-packages/pip/index.py", line 683, in _get_page
    return HTMLPage.get_page(link, session=self.session)
  File "/usr/lib/python3/dist-packages/pip/index.py", line 792, in get_page
    "Cache-Control": "max-age=600",
  File "/usr/share/python-wheels/requests-2.12.4-py2.py3-none-any.whl/requests/sessions.py", line 501, in get
    return self.request('GET', url, **kwargs)
  File "/usr/lib/python3/dist-packages/pip/download.py", line 386, in request
    return super(PipSession, self).request(method, url, *args, **kwargs)
  File "/usr/share/python-wheels/requests-2.12.4-py2.py3-none-any.whl/requests/sessions.py", line 488, in request
    resp = self.send(prep, **send_kwargs)
  File "/usr/share/python-wheels/requests-2.12.4-py2.py3-none-any.whl/requests/sessions.py", line 609, in send
    r = adapter.send(request, **kwargs)
  File "/usr/share/python-wheels/CacheControl-0.11.7-py2.py3-none-any.whl/cachecontrol/adapter.py", line 47, in send
    resp = super(CacheControlAdapter, self).send(request, **kw)
  File "/usr/share/python-wheels/requests-2.12.4-py2.py3-none-any.whl/requests/adapters.py", line 423, in send
    timeout=timeout
  File "/usr/share/python-wheels/urllib3-1.19.1-py2.py3-none-any.whl/urllib3/connectionpool.py", line 643, in urlopen
    _stacktrace=sys.exc_info()[2])
  File "/usr/share/python-wheels/urllib3-1.19.1-py2.py3-none-any.whl/urllib3/util/retry.py", line 315, in increment
    total -= 1
TypeError: unsupported operand type(s) for -=: 'Retry' and 'int'
  • 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
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80

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

sudo nano /etc/apt/sources.list
# 添加下面的源
deb https://mirrors.ustc.edu.cn/raspbian/raspbian/ stretch main contrib non-free rpi

sudo nano /etc/apt/sources.list.d/raspi.list
# 添加下面的源
deb https://mirrors.ustc.edu.cn/archive.raspberrypi.org/ stretch main ui

sudo apt-get update -y 
sudo apt-get dist-upgrade -y
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

1

root@raspberrypi:~# sudo apt update && sudo apt -y upgrade
获取:1 http://mirrors.ustc.edu.cn/raspbian/raspbian stretch InRelease [2,281 B]
错误:1 http://mirrors.ustc.edu.cn/raspbian/raspbian stretch InRelease                   
  明文签署文件不可用,结果为‘NOSPLIT’(您的网络需要认证吗?)
获取:2 http://mirrors.ustc.edu.cn/archive.raspberrypi.org stretch InRelease [2,281 B]   
错误:2 http://mirrors.ustc.edu.cn/archive.raspberrypi.org stretch InRelease
  明文签署文件不可用,结果为‘NOSPLIT’(您的网络需要认证吗?)
已下载 4,562 B,耗时 5秒 (836 B/s)
正在读取软件包列表... 完成
E: 无法下载 http://mirrors.ustc.edu.cn/raspbian/raspbian/dists/stretch/InRelease  明文签署文件不可用,结果为‘NOSPLIT’(您的网络需要认证吗?)
E: 无法下载 http://mirrors.ustc.edu.cn/archive.raspberrypi.org/dists/stretch/InRelease  明文签署文件不可用,结果为‘NOSPLIT’(您的网络需要认证吗?)
E: 部分索引文件下载失败。如果忽略它们,那将转而使用旧的索引文件
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
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 http://raspbian.raspberrypi.org/raspbian stretch/main armhf python3-bs4 all 4.5.3-1 [86.6 kB]
获取:2 http://raspbian.raspberrypi.org/raspbian stretch/main armhf python3-webencodings all 0.5-2 [10.4 kB]
错误:1 http://raspbian.raspberrypi.org/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
  • 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
  • 28
  • 29

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, zope.interface
  Running setup.py bdist_wheel for lxml ... error
  Complete output from command /usr/bin/python3 -u -c "import setuptools, tokenize;__file__='/tmp/pip-build-on0bo_yv/lxml/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();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 **
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

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
  • 1
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/etree.c -o build/temp.linux-armv7l-3.5/src/lxml/etree.o -w
    In file included from src/lxml/etree.c:687:0:
    src/lxml/includes/etree_defs.h:14:31: fatal error: libxml/xmlversion.h: 没有那个文件或目录
     #include "libxml/xmlversion.h"
                                   ^
    compilation terminated.
    Compile failed: command 'arm-linux-gnueabihf-gcc' failed with exit status 1
    cc -I/usr/include/libxml2 -c /tmp/xmlXPathInitt9k2qdli.c -o tmp/xmlXPathInitt9k2qdli.o
    /tmp/xmlXPathInitt9k2qdli.c:1:26: fatal error: libxml/xpath.h: 没有那个文件或目录
     #include "libxml/xpath.h"
                              ^
    compilation terminated.
    *********************************************************************************
    Could not find function xmlCheckVersion in library libxml2. Is libxml2 installed?
    *********************************************************************************
    error: command 'arm-linux-gnueabihf-gcc' failed with exit status 1
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

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 https://mirrors.ustc.edu.cn/raspbian/raspbian stretch/main armhf libevent-core-2.0-5 armhf 2.0.21-stable-3
  Could not resolve host: mirrors.ustc.edu.cn
错误:2 https://mirrors.ustc.edu.cn/raspbian/raspbian stretch/main armhf libevent-extra-2.0-5 armhf 2.0.21-stable-3
  Could not resolve host: mirrors.ustc.edu.cn
获取:3 https://mirrors.ustc.edu.cn/raspbian/raspbian stretch/main armhf libevent-pthreads-2.0-5 armhf 2.0.21-stable-3 [43.6 kB]
获取:4 https://mirrors.ustc.edu.cn/raspbian/raspbian stretch/main armhf libevent-openssl-2.0-5 armhf 2.0.21-stable-3 [48.1 kB]
获取:5 https://mirrors.ustc.edu.cn/raspbian/raspbian stretch/main armhf libevent-dev armhf 2.0.21-stable-3 [221 kB]
已下载 313 kB,耗时 46秒 (6,689 B/s)
E: 无法下载 https://mirrors.ustc.edu.cn/raspbian/raspbian/pool/main/libe/libevent/libevent-core-2.0-5_2.0.21-stable-3_armhf.deb  Could not resolve host: mirrors.ustc.edu.cn
E: 无法下载 https://mirrors.ustc.edu.cn/raspbian/raspbian/pool/main/libe/libevent/libevent-extra-2.0-5_2.0.21-stable-3_armhf.deb  Could not resolve host: mirrors.ustc.edu.cn
E: 有几个软件包无法下载,要不运行 apt-get update 或者加上 --fix-missing 的选项再试试?
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/70895?site
推荐阅读
相关标签
  

闽ICP备14008679号