Page 1 of 1

Libraries used in Micropython

Posted: Sat Jan 11, 2020 4:21 am
by mybigman
Hi All,

I am trying to find where the libraries used in micropython are kept as the libraries here don't seem be to used or accurate.

Take the random/urandom lib for instance -

Code: Select all

>>> import random
>>> help(random)
object <module 'urandom'> is of type module
  __name__ -- urandom
  getrandbits -- <function>
  seed -- <function>
However the random source code shows it has other methods available?

Sometimes I need to look at the source code to see how something is implemented to help with the development.

Is there a place I can see this information that is up to date?


Re: Libraries used in Micropython

Posted: Sat Jan 11, 2020 5:42 am
by tve
I hope someone in the know gives you a more general answer, but this specific library seems to be implemented here: ... durandom.c

Re: Libraries used in Micropython

Posted: Sat Jan 11, 2020 8:30 am
by stijn

Code: Select all

__name__ -- urandom
That's what is going on: you have imported the urandom module, not the random module from micropython-lib. Meaning the latter is not found in the module search path, and as a result MicroPython checks if the module doesn't happen to have a 'micro implementation' i.e. a module with the same name but starting with a 'u' and implemented. That's the case, there's the urandom module, so that gets imported (see source code link in other post). This was done to make it easier to write code which works both on CPython and MicroPython, to avoid some confusion etc, but it also adds confusion if people don't know the whole story. This is also mentioned in the documentation at ... index.html.

Re: Libraries used in Micropython

Posted: Sat Jan 11, 2020 10:29 am
by jimmo
Some extra details:

micropython-lib needs some attention. It provides four things:
- some libraries that are used as part of the core firmware (e.g. urequests, uasyncio)
- some libraries that are similar but different and incompatible to ones used in the core firmware (e.g. upip)
- some libraries that extend the functionality in the core firmware (random being a good example, it extends the built-in urandom)
- some libraries that are only for the unix port and provide access to functionality provided by existing linux libraries.

I'd quite like to split out the four things and have it become just the third thing.

So in your case (as previously explained):
- Any builtin library is usually named "ufoo", but is always aliased to "foo".
- If you create a file on the device "", then it will be imported preferentially to ufoo when you write "import foo". But can use ufoo in it's implementation (this is the case for random -- it starts with "from ufoo import *")
- If you want to use the extra functionality provided by the micropython-lib version, you can copy ... / to your device (or use upip to install it).

The surprising part though, that maybe would have avoided this confusion in the first place is that somehow "urandom" isn't listed in the documentation ... -libraries I have raised to track this.

Another point is that different ports will have different methods available. This is the MICROPY_PY_URANDOM_EXTRA_FUNCS you'll see if you look at modurandom.c. So ESP32 and STM32 have these methods, but (for example) ESP8266 won't.

Re: Libraries used in Micropython

Posted: Sun Jan 12, 2020 3:50 am
by mybigman
Thanks for all the replies guys much appreciated.

As a new comer to MP it definitely makes it difficult when something working when your looking at the libraries and can see it has that code.

I ended up just copying a couple methods over that I needed.

Hopefully I will start getting my head around it.