我们使用一些必要的Cookie来确保网站正常运行。

我们使用可选的 Cookie,具体详情请参阅我们的Cookie政策,用于记住您的设置并了解您如何使用我们的网站。

piwheels:让“pip install”变得更快

TL;DRpip install numpy used to take ages, and now it’s super fast thanks to piwheels.

该Python软件包索引(PyPI)是Python模块的软件包仓库。Python社区的成员将其作为软件和库的发布方式,以便于分发。如果你曾经使用过pip install, PyPI is the service that hosts the software you installed. You may have noticed that some installations can take a long time on the Raspberry Pi. That usually happens when modules have been implemented in C and require compilation.

XKCD comic of two people sword-fighting on office chairs while their code is compiling

No more slacking off! pip install numpy takes just a few seconds now \o/

Python 包的轮子文件 [number]

对此问题有一个通用的解决方案:Python wheels are a standard for distributing pre-built versions of packages, saving users from having to build from source. However, when C code is compiled, it’s compiled for a particular architecture, so package maintainers usually publish wheels for 32-bit and 64-bit Windows, macOS, and Linux. Although Raspberry Pi runs Linux, its architecture is ARM, so Linux wheels are not compatible.

A comic of snakes biting their own tails to roll down a sand dune like wheels

哪些Python轮子不是

Pip 通过浏览 PyPI 来寻找与用户架构匹配的 wheel 文件——如果找不到,它将回退到源码分发版(通常是源代码的 tarball 或 zip)。然后用户必须自行构建,这可能需要很长时间,或者可能需要某些依赖项。如果 pip 找不到源码分发版,安装就会失败。 [1]

Developing piwheels

In order to solve this problem, I decided to build wheels of every package on PyPI. I wrote some tooling for automating the process and used a postgres database to monitor the status of builds and log the output. Using a Pi 3 in my living room, I attempted to build wheels of the latest version of all 100 000 packages on PyPI and to host them on a web server on the Pi. This took a total of ten days, and my proof-of-concept seemed to show that it generally worked and was likely to be useful! You could install packages directly from the server, and installations were really fast.

A Raspberry Pi 3 sitting atop a Pi 2 on cloth

This Pi 3 was the piwheels beta server, sitting atop my SSH gateway Pi 2 at home

我继续规划了版本2的计划,该计划将尝试构建每个软件包的所有版本——总共大约750,000个版本。我估计这需要一台Pi运行75天,但我的打算扩大规模使用多台Pi。我们的网络托管服务商Mythic Beasts提供了专用的Pi 3托管服务,所以我启动了20台来分担负载。在一些帮助下戴夫·琼斯, who created an efficient queuing system for the builders, we were able make this run like clockwork. In under two weeks, it was complete! Read ALL about the first build run drama在我的博客上。

A list of the mythic beasts cloud Pis

所有连接到云的树莓派

改进 piwheels

我们分析了失败原因,做了一些调整,安装了一些关键依赖项,并再次运行构建,将成功率从76%提高到83%。我们还为Python 3.5(Raspbian Stretch中的新默认版本)重新构建了软件包。我们构建的wheel文件标记为‘armv7l’,但由于我们的Raspbian镜像与所有Pi型号兼容,它们实际上是ARMv6,因此它们与Pi 3、Pi 2、Pi 1和Pi Zero兼容。这意味着我们提供的标记为‘armv6l’的wheel文件实际上只是重命名的ARMv7 wheel文件。

The piwheels monitor interface created by Dave Jones

戴夫创建的出色的piwheels监控界面

现在,你可能会想“为什么你不直接交叉编译呢?” 我真的想要完全的兼容性,而在树莓派上原生构建似乎是最好的方法。我可以很容易地使用这些树莓派,而且实际上并没有花太多时间。另外,你知道的,我就是想...eat my own dog food.

你可能也在想“为什么不直接 apt install python3-numpy 呢?”确实,有些 Python 包也通过 Raspbian/Debian 存档分发。但是,如果你在一个虚拟环境中,或者你需要比 Debian 打包的版本更新的版本,你就需要 pip。

工作原理

现在piwheels 软件包存储库作为一个服务运行,在伦敦 Mythic Beasts 数据中心的一台 Pi 3 上托管。Raspbian Stretch 中的 pip 包被配置为使用 piwheels 作为额外的索引,因此如果缺少某个包,则会回退到 PyPI。只需运行sudo apt upgrade to get the configuration change. You’ll find that pip installs are much faster now! If you want to use piwheels on Raspbian Jessie, that’s possible too — find the instructions in our 常见问题。现在,每次你用 pip 安装东西时,你的文件都来自运行在 Raspberry Pi 上的 web 服务器(那台功能强大的小机器)!

在虚拟环境中自己试试:

sudo apt install virtualenv python3-virtualenv -y
virtualenv -p /usr/bin/python3 testpip
source testpip/bin/activate
pip install numpy

这在 Pi 3 上大约需要 20 分钟,在 Pi 1 上需要 2.5 小时,或者只要几秒钟如果你使用 piwheels。

如果你想了解详细信息,请尝试pip install numpy -v for verbose output. You’ll see that pip discovers two indexes to search:

