第1.2.1章 树莓派python编程环境

article/2025/8/26 14:46:28

树莓派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,就会提示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              
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_socketcnx.do_handshake()File "/usr/lib/python3/dist-packages/OpenSSL/SSL.py", line 1426, in do_handshakeself._raise_ssl_error(self._ssl, result)File "/usr/lib/python3/dist-packages/OpenSSL/SSL.py", line 1149, in _raise_ssl_errorraise WantReadError()
OpenSSL.SSL.WantReadErrorDuring 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_requestself._validate_conn(conn)File "/usr/share/python-wheels/urllib3-1.19.1-py2.py3-none-any.whl/urllib3/connectionpool.py", line 837, in _validate_connconn.connect()File "/usr/share/python-wheels/urllib3-1.19.1-py2.py3-none-any.whl/urllib3/connection.py", line 323, in connectssl_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_socketreturn 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_socketraise timeout('select timed out')
socket.timeout: select timed outDuring 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 urlopenchunked=chunked)File "/usr/share/python-wheels/urllib3-1.19.1-py2.py3-none-any.whl/urllib3/connectionpool.py", line 353, in _make_requestself._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_timeoutraise 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 mainstatus = self.run(options, args)File "/usr/lib/python3/dist-packages/pip/commands/install.py", line 353, in runwb.build(autobuilding=True)File "/usr/lib/python3/dist-packages/pip/wheel.py", line 749, in buildself.requirement_set.prepare_files(self.finder)File "/usr/lib/python3/dist-packages/pip/req/req_set.py", line 380, in prepare_filesignore_dependencies=self.ignore_dependencies))File "/usr/lib/python3/dist-packages/pip/req/req_set.py", line 554, in _prepare_filerequire_hashesFile "/usr/lib/python3/dist-packages/pip/req/req_install.py", line 278, in populate_linkself.link = finder.find_requirement(self, upgrade)File "/usr/lib/python3/dist-packages/pip/index.py", line 465, in find_requirementall_candidates = self.find_all_candidates(req.name)File "/usr/lib/python3/dist-packages/pip/index.py", line 423, in find_all_candidatesfor page in self._get_pages(url_locations, project_name):File "/usr/lib/python3/dist-packages/pip/index.py", line 568, in _get_pagespage = self._get_page(location)File "/usr/lib/python3/dist-packages/pip/index.py", line 683, in _get_pagereturn 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 getreturn self.request('GET', url, **kwargs)File "/usr/lib/python3/dist-packages/pip/download.py", line 386, in requestreturn 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 requestresp = 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 sendr = adapter.send(request, **kwargs)File "/usr/share/python-wheels/CacheControl-0.11.7-py2.py3-none-any.whl/cachecontrol/adapter.py", line 47, in sendresp = 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 sendtimeout=timeoutFile "/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 incrementtotal -= 1
TypeError: unsupported operand type(s) for -=: 'Retry' and 'int'

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 rpisudo nano /etc/apt/sources.list.d/raspi.list
# 添加下面的源
deb https://mirrors.ustc.edu.cn/archive.raspberrypi.org/ stretch main uisudo apt-get update -y 
sudo apt-get dist-upgrade -y

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: 部分索引文件下载失败。如果忽略它们,那将转而使用旧的索引文件
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

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.interfaceRunning setup.py bdist_wheel for lxml ... errorComplete 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 **

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/etree.c -o build/temp.linux-armv7l-3.5/src/lxml/etree.o -wIn 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 1cc -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

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-3Could 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-3Could 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 的选项再试试?

http://chatgpt.dhexx.cn/article/ph5sXymk.shtml

相关文章

用python在树莓派上编程,你可以将项目扩展到令人难以置信的规模

树莓派是一个非常廉价的、只有手掌大小的完全可编程的计算机。虽然树莓派的体积小,但是它的潜力无限。你可以像使用常规台式计算机一样在树莓派上创建一个非常酷的工程。例如,你可以用树莓派搭建你自己的家用云存储服务器。 树莓派用什么语言编程&#x…

树莓派基础设置以及编程环境(python语言)

基础条件: 硬件:树莓派 3b 固件:2018-04-18-raspbian-stretch.img IO:树莓派3B的IO是3.3V,5V有风险。 一、python 使用方法 1.1、python2.7进入编程界面 左上角树莓派图标----->Programming----->Python2&…

树莓派教程(基于python编程)--入门篇

一:格式化SD卡 SD卡插入读卡器连接电脑,使用SDFormatter对SD卡进行格式化 (重装烧录也要进行着SD卡格式化操作) 二:下载官方镜像 http://downloads.raspberrypi.org/raspbian_latest 三:烧录SD卡 使用…

unity学习开发小球吃方块小游戏(从制作到发布程序,超详细全教程包你学会)

unity小球吃方块开发过程 你将学会用unity开发小游戏的基础知识吃方块游戏超详细全教程,保姆式教学包你学会一、创建对象创建PlayBall小球吃金币游戏项目,创建地面和小球。 二、添加脚本创建脚本使小球动起来,并用键盘控制小球的运动。 三、控…

python小游戏开发——井字棋

一家懂得用细节留住客户的3年潮牌老店我必须支持!➕🛰:luyao1931 案例介绍 本案例采用 python 实现了一个简单的井字棋游戏。该游戏在控制台界面中进行,有游戏提示。游戏棋盘为 3 X 3 的九宫格样式,每个棋格与数字键盘…

使用Vue框架开发的贪吃蛇小游戏

