2022-11-15 11:59:16 +00:00
|
|
|
# micropython-lib
|
2017-08-13 10:52:49 +00:00
|
|
|
|
2022-11-15 11:59:16 +00:00
|
|
|
This is a repository of packages designed to be useful for writing MicroPython
|
|
|
|
applications.
|
2014-08-05 01:15:00 +00:00
|
|
|
|
2022-11-15 11:59:16 +00:00
|
|
|
The packages here fall into categories corresponding to the four top-level
|
|
|
|
directories:
|
2014-08-05 01:15:00 +00:00
|
|
|
|
2022-11-15 11:59:16 +00:00
|
|
|
* **python-stdlib**: Compatible versions of modules from [The Python Standard
|
|
|
|
Library](https://docs.python.org/3/library/). These should be drop-in
|
|
|
|
replacements for the corresponding Python modules, although many have
|
|
|
|
reduced functionality or missing methods or classes (which may not be an
|
|
|
|
issue for most cases).
|
2014-06-14 16:38:03 +00:00
|
|
|
|
2022-11-15 11:59:16 +00:00
|
|
|
* **python-ecosys**: Compatible, but reduced-functionality versions of
|
|
|
|
packages from the wider Python ecosystem. For example, a package that
|
|
|
|
might be found in the [Python Package Index](https://pypi.org/).
|
2014-06-14 16:38:03 +00:00
|
|
|
|
2022-11-15 11:59:16 +00:00
|
|
|
* **micropython**: MicroPython-specific packages that do not have equivalents
|
|
|
|
in other Python environments. This includes drivers for hardware
|
|
|
|
(e.g. sensors, peripherals, or displays), libraries to work with
|
|
|
|
embedded functionality (e.g. bluetooth), or MicroPython-specific
|
|
|
|
packages that do not have equivalents in CPython.
|
2014-08-05 01:15:00 +00:00
|
|
|
|
2022-11-15 11:59:16 +00:00
|
|
|
* **unix-ffi**: These packages are specifically for the MicroPython Unix port
|
|
|
|
and provide access to operating-system and third-party libraries via FFI,
|
|
|
|
or functionality that is not useful for non-Unix ports.
|
2014-06-10 21:18:09 +00:00
|
|
|
|
2022-11-15 11:59:16 +00:00
|
|
|
## Usage
|
2014-06-14 16:38:03 +00:00
|
|
|
|
2022-11-15 11:59:16 +00:00
|
|
|
To install a micropython-lib package, there are four main options. For more
|
|
|
|
information see the [Package management documentation](https://docs.micropython.org/en/latest/reference/packages.html)
|
|
|
|
documentation.
|
2014-06-10 21:18:09 +00:00
|
|
|
|
2022-11-15 11:59:16 +00:00
|
|
|
### On a network-enabled device
|
2014-06-10 21:18:09 +00:00
|
|
|
|
2022-11-15 11:59:16 +00:00
|
|
|
As of MicroPython v1.20 (and nightly builds since October 2022), boards
|
|
|
|
with WiFi and Ethernet support include the `mip` package manager.
|
|
|
|
|
|
|
|
```py
|
|
|
|
>>> import mip
|
|
|
|
>>> mip.install("package-name")
|
|
|
|
```
|
|
|
|
|
|
|
|
### Using `mpremote` from your PC
|
|
|
|
|
|
|
|
`mpremote` is the officially-supported tool for interacting with a MicroPython
|
|
|
|
device and, since v0.4.0, support for installing micropython-lib packages is
|
|
|
|
provided by using the `mip` command.
|
|
|
|
|
|
|
|
```bash
|
|
|
|
$ mpremote connect /dev/ttyUSB0 mip install package-name
|
|
|
|
```
|
|
|
|
|
|
|
|
See the [mpremote documentation](https://docs.micropython.org/en/latest/reference/mpremote.html).
|
|
|
|
|
|
|
|
### Freeze into your firmware
|
|
|
|
|
|
|
|
If you are building your own firmware, all packages in this repository include
|
|
|
|
a `manifest.py` that can be included into your board manifest via the
|
|
|
|
`require()` command. See [Manifest files](https://docs.micropython.org/en/latest/reference/manifest.html#require) for
|
|
|
|
more information.
|
|
|
|
|
|
|
|
### Copy the files manually
|
|
|
|
|
|
|
|
Many micropython-lib packages are just single-file modules, and you can
|
|
|
|
quickly get started by copying the relevant Python file to your device. For
|
|
|
|
example, to add the `base64` library, you can directly copy
|
|
|
|
`python-stdlib/base64/base64.py` to the `lib` directory on your device.
|
|
|
|
|
|
|
|
This can be done using `mpremote`, for example:
|
|
|
|
|
|
|
|
```bash
|
|
|
|
$ mpremote connect /dev/ttyUSB0 cp python-stdlib/base64/base64.py :/lib
|
|
|
|
```
|
|
|
|
|
|
|
|
For packages that are implemented as a package directory, you'll need to copy
|
|
|
|
the directory instead. For example, to add `collections.defaultdict`, copy
|
|
|
|
`collections/collections/__init__.py` and
|
|
|
|
`collections-defaultdict/collections/defaultdict.py` to a directory named
|
|
|
|
`lib/collections` on your device.
|
|
|
|
|
|
|
|
Note that unlike the other three approaches based on `mip` or `manifest.py`,
|
|
|
|
you will need to manually resolve dependencies. You can inspect the relevant
|
|
|
|
`manifest.py` file to view the list of dependencies for a given package.
|
|
|
|
|
tools/ci.sh: Support publishing package and index files to GitHub Pages.
Opt-in feature to make it easier for folks to test packages that are still
in development, open in Pull Requests, or even in independent forks.
---
To enable this on your own GitHub fork of the micropython-lib repository
then navigate to the fork's "Settings" -> "Secrets and variables" ->
"Actions" -> "Variables" page, then click "New repository variable", and
create a variable named MIP_INDEX with value true (or any "truthy" value).
Once enabled then any time a branch is pushed to your fork and builds
successfully, GitHub Actions will also push the built packages and package
index to the gh-pages branch which is associated with the repo's GitHub
Pages web site. The packages can then be installed remotely via:
mpremote mip --index \
https://USERNAME.github.io/micropython-lib/mip/BRANCH_NAME PACKAGE_NAME
or on a device as:
mip.install(PACKAGE_NAME, index="https://USERNAME.github.io/micropython-lib/mip/BRANCHNAME")
(Replace USERNAME, BRANCH_NAME and PACKAGE_NAME as applicable. If you've
renamed your fork, change the name micropython-lib to match.)
Note: As well as the MIP_INDEX repository variable, this functionality
depends on both GitHub Actions and GitHub Pages being enabled on your
repository in GitHub. However both options should enable automatically,
unless they have been manually disabled.
This work was funded through GitHub Sponsors.
2023-02-20 02:54:55 +00:00
|
|
|
## Installing packages from forks
|
|
|
|
|
|
|
|
It is possible to use the `mpremote mip install` or `mip.install()` methods to
|
|
|
|
install packages built from a
|
|
|
|
[fork](https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/working-with-forks/about-forks)
|
|
|
|
of micropython-lib, if the fork's owner has opted in.
|
|
|
|
|
|
|
|
This can be useful to install packages from a pending Pull Request, for example.
|
|
|
|
|
|
|
|
First, the owner of the fork must opt-in as described under
|
|
|
|
[Publishing packages from forks](CONTRIBUTING.md#publishing-packages-from-forks).
|
|
|
|
|
|
|
|
After this has happened, each time someone pushes to a branch in that fork then
|
|
|
|
GitHub Actions will automatically publish the packages to a GitHub Pages site.
|
|
|
|
|
|
|
|
To install these packages, use commands such as:
|
|
|
|
|
|
|
|
```bash
|
|
|
|
$ mpremote connect /dev/ttyUSB0 mip install --index https://USERNAME.github.io/micropython-lib/mip/BRANCH_NAME PACKAGE_NAME
|
|
|
|
```
|
|
|
|
|
|
|
|
Or from a networked device:
|
|
|
|
|
|
|
|
```py
|
|
|
|
import mip
|
|
|
|
mip.install(PACKAGE_NAME, index="https://USERNAME.github.io/micropython-lib/mip/BRANCH_NAME")
|
|
|
|
```
|
|
|
|
|
|
|
|
(Where `USERNAME`, `BRANCH_NAME` and `PACKAGE_NAME` are replaced with the owner
|
|
|
|
of the fork, the branch the packages were built from, and the package name.)
|
|
|
|
|
2022-11-15 11:59:16 +00:00
|
|
|
## Contributing
|
|
|
|
|
|
|
|
We use [GitHub Discussions](https://github.com/micropython/micropython/discussions)
|
|
|
|
as our forum, and [Discord](https://micropython.org/discord) for chat. These
|
|
|
|
are great places to ask questions and advice from the community or to discuss your
|
|
|
|
MicroPython-based projects.
|
|
|
|
|
|
|
|
The [MicroPython Wiki](https://github.com/micropython/micropython/wiki) is
|
|
|
|
also used for micropython-lib.
|
|
|
|
|
|
|
|
For bugs and feature requests, please [raise an issue](https://github.com/micropython/micropython-lib/issues/new).
|
|
|
|
|
|
|
|
We welcome pull requests to add new packages, fix bugs, or add features.
|
|
|
|
Please be sure to follow the
|
|
|
|
[Contributor's Guidelines & Code Conventions](CONTRIBUTING.md). Note that
|
|
|
|
MicroPython is licensed under the [MIT license](LICENSE) and all contributions
|
|
|
|
should follow this license.
|
|
|
|
|
|
|
|
### Future plans (and new contributor ideas)
|
|
|
|
|
|
|
|
* Develop a set of example programs using these packages.
|
|
|
|
* Develop more MicroPython packages for common tasks.
|
|
|
|
* Expand unit testing coverage.
|
|
|
|
* Add support for referencing remote/third-party repositories.
|
|
|
|
|
|
|
|
## Notes on terminology
|
|
|
|
|
|
|
|
The terms *library*, *package*, and *module* are overloaded and lead to some
|
|
|
|
confusion. The interpretation used in by the MicroPython project is that:
|
|
|
|
|
|
|
|
A *library* is a collection of installable packages, e.g. [The Python Standard
|
|
|
|
Library](https://docs.python.org/3/library/), or micropython-lib.
|
|
|
|
|
|
|
|
A *package* can refer to two things. The first meaning, "library package", is
|
|
|
|
something that can be installed from a library, e.g. via `mip` (or `pip` in
|
|
|
|
CPython/PyPI). Packages provide *modules* that can be imported. The ambiguity
|
|
|
|
here is that the module provided by the package does not necessarily have to
|
|
|
|
have the same name, e.g. the `pyjwt` package provides the `jwt` module. In
|
|
|
|
CPython, the `pyserial` package providing the `serial` module is another
|
|
|
|
common example.
|
|
|
|
|
|
|
|
A *module* is something that can be imported. For example, "the *os* module".
|
|
|
|
|
|
|
|
A module can be implemented either as a single file, typically also called
|
|
|
|
a *module* or "single-file module", or as a *package* (the second meaning),
|
|
|
|
which in this context means a directory containing multiple `.py` files
|
|
|
|
(usually at least an `__init__.py`).
|
|
|
|
|
|
|
|
In micropython-lib, we also have the concept of an *extension package* which
|
|
|
|
is a library package that extends the functionality of another package, by
|
|
|
|
adding additional files to the same package directory. These packages have
|
|
|
|
hyphenated names. For example, the `collections-defaultdict` package extends
|
|
|
|
the `collections` package to add the `defaultdict` class to the `collections`
|
|
|
|
module.
|
2020-03-20 01:33:11 +00:00
|
|
|
|