2 location(s) to search for versions of numpy:
  * https://pypi.python.org/simple/numpy/
  * https://www.piwheels.hostedpi.com/simple/numpy/

然后它会在两个索引中搜索可用文件。从这个文件列表中,它确定可用的最新版本。接下来,它寻找与 Python 版本和架构匹配的文件,并优先选择 wheel 而不是源码分发。如果发布了一个新包或新版本,piwheels 将自动检测并将其添加到构建队列中。

A flowchart of how piwheels works

piwheels 的工作原理

对于不熟悉虚拟环境的用户,我应该提到这样做并不是必需的——只是在沙箱中测试安装的一种简单方法。大多数 pip 使用将需要sudo pip3 install {package}, which installs at a system level.

如果你在 piwheels 的任何包中遇到任何问题,请在GitHub issue.

中进一步推进 piwheels

我们目前提供超过670,000 个 wheels适用于超过96,000 个包,所有这些都在 Raspberry Pi 硬件上本地编译。此外,我们将继续构建新发布的包。

请注意,目前我们已经为 Python 3.4 和 3.5 构建了 wheels —— 我们计划添加对 Python 3.6 和 2.7 的支持。piwheels 当前缺少 Python 2 的 wheels 不会影响用户:直到我们为 Python 2 重建之前,PyPI 将照常使用,只是安装具有我们提供的 wheels 的 Python 3 包会比这更耗时。但请记住,Python 2 的生命周期结束就在不到三年之后!

非常感谢戴夫·琼斯感谢他对该项目的贡献,以及Mythic Beasts 机架中的服务器上提供出色的托管Pi服务.

Screenshot of the mythic beasts Raspberry Pi 3 server service website

相关/不相关,查看我的海报来自英国Python大会海报会议:

A poster about Python and Raspberry Pi

点击下载PDF!

22条评论

Alex Bate avatar

我知道你总归有用处。

GeekyTim avatar

干得好,本。

Nathan Henrie avatar

关于在pip安装中添加新的默认索引,我们是否应该注意任何安全影响?如果您的Pi安全性受到威胁,这是否可能导致全球范围内安装恶意的轮子?

Ben Nuttall avatar

piwheels服务器的安全性与运行raspberrypi.org的服务器一样(包括网站,镜像下载,apt包等),即仅通过SSH密钥访问,SSL,定期更新和管理安全补丁等。虽然Python打包系统不如Debian的安全(没有密钥签名),但piwheels和PyPI之间没有实际区别。

还要注意的是,构建用的Pi与存储库服务器是隔离的,并且没有root权限。每次构建后都会进行清理,并且在发布之前会验证构建器和主机之间传递的文件。已经投入了很多精力来确保事情合理地完成。

Milliways avatar

我不明白以TL;DR开头的文章。这可能(可以说)在一个回复中有其位置,但作者是在警告我们他自己的文章吗?

Ben Nuttall avatar

这是我提供长篇文章摘要的方式。看到“这是TL;DR”并不罕见 :)

Tim Rowledge avatar

我们这些老家伙倾向于将其视为“摘要”。你知道的,那部分解释了整篇论文的推特式段落内容。

Dougie Lawson avatar

谢谢本,这太棒了。我已经恢复了我的/etc/pip.conf并运行

pip3 list -o | cut -d = -f 1 | xargs -n1 pip3 install -U

这非常迅速地升级了我系统中的所有python包,使用了你的轮子。

Ben Nuttall avatar

太棒了!

Milliways avatar

在我的Pi3上,pip3 list -o产生了许多错误消息

Dante avatar

稍微离题一下 - 您从哪里得到“螺丝”来固定或分层安装树莓派板。它们是否可以在某个地方买到(也许我只是太笨找不到它自己)?

Ben Nuttall avatar

它们是2.5毫米的公母柱脚。Pi的安装孔是2.5毫米,因此任何M2.5螺丝都可以根据您的堆叠需求使用。我在eBay上购买了这个组合箱在eBay上。

在eBay等网站上搜索“M2.5柱脚螺丝六角螺母”。

Cancelor avatar

精彩,做得好,非常感谢。

Andy Warburton avatar

Ben做得很好(写得也太棒了!)

Michael Peskett avatar

很棒的团队,太棒了。

Andrew Gale avatar

我不能说自己全部都懂,但正是这种奉献精神和努力工作让Raspberry Pi社区成为了现在的样子。干得好,Ben和Dave,谢谢你们!

Ben Nuttall avatar

谢谢!来自社区的优秀工具真是令人惊叹 - 更棒的是,当人们发现有机会时,他们确实可以有意义地做出贡献。

Chris Arndt avatar

是否有我们可以添加到项目 README 中的徽章 URL,以显示它是否可在 pipwheels 上使用?

Ben Nuttall avatar

目前还没有,但一旦我们实现了API,这可能是可行的。

laixintao avatar

Dave Jones? 加勒比海盗里的那个人? 现在成程序员了?

Ben Nuttall avatar

当Dave(原姓Hughes)结婚时,他选择了一个新的名字。阅读这篇文章了解更多:https://www.raspberrypi.org/blog/picamera-pure-python-interface-for-camera-module/

Dima avatar

http://www.piwheels.hostedpi.com/网站宕机了。我想知道是否有任何镜像网站?

评论已关闭