|  | 
| 1 | 1 | micropython-lib | 
| 2 | 2 | =============== | 
| 3 |  | -micropython-lib is a project to develop a non-monolothic standard library | 
| 4 |  | -for "advanced" MicroPython fork (https://github.com/pfalcon/micropython). | 
| 5 |  | -Each module or package is available as a separate distribution package from | 
| 6 |  | -PyPI. Each module comes from one of the following sources (and thus each | 
| 7 |  | -module has its own licensing terms): | 
| 8 | 3 | 
 | 
| 9 |  | -* written from scratch specifically for MicroPython | 
| 10 |  | -* ported from CPython | 
| 11 |  | -* ported from some other Python implementation, e.g. PyPy | 
| 12 |  | -* some modules actually aren't implemented yet and are dummy | 
|  | 4 | +This is a repository of libraries designed to be useful for writing | 
|  | 5 | +MicroPython applications. | 
| 13 | 6 | 
 | 
| 14 |  | -Note that the main target of micropython-lib is a "Unix" port of the | 
| 15 |  | -aforementioned fork of MicroPython. Actual system requirements vary per | 
| 16 |  | -module. Majority of modules are compatible with the upstream MicroPython, | 
| 17 |  | -though some may require additional functionality/optimizations present in | 
| 18 |  | -the "advanced" fork. Modules not related to I/O may also work without | 
| 19 |  | -problems on bare-metal ports, not just on "Unix" port (e.g. pyboard). | 
|  | 7 | +The libraries here fall into roughly four categories: | 
| 20 | 8 | 
 | 
|  | 9 | + * Compatible ports of CPython standard libraries. These should be drop-in replacements for the CPython libraries, although many have reduced functionality or missing methods or classes (which may not be an issue for many use cases). | 
| 21 | 10 | 
 | 
| 22 |  | -Usage | 
| 23 |  | ------ | 
| 24 |  | -micropython-lib packages are published on PyPI (Python Package Index), | 
| 25 |  | -the standard Python community package repository: https://pypi.org/ . | 
| 26 |  | -On PyPI, you can search for MicroPython related packages and read | 
| 27 |  | -additional package information. By convention, all micropython-lib package | 
| 28 |  | -names are prefixed with "micropython-" (the reverse is not true - some | 
| 29 |  | -package starting with "micropython-" aren't part of micropython-lib and | 
| 30 |  | -were released by 3rd parties). | 
| 31 |  | - | 
| 32 |  | -Browse available packages [via this | 
| 33 |  | -URL](https://pypi.org/search/?q=&o=&c=Programming+Language+%3A%3A+Python+%3A%3A+Implementation+%3A%3A+MicroPython). | 
| 34 |  | - | 
| 35 |  | -To install packages from PyPI for usage on your local system, use the | 
| 36 |  | -`upip` tool, which is MicroPython's native package manager, similar to | 
| 37 |  | -`pip`, which is used to install packages for CPython. `upip` is bundled | 
| 38 |  | -with MicroPython "Unix" port (i.e. if you build "Unix" port, you | 
| 39 |  | -automatically have `upip` tool). Following examples assume that | 
| 40 |  | -`micropython` binary is available on your `PATH`: | 
| 41 |  | - | 
| 42 |  | -~~~~ | 
| 43 |  | -$ micropython -m upip install micropython-pystone | 
| 44 |  | -... | 
| 45 |  | -$ micropython | 
| 46 |  | ->>> import pystone | 
| 47 |  | ->>> pystone.main() | 
| 48 |  | -Pystone(1.2) time for 50000 passes = 0.534 | 
| 49 |  | -This machine benchmarks at 93633 pystones/second | 
| 50 |  | -~~~~ | 
|  | 11 | + * "Micro" versions of CPython standard libraries with limited compatibility. These can often provide the same functionality, but might require some code changes compared to the CPython version. | 
| 51 | 12 | 
 | 
| 52 |  | -Run `micropython -m upip --help` for more information about `upip`. | 
|  | 13 | +* MicroPython-specific libraries. These include drivers and other libraries targeted at running Python on hardware or embedded systems. | 
| 53 | 14 | 
 | 
|  | 15 | + * MicroPython-on-Unix-specific libraries. These extend the functionality of the Unix port to allow access to operating-system level functionality (which allows more CPython compatibility). | 
| 54 | 16 | 
 | 
| 55 |  | -Development | 
| 56 |  | ------------ | 
| 57 |  | -To install modules during development, use `make install`. By default, all | 
| 58 |  | -available packages will be installed. To install a specific module, add the | 
| 59 |  | -`MOD=<module>` parameter to the end of the `make install` command. | 
| 60 |  | - | 
| 61 |  | - | 
| 62 |  | -Links | 
|  | 17 | +Usage | 
| 63 | 18 | ----- | 
| 64 |  | -If you would like to trace evolution of MicroPython packaging support, | 
| 65 |  | -you may find following links useful (note that they may contain outdated | 
| 66 |  | -information): | 
| 67 | 19 | 
 | 
| 68 |  | - * https://github.com/micropython/micropython/issues/405 | 
| 69 |  | - * http://forum.micropython.org/viewtopic.php?f=5&t=70 | 
|  | 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 `base64/base64.py` to the `lib` | 
|  | 23 | +directory on your device. | 
| 70 | 24 | 
 | 
| 71 |  | -Guidelines for packaging MicroPython modules for PyPI: | 
|  | 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. | 
| 72 | 26 | 
 | 
| 73 |  | - * https://github.com/micropython/micropython/issues/413 | 
|  | 27 | +For devices that have network connectivity (e.g. PYBD, ESP8266, ESP32), they | 
|  | 28 | +will have the `upip` module installed. | 
|  | 29 | + | 
|  | 30 | +``` | 
|  | 31 | +>>> import upip | 
|  | 32 | +>>> upip.install('micropython-base64') | 
|  | 33 | +>>> upip.install('micropython-collections.defaultdict') | 
|  | 34 | +... | 
|  | 35 | +>>> import base64 | 
|  | 36 | +>>> base64.b64decode('aGVsbG8sIG1pY3JvcHl0aG9u') | 
|  | 37 | +b'hello, micropython' | 
|  | 38 | +>>> from collections import defaultdict | 
|  | 39 | +>>> d = defaultdict(int) | 
|  | 40 | +>>> d[a] += 1 | 
|  | 41 | +``` | 
|  | 42 | + | 
|  | 43 | +Future plans (and new contributor ideas) | 
|  | 44 | +---------------------------------------- | 
|  | 45 | + | 
|  | 46 | +* Provide compiled .mpy distributions. | 
|  | 47 | +* Develop a set of example programs using these libraries. | 
|  | 48 | +* Develop more MicroPython libraries for common tasks. | 
0 commit comments