包管理¶
安装带有 mip 的软件包¶
有网络功能的板包含 mip 模块,它可以安装来自 :term: micropython-lib 和第三方网站(包括GitHub,GitLab)的软件包。
mip (“mip安装包”)在概念上类似于Python的 pip 工具,但它不使用PyPI索引,而是默认使用:term: micropython-lib 作为其索引。 mip 将自动获取编译过的 .mpy file 从micropython-lib下载时。
使用 mip 的最常见方法来自REPL:
>>> import mip
>>> mip.install("pkgname") # Installs the latest version of "pkgname" (and dependencies)
>>> mip.install("pkgname", version="x.y") # Installs version x.y of "pkgname"
>>> mip.install("pkgname", mpy=False) # Installs the source version (i.e. .py rather than .mpy files)
mip 将通过搜索 sys.path 来检测文件系统上的适当位置,查找以 /lib 结尾的第一个条目。您可以使用 target 覆盖目标,但请注意,此路径必须在 sys.path 中才能随后导入。:
>>> mip.install("pkgname", target="third-party")
>>> sys.path.append("third-party")
除了从micropython-lib索引下载包外, mip 也可以安装第三方库。最简单的方法是直接下载文件:
>>> mip.install("http://example.com/x/y/foo.py")
>>> mip.install("http://example.com/x/y/foo.mpy")
当直接安装文件时,仍然支持 target 参数来设置目标路径,但 mpy 和 version 将被忽略。
URL也可以以 github: 或 gitlab: 开头,作为指向托管在GitHub或GitLab上的内容的简单方式:
>>> mip.install("github:org/repo/path/foo.py") # Uses default branch
>>> mip.install("github:org/repo/path/foo.py", version="branch-or-tag") # Optionally specify the branch or tag
>>> mip.install("gitlab:org/repo/path/foo.py") # Uses default branch
>>> mip.install("gitlab:org/repo/path/foo.py", version="branch-or-tag") # Optionally specify the branch or tag
更复杂的包(即包含多个文件或依赖项的包)可以通过指定其 package.json 的路径来下载。
>>> mip.install("http://example.com/x/package.json")
>>> mip.install("github:org/user/path/package.json")
>>> mip.install("gitlab:org/user/path/package.json")
如果没有指定json文件,则 “package.json” 被隐式添加:
>>> mip.install("http://example.com/x/")
>>> mip.install("github:org/repo") # Uses default branch of that repo
>>> mip.install("github:org/repo", version="branch-or-tag")
>>> mip.install("gitlab:org/repo") # Uses default branch of that repo
>>> mip.install("gitlab:org/repo", version="branch-or-tag")
在Unix移植版本上使用 mip¶
在Unix移植版本上,mip 可以如上所述在REPL中使用,也可以使用 -m:
$ ./micropython -m mip install pkgname-or-url
$ ./micropython -m mip install pkgname-or-url@version
参数 --target=path、--no-mpy 和 --index 被设置:
$ ./micropython -m mip install --target=third-party pkgname
$ ./micropython -m mip install --no-mpy pkgname
$ ./micropython -m mip install --index https://host/pi pkgname
使用 mpremote 安装软件包¶
mpremote 工具还包括与 mip 相同的功能,可以从主机PC安装软件包到本地连接的设备(例如通过USB或UART):
$ mpremote mip install pkgname
$ mpremote mip install pkgname@x.y
$ mpremote mip install http://example.com/x/y/foo.py
$ mpremote mip install github:org/repo
$ mpremote mip install github:org/repo@branch-or-tag
$ mpremote mip install gitlab:org/repo
$ mpremote mip install gitlab:org/repo@branch-or-tag
参数 --target=path、--no-mpy 和 --index 被设置:
$ mpremote mip install --target=/flash/third-party pkgname
$ mpremote mip install --no-mpy pkgname
$ mpremote mip install --index https://host/pi pkgname
手动安装软件包¶
软件包也可以通过手动将文件复制到设备中来安装(以.py或.mpy形式)。根据主板的不同,这可能是通过USB大容量存储, mpremote 工具(例如 :mpremote fs cp path/to/package.py:package.py ), webrepl 等。
编写和发布软件包¶
发布到 micropython-lib 是让MicroPython用户广泛访问您的包的最简单方法,并且可以通过 mip 和 mpremote 自动获得并编译为字节码。更多信息请参见https://github.com/micropython/micropython-lib。
要编写可以通过 mip 或 mpremote 下载的“自托管”包,您需要一个静态web服务器(或GitHub)来托管单个.py文件或与.py文件一起的 package.json 文件。
一个典型的 package.json 文件,例如用于 mlx90640 库的示例,可能如下所示:
{
"urls": [
["mlx90640/__init__.py", "github:org/micropython-mlx90640/mlx90640/__init__.py"],
["mlx90640/utils.py", "github:org/micropython-mlx90640/mlx90640/utils.py"]
],
"deps": [
["collections-defaultdict", "latest"],
["os-path", "latest"],
["github:org/micropython-additions", "main"],
["gitlab:org/micropython-otheradditions", "main"]
],
"version": "0.2"
}
这包括两个文件,托管在名为 org/micropython-mlx90640 的GitHub repo中,它们安装到设备上的 mlx90640 目录中。它依赖于 collections-defaultdict 和 os-path,它们将从:term:micropython-lib 自动安装。第三个依赖项根据GitHub repo org/micropython-additions 的 main 分支中的 package.json 文件定义的内容进行安装。
冷冻包¶
当从设备文件系统导入一个Python模块或包时,它会在RAM中编译成 字节码,准备由虚拟机执行。对于 .mpy 文件,这个转换已经完成,但字节码仍然会在RAM中。
对于低内存设备或大型应用程序,将字节码从ROM(即闪存)运行可能更有利。这可以通过将字节码“冻结”到MicroPython固件中来完成,然后将其闪存到设备中。运行时性能是相同的(尽管导入更快),但它可以释放大量RAM供你的程序使用。
这种方法的缺点是开发速度会慢得多,因为每次都必须闪存固件,但它仍然可以用于冻结不经常更改的依赖项。
冻结通过编写清单文件并在构建中使用它来完成,通常作为自定义板定义的一部分。有关更多信息,请参阅 MicroPython清单文件 指南。