注意&#xff1a;尽量不要用谷歌浏览器运行&#xff0c;因为谷歌浏览器会把GameValue翻译成中文&#xff0c;使游戏掉帧卡顿 解决办法&#xff1a;1.使用Edge浏览器&#xff0c;2.把谷歌浏览器的自动中文翻译关闭即可 游戏截图&#xff1a; 源码如下&#xff1a; <!DOCTY…

QT大作业——自制小游戏

The RPGgame with QT 一、 系统架构 该项目有4个ui组成&#xff0c;其中mainwindow作为项目程序入口&#xff0c;窗口跳转流程为&#xff1a;mainwindow->gamegraph->area_prairrie->playerstastus。 主要游戏功能装载在gamegraph和area_prairrie两个ui中&#xff0…

webpack + typescript 开发微信小游戏实践

源码地址 微信小游戏版本技术选型使用typescript开发 但是微信小游戏原生不支持 typescript 开发&#xff0c;于是探索一下使用ts开发微信小游戏 1. 创建小游戏 使用测试号&#xff0c;创建一个使用官方示例的小游戏 会生成一个可以直接运行的打飞机小游戏 2. 准备工作 …

小游戏开发引擎CocosCreator

小游戏 六彩跳棋 已经通过审核并且发布了好几天了&#xff0c;对跳棋感兴趣的朋友可以去看看&#xff0c;在微信游戏里搜索 六彩跳棋 &#xff0c;点击 立即玩 吧&#xff01;进去游戏后&#xff0c;需要点击 获取头像昵称 才能得到玩家数据&#xff0c;然后 划动屏幕 选择游戏…

unity开发微信小游戏1

提示&#xff1a;文章写完后&#xff0c;目录可以自动生成&#xff0c;如何生成可参考右边的帮助文档 文章目录 前言一、准备工作二、官方文档三、WX开发者工具四、获得Appid五、获得AppidError: app.json: app.json六、资源下载失败404总结 前言 最开使用unity3d开发微信小游…

【微信小游戏】微信小游戏开发设置竖屏

微信小游戏开发环境默认横屏&#xff0c;对竖屏游戏非常不方便。 设置竖屏的入口十分隐蔽&#xff0c;以至于一度令我以为不能设置竖屏。 网上也根本搜不到解决方法&#xff0c;经过我的不懈努力&#xff0c;最终还是找到了设置方法。 原本是横屏的 按下面路径设置竖屏 开发…

使用pygame开发一个小游戏

学习了pygame&#xff0c;身为一个IKUN所以&#xff0c;做了一个简单的小游戏。游戏规则是&#xff0c;使用键盘的方向键控制坤坤&#xff0c;当坤坤触碰到篮球&#xff0c;就会爆发出音乐”只因你太美“。代码如下&#xff1a; import random import sys import pygame pygam…

微信小游戏开发教程

微信小游戏开发教程-前言 自18年年初对开发者开放小游戏接口以后小游戏越来越火热&#xff0c;本文就是对小游戏开发的入门教程&#xff0c;希望这篇文章能够帮到想要入门开发游戏的你。 哈哈哈&#xff0c;看到有人说我故意分成好几篇刷PV&#xff0c;改了一下删了两篇&…

支持小游戏开发的“引擎四剑客”

2017年12月28日&#xff0c;微信发布了一款叫“跳一跳”的微信小程序的一个小游戏&#xff0c;无需下载安装&#xff0c;即点即玩&#xff0c;只需要在微信客户端的小程序界面搜索“跳一跳”&#xff0c;点击即可加载进入该游戏玩。游戏小而精&#xff0c;借助于微信巨大的社交…

小程序开发小游戏注意事项

今天研究小游戏开发&#xff0c;总结了一些自己遇到的问题 : 一. 注册appId 用小程序开发的小游戏跟用小程序开发其他项目不是公用的一个appId 如果你现在的小程序账号已经选了别的类目&#xff08;非游戏&#xff09;&#xff0c;那你就需要另外注册一个账号来单独申请小游戏…

【小程序】快来开发你的第一个微信小游戏(详细流程)

&#x1f973; 作者&#xff1a;伯子南 &#x1f60e; 坚信&#xff1a; 好记性不如乱笔头&#xff0c;独乐乐不如众乐乐 &#x1f4aa; 个人主页&#xff1a;https://blog.csdn.net/qq_34577234?spm1010.2135.3001.5421 &#x1f46c;&#x1f3fb; 觉得博主文章不错的话&…

小游戏开发

小游戏开发 1、游戏发展历史 广义&#xff1a;一种有组织的玩耍&#xff0c;一般是以娱乐为目的&#xff0c;有时也有教育目的。在英语中&#xff0c;体育比赛(Game)也是游戏&#xff0c;只要其活动本质带有目的、规则、挑战和互动&#xff0c;我们都可以把其归为游戏。 狭义…

小游戏开发指南及过程中的难点问题

如果仅仅针对个人开发者来讲&#xff0c;要独立开发一款大型游戏几乎无可能&#xff0c;更大成功的可能还是开发一款类似《羊了个羊》这样洗脑的小程序游戏。 所以这里主要论述小游戏开发的情况&#xff0c;也就是小程序游戏&#xff0c;首先从小游戏的开发流程来看&#xff1…

oracle数据库中的注释

oracle数据库中的注释 单行注释-- --这是oracle中的单行注释 SELECT SYSDATE FROM dual; 多行注释/**/ /*这是oracle中的多行注释*/ SELECT 6 6 AS "计算结果" FROM dual;

MYSQL数据库如何写注释

方式一 注意&#xff1a;在写注释时&#xff0c;–与注释之间必须要一个空格 select * from stu -- 注释方式二 注意&#xff1a;这种方式比较随意&#xff0c;加不加空格不影响 select * from t_info #注释select * from t_info#注释select * from t_info # 注释方式三 s…