Raspberry Pi OS 是 Raspberry Pi 计算机的官方操作系统(OS),且免费。我们推荐在大多数 Raspberry Pi 应用场景中使用 Raspberry Pi OS。
Raspberry Pi OS 基于DebianLinux 发行版,支持超过 69,000 个 Debian 软件包。Raspberry Pi 遵循与 Debian 错开的版本策略:Debian 版本发布周期。新版本大约每两年发布一次。最新版的 Raspberry Pi OS 基于Debian Trixie。上一版本基于Debian Bookworm.
OS 安装
Raspberry Pi OS 使用 Raspberry Pi Imager 进行安装,该软件支持 Windows、Mac 和 Linux。如需操作指南,请参阅使用 Imager 安装。您也可以直接从我们的网站下载 Raspberry Pi OS 的磁盘镜像文件(开箱即用文件),随后将其写入 SD 卡即可开始使用您的 Raspberry Pi。
如果您正在运行旧版本的Raspberry Pi OS并希望使用最新主版本,请勿执行原位更新(不重新安装而直接升级当前系统)。相反,请重新开始并安装全新的 Raspberry Pi OS 镜像。如需指南,请参阅使用 Imager 安装.
| 备份旧 OS 中您不想丢失的任何重要文件。安装新 OS 会覆盖 SD 卡上的所有内容。 |
版本
Raspberry Pi OS 提供三种版本:
-
Raspberry Pi OS(包含基本软件的桌面版)是 Raspberry Pi Imager 中推荐的版本。它提供图形化桌面环境,并包含 Chromium 和 Firefox 网页浏览器、VLC 媒体播放器以及 Thonny Python 开发环境等应用程序。
-
Raspberry Pi OS Full(包含基本软件和推荐应用的桌面版)提供与默认Raspberry Pi OS版相同的图形化桌面环境和应用程序,此外还包含 LibreOffice 办公套件、KiCad、Scratch 编程环境及众多其他应用。
-
Raspberry Pi OS Lite(无图形化桌面的纯命令行版本)。此版本适用于无头服务器、嵌入式系统或较旧的 Raspberry Pi 型号,如初代 Raspberry Pi、Raspberry Pi 2 和 Raspberry Pi Zero。
架构
Raspberry Pi OS 提供 64-bit 和 32-bit 版本。请选择最匹配您 Raspberry Pi 设备硬件的版本:
-
该64位版本专为配备 64-bit 处理器的较新 Raspberry Pi 型号设计,例如 Raspberry Pi 3、4 和 5。它可运行 64-bit 和 32-bit 软件,支持更多 RAM,并在需要更高计算能力的任务中速度更快。
-
该32位版本专为配备 32-bit 处理器的较旧 Raspberry Pi 型号设计,如原始款 Raspberry Pi、Raspberry Pi 2 和 Raspberry Pi Zero。它占用的内存更少,可能与旧版软件及硬件具有更好的兼容性。
管理软件和 firmware 更新
更新当前操作系统有两种方式:
-
基于 APT。我们建议大多数用户使用此方法。用于更新当前 Raspberry Pi OS 安装的软件、kernel 和稳定版 firmware。如需操作指南,请参阅更新您当前的 Raspberry Pi 软件.
-
rpi-update。将其用于实验性或预发布版 kernel 和 firmware 的测试、开发,或特定缺陷修复。如需操作指南,请参阅更新预发布固件.
常规 firmware 更新包含在 APT 更新中(raspi-firmware package); rpi-update is only appropriate for developers and testers, or when you’re specifically instructed by Raspberry Pi engineers to use it.
如果不想更新当前操作系统,而是希望将 Raspberry Pi OS 从旧版本升级至新版本(例如从 Bookworm 升级到 Trixie),请通过新镜像重新安装操作系统。如需操作指南,请参阅升级到新主版本.
更新您当前的 Raspberry Pi 软件
保持 Raspberry Pi 上的软件为最新版本。这有助于确保您的 Raspberry Pi 免受漏洞并确保您获得最新的 bug fixes。它还会更新您的 Linux kernel 和 firmware。
以下说明介绍了如何使您的当前的安装Raspberry Pi OS在您正在运行的操作系统同一主版本内(例如 Trixie)保持最新。这些说明不会将您的 Raspberry Pi OS 升级至新主版本(例如从 Bookworm 到 Trixie)。如需升级操作系统的说明,请参阅升级到新主版本.
我们建议使用 APT 来保持您的 Raspberry Pi 软件为最新版本。您还可以使用 APT 查找特定的包或工具、检查包的 metadata、安装特定包以及卸载特定包。如需操作指南,请参阅管理软件包.
操作系统软件包来自远程托管的 repositories。Raspberry Pi OS 基于来自第三方 repositories 的上游软件包。对于 64-bit Raspberry Pi OS,这些软件包来自由Debian project 管理的 repositories。;对于 32 位 Raspberry Pi OS,这些软件包来自由Raspbian 项目管理的存储库 。我们将特定于 Raspberry Pi OS 的软件包托管在Raspberry Pi 软件包存储库.
要使用 APT 将当前 Raspberry Pi 软件更新至最新的修复程序、Linux 内核和固件,请定期运行以下命令:
-
更新您的软件包列表。APT 在
/etc/apt/sources.listand/etc/apt/sources.list.d/; run the following command to refresh your list of available software packages:$ sudo apt update -
中维护可用软件源的列表。更新已安装的软件包至最新版本。既然您已经有了可用新版本的列表,请运行以下命令以下载并安装更新。由于 Raspberry Pi OS 更改软件包依赖关系的频率比 Debian 更高,我们建议您使用
full-upgradeinstead of the standardupgrade:$ sudo apt full-upgrade在继续更新之前,请检查您是否有足够的可用磁盘空间。如果
sudo apt full-upgradesays that it needs extra space to complete the update, you can check that your system has at least that much free space available usingdf -h. You can then clean up downloaded files to make more space as described in 清理文件.
更新预发布固件
rpi-update is a command-line tool used to install experimental or pre-release firmware on a Raspberry Pi. On the Rasberry Pi 4 and 5, it also updates the EEPROM bootloader.
rpi-update downloads the latest pre-release version of the Linux kernel, its matching modules, device tree files, and the latest version of the VideoCore firmware. It then installs these files into an existing Raspberry Pi OS install.
| \n预发布固件不能保证正常运行,可能会引入不稳定性,导致系统变得不可靠或无法启动。 |
由rpi-update comes from the rpi-firmware 存储库使用的所有源数据 ,其中包含来自官方firmware 存储库.
rpi-update is intended for use by developers and engineers for testing, development, and specific bug fixes. In most cases, you don’t need to use rpi-update, and you should only do so if recommended by a Raspberry Pi engineer; for routine updates, we strongly recommend using APT instead, as described in 更新您当前的 Raspberry Pi 软件.
-
的数据子集,请备份您的系统。
-
运行以下命令以启动更新:
$ sudo rpi-update -
重启您的 Raspberry Pi 以使更改生效:
$ sudo reboot
了解更多信息,请参阅更新 Raspberry Pi 固件.
将固件降级至最后一个稳定版本
如果更新到最新预发布固件导致问题,您可以回退到最后稳定的固件,然后重启以使降级生效:
$ sudo apt update
$ sudo apt install --reinstall raspi-firmware
$ sudo reboot
这将重新安装 Raspberry Pi OS 官方支持的固件,恢复系统稳定性。
管理软件包
从 Terminal,APT是用于在您当前运行的 Raspberry Pi OS 中安装、更新和卸载软件的默认包管理器。您可通过类似以下的 Terminal 命令对其进行控制:apt-cache and apt install.
从桌面开始,您可以使用首选项 > 添加/删除软件工具通过图形界面来管理软件。
搜索软件
要查找特定的软件包或工具,请使用apt-cache search followed by a keyword. This returns a list of packages with names and descriptions that include the keyword. For example, to show all packages related to raspi:
$ apt-cache search raspi
查看软件包详情
要查看软件包的详细信息,请使用apt-cache show followed by a package name. This displays information such as package version, size, and dependencies. For example, to view more detailed information about the raspi-config package:
$ apt-cache show raspi-config
安装软件包
要在您的 Raspberry Pi 上安装特定的软件包,请使用sudo apt install followed by the package name:
$ sudo apt install <package-name>
系统将显示该软件包所需的磁盘空间量,并提示您确认安装。若要确认安装,请在键盘上输入Y followed by 输入或返回。若要在安装时跳过确认提示并自动回答“是”,请添加-y to the command:
$ sudo apt install -y <package-name>
卸载软件包
要从您的 Raspberry Pi 上卸载特定的软件包,请使用sudo apt remove followed by the package name:
$ sudo apt remove <package-name>
系统将显示卸载该软件包可释放的磁盘空间量,并提示您确认卸载。若要确认卸载,请在键盘上输入Y followed by 输入或返回在键盘上输入以确认安装。若要在卸载时跳过确认提示并自动回答“是”,请添加-y to the command:
$ sudo apt remove -y <package-name>
若要彻底删除软件包的所有痕迹(包括配置文件),请使用purge instead of remove:
$ sudo apt purge <package-name>
检查磁盘空间
在安装或更新 Raspberry Pi 上的软件之前,请使用以下命令检查可用存储空间:
$ df -h
清理文件
APT 会存储下载的包(.deb files) in /var/cache/apt/archives. During installation, APT downloads these packages and then copies files from these packages to the correct installation locations. Depending on the software you’ve installed, package files can take up a lot of disk space. Use the following command to delete old downloaded package files:
$ sudo apt clean
升级到新主版本
与其对现有启动介质进行就地升级,我们强烈建议您采用全新安装的方式来升级 Raspberry Pi 上的操作系统版本,例如从 Bookworm 迁移至 Trixie。此过程需要使用Raspberry Pi Imager将新的操作系统镜像写入到新的启动介质中。
以下说明需要:
-
一块用于启动介质的新存储设备(通常是 microSD 卡),以及将其连接至计算机的方式(例如 USB 读卡器)。
-
一台用于将新的操作系统镜像写入新启动介质,以及在启动介质之间存储和传输数据的计算机。
以下说明假设您使用 microSD 卡作为新的启动介质,并使用 Raspberry Pi Imager 将新的操作系统镜像安装到该启动介质上。如果您使用其他类型的存储设备作为启动介质,请相应修改以下步骤。
-
创建当前启动介质的备份。
| 此步骤将永久删除您所选存储设备上的所有文件。请在继续操作前备份这些文件。 |
-
准备一个装有新操作系统的新存储设备。在新的存储设备上,安装包含最新 Raspberry Pi OS 版本的新操作系统镜像。有关说明,请参阅使用 Imager 安装.
-
复制现有的文件和配置。使用存储设备从您的 Raspberry Pi 复制重要文件。
-
将新的启动介质插入您的 Raspberry Pi。将包含新操作系统的存储设备插入您的 Raspberry Pi。
-
启动您的 Raspberry Pi。将您的 Raspberry Pi 连接电源并开机。
将 Raspberry Pi OS Lite 转换为 Raspberry Pi OS Desktop
您可以使用 APT 添加相应的软件包,将 Lite 版本转换为 Desktop 版本。
-
安装您首选的桌面窗口系统。
-
要安装 Wayland(推荐),请运行:
$ sudo apt install rpd-wayland-core -
要安装 X,请运行:
$ sudo apt install rpd-x-core
-
-
安装 Raspberry Pi 主题和控制中心。
$ sudo apt install rpd-theme $ sudo apt install rpd-preferences -
添加应用程序。
$ sudo apt install rpd-applications $ sudo apt install rpd-utilities $ sudo apt install rpd-developer $ sudo apt install rpd-graphics -
为您选择的窗口系统添加额外功能,例如截图和远程桌面功能。
-
对于 Wayland,请运行:
$ sudo apt install rpd-wayland-extras -
对于 X,请运行:
$ sudo apt install rpd-x-extras
-
-
重启您的 Raspberry Pi 以进入桌面环境。
$ sudo reboot
现在,您已拥有构成 Raspberry Pi OS Desktop 的所有软件包。
如果您想恢复到 Raspberry Pi OS 的 Lite 版本,可以卸载这些软件包。或者,若要在不卸载已安装桌面软件包的情况下获得 Lite 版体验,请按照启动到控制台或桌面中的说明,将您的 Raspberry Pi 设置为启动至命令行。
您还可以通过前往 Raspberry Pi 主菜单并选择以下选项,来安装 Raspberry Pi OS Full 中包含的推荐应用程序:首选项 > 推荐软件.
播放音频和视频
Raspberry Pi OS 预装了VLC media player。您可以使用 VLC 播放视频和音频文件。VLC 在 Raspberry Pi OS 中使用硬件加速,并支持多种流行的音视频文件格式。
VLC media player
VLC 图形界面
要从 Raspberry Pi Desktop 播放音频或视频文件,请在文件管理器中双击该文件。这将自动启动 VLC 来播放该文件。或者,从影音菜单中,启动VLC 媒体播放器。然后,从媒体菜单,选择打开文件…并导航到要播放的文件。
默认情况下,Raspberry Pi OS 通过 HDMI 将音频发送到显示器。若要将音频输出到其他接口(如耳机插孔或 USB 扬声器),请右键单击系统托盘中的扬声器图标并选择相应选项。
vlc CLI
您也可以从命令行启动 VLC。在以下示例中,我们使用了 Big Buck Bunny 的一段短片。要从 Raspberry Pi 下载此短片,请运行以下命令:
$ wget --trust-server-names http://rpltd.co/big-buck-bunny
要从命令行在 VLC 中播放该短片,请运行以下命令:
$ vlc big-buck-bunny-1080p.mp4
为防止文件播放完毕后 VLC GUI 保持打开状态,请添加--play-and-exit flag:
$ vlc --play-and-exit big-buck-bunny-1080p.mp4
要以全屏模式播放视频(在某些情况下可使播放更流畅),请添加--fullscreen flag:
$ vlc --play-and-exit --fullscreen big-buck-bunny-1080p.mp4
使用cvlc to play media without a GUI
如果您使用cvlc instead of vlc with any of these commands, then the VLC GUI won’t be shown:
$ cvlc --play-and-exit big-buck-bunny-1080p.mp4
在 Raspberry Pi OS Lite 上播放音频和视频
与完整版 Raspberry Pi OS 不同,VLC 未预装在 Raspberry Pi OS Lite 中。要在 Raspberry Pi OS Lite 上使用 VLC 播放视频和音频,请安装无桌面播放所需的软件包:
$ sudo apt install --no-install-recommends vlc-bin vlc-plugin-base
在以下示例中,我们使用了一段短音频。要从 Raspberry Pi 下载此音频片段,请运行以下命令:
$ wget --trust-server-names http://rpltd.co/startup-music
要从命令行在 VLC 中播放该短片,请运行以下命令:
$ cvlc --play-and-exit computer-startup-music.mp3
指定音频输出设备
要强制将音频输出到特定设备,请传递alsa value to the the -A option to use ALSA音频输出,以及--alsa-audio-device option to specify an audio output device:
$ cvlc --play-and-exit -A alsa --alsa-audio-device <alsa-device> computer-startup-music.mp3
替换其中的<alsa-device> placeholder with one of the following options:
| ALSA 设备 | 说明 |
|---|---|
|
Raspberry Pi 4 及更早型号的耳机插孔 |
|
具有单个 HDMI 端口的设备上的 HDMI 输出(Zero 型号、CM4S、CM4 之前的 Compute Module 以及 Raspberry Pi 4 之前的旗舰型号) |
|
Raspberry Pi 4B 及之后的旗舰型号、CM4 及之后的 Compute Module 以及 Keyboard 型号上的 HDMI0 输出 |
|
Raspberry Pi 4B 及之后的旗舰型号、CM4 及之后的 Compute Module 以及 Keyboard 型号上的 HDMI1 输出 |
|
使用以下命令获取 Raspberry Pi 上所有 ALSA 设备的列表:
|
指定视频输出设备
您可以使用 VLC GUI 或使用--drm-vout-display from the console.
要从控制台指定视频输出:
-
将您的 Raspberry Pi 设备启动至控制台.
-
添加
--drm-vout-displayoption:$ cvlc --play-and-exit --drm-vout-display <drm-device> big-buck-bunny-1080p.mp4 -
替换其中的
<drm-device>placeholder with one of the following options:DRM 设备
描述
HDMI-A-1Raspberry Pi Zero 或 Raspberry Pi Model 1、2 或 3 上的 HDMI 输出;或者Raspberry Pi 4B 及更高版本以及所有 Keyboard 型号上的 HDMI0 输出
HDMI-A-2Raspberry Pi 4B 及更高版本、Compute Module CM4 及更高版本(不包括 CM4S)以及所有 Keyboard 型号上的 HDMI1 输出
DSI-1Raspberry Pi Touch Display、Raspberry Pi Touch Display 2 或第三方 DSI 屏幕
DSI-2具有多个 DSI 端口的型号的第二个 DSI 输出(Raspberry Pi 5 以来的旗舰型号以及所有 Compute Module 型号)
|
使用以下命令获取 Raspberry Pi 上所有 DRM 设备的列表。
|
同时指定音频和视频输出设备
您可以组合音频和视频输出选项。例如,要将视频输出定向到触摸屏,并将音频输出定向到耳机插孔,请使用上述命令的以下组合:
$ cvlc --play-and-exit --fullscreen --drm-vout-display DSI-1 -A alsa --alsa-audio-device sysdefault:CARD=Headphones your_video.mp4
提升流媒体播放性能
如果您有原始 H.264 流(例如从 Raspberry Pi Camera Module 捕获的流),可以通过将该流封装在 MP4 等容器格式中来提升 VLC 中的播放性能。您可以使用ffmpeg to convert stream content into a container file. For example, the following command converts a stream named video.h264 to a MP4 container named video.mp4 at 30fps:
$ ffmpeg -r 30 -i video.h264 -c:v copy video.mp4
实用工具
Raspberry Pi OS 中预装了多个实用的命令行工具。
kmsprint
该kmsprint tool can be used to list the display-modes supported by the monitors attached to your Raspberry Pi device.
使用kmsprint to see the state of all displays connected to your Raspberry Pi, and kmsprint -m to list the display modes supported by each display.
在 Raspberry Pi 5 及更高版本上,显示接口(如 HDMI、DSI、DPI 和 VEC)分布在多个独立的 DRM 卡(/dev/dri/cardX)上,而不是像 Raspberry Pi 4 及更早版本那样位于单一统一卡上。
要在 Raspberry Pi 5 上查询特定接口,您可能需要使用--device (or -d) option:
$ kmsprint --device=/dev/dri/card1
卡号在启动时动态分配,并且可能会在重启之间根据所连接的硬件发生变化。
vclog
vclog displays log messages from the VideoCore GPU from Linux running on the Arm. It needs to be run as root.
sudo vclog --msg prints out the message log, while sudo vclog --assert prints out the assertion log.
vcgencmd
该vcgencmd tool is used to output information from the VideoCore GPU on Raspberry Pi devices. You can find source code for the vcgencmd utility 在 GitHub 上.
要获取 支持的所有命令列表vcgencmd, use vcgencmd commands. Some useful commands and their required parameters are listed here:
vcos
该vcos command has two useful sub-commands:
-
versiondisplays the build date and version of the firmware on the VideoCore -
log statusdisplays the error log status of the various VideoCore firmware areas
version
显示 VideoCore 固件的构建日期和版本。
get_throttled
返回系统的节流状态。这是一个位模式。某一位被置位表示以下含义:
| 位 | 十六进制值 | 含义 |
|---|---|---|
0 |
|
检测到欠压 |
1 |
|
Arm 频率受限 |
2 |
|
当前已降频 |
3 |
|
软温度限制已生效 |
十六 |
|
已发生欠压 |
17 |
|
已发生 Arm 频率受限 |
18 |
|
已发生降频 |
https://www.raspberrypi.org/?p=65103 |
|
已触发软温度限制 |
measure_temp
返回由内部温度传感器测量的 SoC 温度。在 Raspberry Pi 4 系列和 Raspberry Pi 5 系列设备上,measure_temp pmic returns the temperature of the PMIC.
measure_clock [clock]
返回指定时钟的当前频率。接受以下时钟值:
| 时钟 | 说明 |
|---|---|
|
Arm 核心 |
|
GPU 核心 |
|
H.264 模块 |
|
图像传感器流水线 |
|
3D 模块 |
|
通用异步收发器 |
|
PWM 模块(模拟音频输出) |
|
SD 卡接口 |
|
像素阀 |
|
模拟视频编码器 |
|
HDMI |
|
显示并行接口 |
例如,vcgencmd measure_clock arm
measure_volts [block]
显示特定模块当前使用的电压。接受以下模块值:
| 模块 | 说明 |
|---|---|
|
VC4 核心电压 |
|
SDRAM 核心电压 |
|
SDRAM I/O 电压 |
|
SDRAM PHY 电压 |
pmic_read_adc [channel]
在 Raspberry Pi 5 上,显示 PMIC 内置 ADC 的读数:每个 PMIC 供电轨的拉取电流和电压,以及电源电压EXT5V_V. Without a parameter, the command lists every channel:
$ vcgencmd pmic_read_adc
3V7_WL_SW_A current(0)=0.09466522A
3V3_SYS_A current(1)=0.05465208A
1V8_SYS_A current(2)=0.16005250A
DDR_VDD2_A current(3)=0.02441920A
DDR_VDDQ_A current(4)=0.00000000A
1V1_SYS_A current(5)=0.17566740A
0V8_SW_A current(6)=0.26838080A
VDD_CORE_A current(7)=0.51794000A
3V3_DAC_A current(17)=0.00000000A
3V3_ADC_A current(18)=0.00048840A
0V8_AON_A current(16)=0.00293040A
HDMI_A current(22)=0.02576310A
3V7_WL_SW_V volt(8)=3.70627200V
3V3_SYS_V volt(9)=3.31188900V
1V8_SYS_V volt(10)=1.80268400V
DDR_VDD2_V volt(11)=1.11355200V
DDR_VDDQ_V volt(12)=0.60549390V
1V1_SYS_V volt(13)=1.11025500V
0V8_SW_V volt(14)=0.80402850V
VDD_CORE_V volt(15)=0.75186740V
3V3_DAC_V volt(20)=3.31409900V
3V3_ADC_V volt(21)=3.30768900V
0V8_AON_V volt(19)=0.80146450V
HDMI_V volt(23)=5.06654000V
EXT5V_V volt(24)=5.06788000V
BATT_V volt(25)=0.00085470V
要读取单个通道,请传入其名称。例如,要读取电源电压:
$ vcgencmd pmic_read_adc EXT5V_V
EXT5V_V volt(24)=5.06520000V
如果无法识别通道名称,该命令将列出所有通道,而不是返回错误。
这些读数不包括 USB 电流或直接连接到 5V 电源的任何其他设备的电流,因为该电源绕过了 PMIC。有关更多信息,请参阅电源与 Raspberry Pi OS.
otp_dump
显示 SoC 内部 OTP(一次性可编程)存储器的内容。这些是 32 位值,索引范围为 8 到 64。请参阅OTP 位页面以获取更多详情。
get_config [configuration item|int|str]
显示指定配置设置的值;或者,指定int (integer) or str (string) to see all configuration items of the given type. For example, the following command returns the total memory on the device in megabytes:
$ vcgencmd get_config total_mem
get_mem type
报告 Arm 和 GPU 可寻址的内存量。要显示 Arm 可寻址的内存量,请使用vcgencmd get_mem arm; to show the amount of GPU-addressable memory, use vcgencmd get_mem gpu. On devices with more than 1 GB of memory, the arm parameter always returns 1 GB minus the gpu memory value, since the GPU firmware is only aware of the first 1 GB of memory. To get an accurate report of the total memory on the device, see the total_mem configuration item and the get_config前文部分。
codec_enabled [type]
报告指定的编解码器类型是否已启用。type 的可能选项包括 AGIF、FLAC、H263、H264、MJPA、MJPB、MJPG、MPG2、MPG4、MVC0、PCM、THRA、VORB、VP6、VP8、WMV9、WVC1。由于 Raspberry Pi 4 及后续设备系列上的 H.265 硬件模块不属于 VideoCore GPU,因此无法通过此命令访问其状态。
mem_oom
显示 VideoCore 内存空间中发生的任何 OOM(内存不足)事件的统计信息。 此命令在 Raspberry Pi 5 上不可用。
mem_reloc_stats
显示 VideoCore 上可重定位内存分配器的统计信息。 此命令在 Raspberry Pi 5 上不可用。
read_ring_osc
返回环形振荡器的当前速度、电压和温度。
辅助功能选项
视觉辅助工具
视力受损的 Raspberry Pi OS 用户可以在推荐软件菜单。
我们提供Orca 屏幕阅读器中找到有助于轻松导航 Raspberry Pi 桌面的实用工具,以及用于提高 UI 和屏幕元素可读性的屏幕放大镜。您还可以从控制中心.
Orca 屏幕阅读器
您可以从推荐软件部分安装 Orca 屏幕阅读器。或者,按Ctrl + Alt + 太空以自动安装 Orca。
在安装新镜像后首次启动 Raspberry Pi OS 时,30 秒后会自动播放语音提示。该提示提供了如何安装 Orca 的说明。
屏幕放大镜
要放大屏幕,请按Ctrl + Alt + M。这会增大显示桌面的尺寸,因此屏幕上现在只能看到桌面的一部分。移动鼠标光标可查看放大后桌面的不同部分。要关闭屏幕放大,请按Ctrl + Alt + M.
放大 UI 元素
要放大 UI 元素以便更轻松查看,请选择偏好设置 > Control Centre,然后转到默认值选项卡。在此选项卡上,您可以选择设为默认值以适应不同尺寸的屏幕。您选择的屏幕尺寸越大设为默认值,UI 文本、图标和其他元素显示得就越大。
在 Raspberry Pi 上使用 Python
草莓派操作系统已预装了 Python 3。若干扰系统中的 Python 安装,可能会给您的操作系统带来问题。在安装第三方 Python 库时,请务必使用正确的软件包管理工具。
在 Linux 上,您可以安装python dependencies in two ways:
-
使用
aptto install pre-configured system packages -
使用
pipto install libraries using Python’s dependency manager 在虚拟环境中从 Raspberry Pi OS书虫开始,您只能使用 pipto install into a Python Virtual Environment (venv). This change was introduced by the Python community, not by Raspberry Pi: for more information, see PEP 668.
安装 Python 包,使用apt
通过apt are packaged specifically for Raspberry Pi OS. These packages usually come pre-compiled, so they install faster. Because apt manages dependencies for all packages, installing with this method includes all of the sub-dependencies needed to run the package. And apt ensures that you don’t break other packages if you uninstall.
例如,要安装支持 Raspberry Pi 的 Python 3 库与树莓派合作推出,请运行以下命令:
$ sudo apt install python3-build-hat
若需查找随附的 Python 包,apt, 使用apt search。在大多数情况下,Python 包使用前缀python- or python3-: for instance, you can find the numpy package under the name python3-numpy.
安装 Python 库,使用pip
书虫更改为pip installation
从 Raspberry Pi OS书虫开始,您不能将库直接安装到系统版本的 Python 中。
相反,请将库安装到虚拟环境中(venv)。要在系统级别为所有用户安装库,请使用apt.
在系统范围内尝试安装 Python 包时,会显示类似如下的错误信息:
$ pip install buildhat
error: externally-managed-environment
× This environment is externally managed
╰─> To install Python packages system-wide, try apt install
python3-xyz, where xyz is the package you are trying to
install.
If you wish to install a non-Debian-packaged Python package,
create a virtual environment using python3 -m venv path/to/venv.
Then use path/to/venv/bin/python and path/to/venv/bin/pip. Make
sure you have python3-full installed.
For more information visit http://rptl.io/venv
note: If you believe this is a mistake, please contact your Python installation or OS distribution provider. You can override this, at the risk of breaking your Python installation or OS, by passing --break-system-packages.
hint: See PEP 668 for the detailed specification.
Python 用户长期以来一直面临与操作系统包管理器之间冲突的问题,例如:apt and Python-specific package management tools like pip. These conflicts include both Python-level API incompatibilities and conflicts over file ownership.
从 Raspberry Pi OS书虫起,通过pip 安装的包必须安装到 Python 虚拟环境中 (venv). A virtual environment is a container where you can safely install third-party modules so they won’t interfere with your system Python.
使用pip with virtual environments
要使用虚拟环境,需创建一个容器来存储环境。根据您希望如何使用 Python 的不同需求,您可以采用多种方法:
- 按项目创建环境
-
许多用户会为每个 Python 项目创建单独的虚拟环境。将虚拟环境放在每个项目的根文件夹中,通常使用类似
env. Run the following command from the root folder of each project to create a virtual environment configuration folder:$ python -m venv env在开始处理项目之前,从项目根目录运行以下命令,以启动虚拟环境的使用:
$ source env/bin/activate您随后将看到类似以下的提示符:
(env) $当您完成项目工作后,从任意目录运行以下命令,以退出虚拟环境:
(env) $ deactivate - 按用户创建环境
-
您无需为每个 Python 项目创建虚拟环境,而是可以为您的用户账户创建一个统一的虚拟环境。在运行任何 Python 代码之前,请先激活该虚拟环境。对于在多个项目间共享大量库的工作流,这种方法更为便捷。
为整个用户账户的多个项目创建虚拟环境时,请考虑将虚拟环境配置文件放在主目录中。将配置存储在文件夹中,其名称以句点开头以便默认隐藏该文件夹,防止其使主目录显得杂乱。
使用以下命令,在当前用户的主目录中创建一个隐藏文件夹中的虚拟环境:
$ python -m venv ~/.env从任意目录运行以下命令,以开始使用虚拟环境:
$ source ~/.env/bin/activate您随后将看到类似以下的提示符:
(.env) $要退出虚拟环境,请从任意目录运行以下命令:
(.env) $ deactivate
创建虚拟环境
运行以下命令创建虚拟环境配置文件夹,将<env-name> with the name you would like to use for the virtual environment (for example, env):
$ python -m venv <env-name>
传入--system-site-packages flag before the folder name to preload all of the currently installed packages in your system Python installation into the virtual environment.
|
进入虚拟环境
然后,执行bin/activate script in the virtual environment configuration folder to enter the virtual environment:
$ source <env-name>/bin/activate
您随后将看到类似以下的提示符:
(<env-name>) $
该(<env-name>) command prompt prefix indicates that the current terminal session is in a virtual environment named <env-name>.
要检查您是否处于虚拟环境中,请使用pip list to view the list of installed packages:
(<env-name>) $ pip list
Package Version
---------- -------
pip 23.0.1
setuptools 66.1.1
该列表应比系统 Python 中安装的包列表短得多。现在您可以安全地使用pip. Any packages you install with pip while in a virtual environment only install to that virtual environment. In a virtual environment, the python or python3 commands automatically use the virtual environment’s version of Python and installed packages instead of the system Python.
退出虚拟环境
要退出虚拟环境,请运行以下命令:
(<env-name>) $ deactivate
使用 Thonny 编辑器
我们推荐Thonny在 Raspberry Pi 上编辑 Python 代码。
默认情况下,Thonny 使用系统 Python。但是,您可以通过点击解释器菜单切换到使用 Python 虚拟环境,该菜单位于 Thonny 窗口的右下角。选择已配置的环境或使用Configure interpreter….
从 Python 使用 GPIO
使用GPIO Zero库可让您轻松使用 Python 控制 GPIO 设备。该库的完整文档请参见gpiozero.readthedocs.io.
有关 GPIO 硬件的信息,请参阅GPIO 硬件.
LED 控制
以下示例代码用于控制连接到 GPIO17 的 LED:
from gpiozero import LED
from time import sleep
led = LED(17)
while True:
led.on()
sleep(1)
led.off()
sleep(1)
在 Thonny 等 IDE 中运行此代码,LED 将会反复闪烁。
LED 方法包括on(), off(), toggle(), and blink().
读取按钮状态
以下示例代码用于读取连接到 GPIO2 的按钮状态:
from gpiozero import Button
from time import sleep
button = Button(2)
while True:
if button.is_pressed:
print("Pressed")
else:
print("Released")
sleep(1)
按钮功能包括以下属性is_pressed and is_held; callbacks when_pressed, when_released, and when_held; and methods wait_for_press() and wait_for_release.
使用按钮控制 LED
以下示例代码读取连接到 GPIO2 的按钮状态,并在按下按钮时点亮连接到 GPIO17 的 LED:
from gpiozero import LED, Button
led = LED(17)
button = Button(2)
while True:
if button.is_pressed:
led.on()
else:
led.off()
或者:
from gpiozero import LED, Button
led = LED(17)
button = Button(2)
while True:
button.wait_for_press()
led.on()
button.wait_for_release()
led.off()
或:
from gpiozero import LED, Button
led = LED(17)
button = Button(2)
button.when_pressed = led.on
button.when_released = led.off