|
1 |
| -micropython-lib |
2 |
| -=============== |
| 1 | +# micropython-lib |
3 | 2 |
|
4 |
| -This is a repository of libraries designed to be useful for writing |
5 |
| -MicroPython applications. |
| 3 | +This is a repository of packages designed to be useful for writing MicroPython |
| 4 | +applications. |
6 | 5 |
|
7 |
| -The libraries here fall into four categories corresponding to the four top-level directories: |
| 6 | +The packages here fall into categories corresponding to the four top-level |
| 7 | +directories: |
8 | 8 |
|
9 |
| - * **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 Python libraries, although many have reduced functionality or missing methods or classes (which may not be an issue for many most cases). |
| 9 | +* **python-stdlib**: Compatible versions of modules from [The Python Standard |
| 10 | + Library](https://docs.python.org/3/library/). These should be drop-in |
| 11 | + replacements for the corresponding Python modules, although many have |
| 12 | + reduced functionality or missing methods or classes (which may not be an |
| 13 | + issue for most cases). |
10 | 14 |
|
11 |
| - * **python-ecosys**: Compatible, but reduced-functionality versions of modules from the larger Python ecosystem, for example that might be found in the [Python Package Index](https://pypi.org/). |
| 15 | + * **python-ecosys**: Compatible, but reduced-functionality versions of |
| 16 | + packages from the wider Python ecosystem. For example, a package that |
| 17 | + might be found in the [Python Package Index](https://pypi.org/). |
12 | 18 |
|
13 |
| -* **micropython**: MicroPython-specific modules that do not have equivalents in other Python environments. These are typically hardware drivers or highly-optimised alternative implementations of functionality available in other Python modules. |
| 19 | + * **micropython**: MicroPython-specific packages that do not have equivalents |
| 20 | + in other Python environments. This includes drivers for hardware |
| 21 | + (e.g. sensors, peripherals, or displays), libraries to work with |
| 22 | + embedded functionality (e.g. bluetooth), or MicroPython-specific |
| 23 | + packages that do not have equivalents in CPython. |
14 | 24 |
|
15 |
| - * **unix-ffi**: These modules are specifically for the MicroPython Unix port and provide access to operating-system and third-party libraries via FFI. |
| 25 | +* **unix-ffi**: These packages are specifically for the MicroPython Unix port |
| 26 | + and provide access to operating-system and third-party libraries via FFI, |
| 27 | + or functionality that is not useful for non-Unix ports. |
16 | 28 |
|
17 |
| -Usage |
18 |
| ------ |
| 29 | +## Usage |
19 | 30 |
|
20 |
| -Many libraries are self contained modules, and you can quickly get started by |
21 |
| -copying the relevant Python file to your device. For example, to add the |
22 |
| -`base64` library, you can directly copy `python-stdlib/base64/base64.py` to the `lib` |
23 |
| -directory on your device. |
| 31 | +To install a micropython-lib package, there are four main options. For more |
| 32 | +information see the [Package management documentation](https://docs.micropython.org/en/latest/reference/packages.html) |
| 33 | +documentation. |
24 | 34 |
|
25 |
| -Other libraries are packages, in which case 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. |
| 35 | +### On a network-enabled device |
26 | 36 |
|
27 |
| -Future plans (and new contributor ideas) |
28 |
| ----------------------------------------- |
| 37 | +As of MicroPython v1.20 (and nightly builds since October 2022), boards |
| 38 | +with WiFi and Ethernet support include the `mip` package manager. |
| 39 | + |
| 40 | +```py |
| 41 | +>>> import mip |
| 42 | +>>> mip.install("package-name") |
| 43 | +``` |
| 44 | + |
| 45 | +### Using `mpremote` from your PC |
| 46 | + |
| 47 | +`mpremote` is the officially-supported tool for interacting with a MicroPython |
| 48 | +device and, since v0.4.0, support for installing micropython-lib packages is |
| 49 | +provided by using the `mip` command. |
| 50 | + |
| 51 | +```bash |
| 52 | +$ mpremote connect /dev/ttyUSB0 mip install package-name |
| 53 | +``` |
| 54 | + |
| 55 | +See the [mpremote documentation](https://docs.micropython.org/en/latest/reference/mpremote.html). |
| 56 | + |
| 57 | +### Freeze into your firmware |
| 58 | + |
| 59 | +If you are building your own firmware, all packages in this repository include |
| 60 | +a `manifest.py` that can be included into your board manifest via the |
| 61 | +`require()` command. See [Manifest files](https://docs.micropython.org/en/latest/reference/manifest.html#require) for |
| 62 | +more information. |
| 63 | + |
| 64 | +### Copy the files manually |
| 65 | + |
| 66 | +Many micropython-lib packages are just single-file modules, and you can |
| 67 | +quickly get started by copying the relevant Python file to your device. For |
| 68 | +example, to add the `base64` library, you can directly copy |
| 69 | +`python-stdlib/base64/base64.py` to the `lib` directory on your device. |
| 70 | + |
| 71 | +This can be done using `mpremote`, for example: |
| 72 | + |
| 73 | +```bash |
| 74 | +$ mpremote connect /dev/ttyUSB0 cp python-stdlib/base64/base64.py :/lib |
| 75 | +``` |
| 76 | + |
| 77 | +For packages that are implemented as a package directory, you'll need to copy |
| 78 | +the directory instead. For example, to add `collections.defaultdict`, copy |
| 79 | +`collections/collections/__init__.py` and |
| 80 | +`collections-defaultdict/collections/defaultdict.py` to a directory named |
| 81 | +`lib/collections` on your device. |
| 82 | + |
| 83 | +Note that unlike the other three approaches based on `mip` or `manifest.py`, |
| 84 | +you will need to manually resolve dependencies. You can inspect the relevant |
| 85 | +`manifest.py` file to view the list of dependencies for a given package. |
| 86 | + |
| 87 | +## Contributing |
| 88 | + |
| 89 | +We use [GitHub Discussions](https://github.com/micropython/micropython/discussions) |
| 90 | +as our forum, and [Discord](https://micropython.org/discord) for chat. These |
| 91 | +are great places to ask questions and advice from the community or to discuss your |
| 92 | +MicroPython-based projects. |
| 93 | + |
| 94 | +The [MicroPython Wiki](https://github.com/micropython/micropython/wiki) is |
| 95 | +also used for micropython-lib. |
| 96 | + |
| 97 | +For bugs and feature requests, please [raise an issue](https://github.com/micropython/micropython-lib/issues/new). |
| 98 | + |
| 99 | +We welcome pull requests to add new packages, fix bugs, or add features. |
| 100 | +Please be sure to follow the |
| 101 | +[Contributor's Guidelines & Code Conventions](CONTRIBUTING.md). Note that |
| 102 | +MicroPython is licensed under the [MIT license](LICENSE) and all contributions |
| 103 | +should follow this license. |
| 104 | + |
| 105 | +### Future plans (and new contributor ideas) |
| 106 | + |
| 107 | +* Develop a set of example programs using these packages. |
| 108 | +* Develop more MicroPython packages for common tasks. |
| 109 | +* Expand unit testing coverage. |
| 110 | +* Add support for referencing remote/third-party repositories. |
| 111 | + |
| 112 | +## Notes on terminology |
| 113 | + |
| 114 | +The terms *library*, *package*, and *module* are overloaded and lead to some |
| 115 | +confusion. The interpretation used in by the MicroPython project is that: |
| 116 | + |
| 117 | +A *library* is a collection of installable packages, e.g. [The Python Standard |
| 118 | + Library](https://docs.python.org/3/library/), or micropython-lib. |
| 119 | + |
| 120 | +A *package* can refer to two things. The first meaning, "library package", is |
| 121 | +something that can be installed from a library, e.g. via `mip` (or `pip` in |
| 122 | +CPython/PyPI). Packages provide *modules* that can be imported. The ambiguity |
| 123 | +here is that the module provided by the package does not necessarily have to |
| 124 | +have the same name, e.g. the `pyjwt` package provides the `jwt` module. In |
| 125 | +CPython, the `pyserial` package providing the `serial` module is another |
| 126 | +common example. |
| 127 | + |
| 128 | +A *module* is something that can be imported. For example, "the *os* module". |
| 129 | + |
| 130 | +A module can be implemented either as a single file, typically also called |
| 131 | +a *module* or "single-file module", or as a *package* (the second meaning), |
| 132 | +which in this context means a directory containing multiple `.py` files |
| 133 | +(usually at least an `__init__.py`). |
| 134 | + |
| 135 | +In micropython-lib, we also have the concept of an *extension package* which |
| 136 | +is a library package that extends the functionality of another package, by |
| 137 | +adding additional files to the same package directory. These packages have |
| 138 | +hyphenated names. For example, the `collections-defaultdict` package extends |
| 139 | +the `collections` package to add the `defaultdict` class to the `collections` |
| 140 | +module. |
29 | 141 |
|
30 |
| -* Develop a set of example programs using these libraries. |
31 |
| -* Develop more MicroPython libraries for common tasks. |
|
0 commit comments