micropython-lib version included in official downloads is unclear
The official downloads page lists pre-built images by micropython version, but does not give any information on which micropython-lib version is used. I've had a few issues where I got stack traces into micropython-lib but was unable to find the matching source version.
I think either the micropython-lib repo should contain tags pointing to the corresponding micropython versions, or the micropython-lib version/commit/tag should be linked on the downloads page and somewhere in the micropython repo.
Thanks!
micropython/mip: Add a new `mip` library for on-device installation.
This PR adds:
- A library (
mip) that can be frozen (or manually installed) on network-capable boards for on-device installation of packages from micropython-lib (or other similar package indices). - A tool (
tools/build.py) to deploy micropython-lib to a static web server suitable for access frommip(ormpremotewhen similar functionality is added).
The structure of the static web server output is explained in the comments at the top of deploy.py.
mip.install() supports the following use cases:
mip.install("foo")-- this will download the latest version of the foo package (and dependencies) from micropython-lib.mip.install("foo", version="x.y")-- install foo version x.y, including dependencies at the version when foo x.y was publishedmip.install("foo", target="path/to/lib")-- install foo and dependencies in specified path (otherwise defaults to the first "lib" directory in sys.path)mip.install("foo", index="https://example.com/")-- install foo from example.com's indexmip.install("http://example.com/x/y/foo.py")-- download foo.py directly from the URLmip.install("http://example.com/x/y/foo.json")-- download a package, including dependencies, described by foo.json (see below)mip.install("http://example.com/x/y")-- implicitly http://example.com/x/y/package.jsonmip.install("github:org/user/path/foo.py")-- shortcut for github-hosted content (fetches user/path/foo.py from the default branch)mip.install("github:org/user/path/mypackage.json")-- as abovemip.install("github:org/user")-- this will hopefully be the common case for "self-hosted" packages.mip.install("github:org/user", version="devel")-- as above, but using the "devel" branch- `mip.install("foo", mpy=False) -- fetch foo but as .py files (i.e. for debugging).
Major differences in mip compared to upip:
- Packages installed from the index are bytecode compiled to .mpy by default. The request includes the device's supported version.
- pypi is not supported as a source.
- It uses
urequestsrather than having its own HTTP client. (Most boards that freezemipalso freeze urequests anyway)
The package.json looks like
{
"hashes": [
["aioble/server.mpy", "e39dbf64"],
...
],
"urls": [
["target/path.py", "http://url/foo/bar/path.py"],
...
],
"deps": [
["name", "version"],
...
]
"version": "0.1"
}
The "hashes" are for referencing .mpy files directly from micropython-lib. This is used by micropython-lib packages, but shouldn't be used for self-hosted ones. The "urls" allows arbritrary URLs to be fetched, and "deps" allows recursive dependencies, e.g. a self-hosted package might want to provide some urls to their own files, and then depend on some micropython-lib packages (or a package json in another github repo).
Note: deploy.py requires https://github.com/micropython/micropython/pull/9437
This work was funded through GitHub Sponsors.
I've written a small demo of what writing a "self-hosted" (on GitHub) package will look like, including how to specify dependencies: https://github.com/jimmo/micropython-mlx90640
This can be installed by
Of course I'd prefer for a driver like this one to end up in micropython-lib and therefore get all the benefits of being bytecode compiled etc, but this third-party publishing (to github or other web server) is still very much a use case we want to support.
Another artifact of the build is a top-level
index.json(which will be https://micropython.org/pi/v2/index.json) which contains the full list of available packages from micropython-lib.mipdoesn't consume this, but other tools that want to discover available packages can. ~What's included in this for now is pretty minimal, but unlike the package metadata I don't imagine this being consumed by low-memory devices so this could have more info added (possibly version history etc).~ (Edit: added full version history for each bytecode version, as well as description, author, license)I checked the frozen version of this new upip, compared to the old one (compiled with mpy-cross v6 using -O0, ie no optimisations). This is to see how the new one compares to the old one in terms of flash size. Note that the new upip relies on urequests, so the total size of the new upip needs to include the size of urequests.
Old upip size:
New upip+urequests combined size:
So the new upip combined with urequests is about 500 bytes smaller (around 10% smaller) than the old upip. Even bigger savings are made when urequests already exists for other reasons. In that case upip on its own is just 2464 bytes frozen (on rp2).
Furthermore the dependencies on built-in C modules are reduced with the new upip. The dependencies are:
So gc, errno, zlib and uctypes are no longer required by the new upip, compared to the old one.
I've pushed an updated version that adds two features requested via private feedback:
mip.install(..., mpy=False).What about the ability to do, on the unix port,
micropython -m upip install foo? That could be done with the old upip. Do we need to support that?Edit: maybe we can have a separate
upip-clithat supports command-line for unix?Addressed comments above.
Yep, I want to do this like the change we made for
unittestwhere if you installunittest-discoverthen you also getmicropython -m unittest. (Separate PR)As discussed, renamed
upiptomip, anddeploy.pytobuild.py.Thank you, this is awesome!
Are there any ideas on how to support platforms like Pico that aren't network-enabled?
@peterhinch
Yep! https://github.com/micropython/micropython/pull/9467
And https://github.com/micropython/micropython/pull/9463 included a significant overhaul of https://docs.micropython.org/en/latest/reference/packages.html (which hasn't gone live yet, but will hopefully update soon).