Libraries used in Micropython

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
mybigman
Posts: 4
Joined: Tue Jan 07, 2020 11:15 am

Libraries used in Micropython

Post by mybigman » Sat Jan 11, 2020 4:21 am

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?

Thanks

User avatar
tve
Posts: 60
Joined: Wed Jan 01, 2020 10:12 pm
Location: Santa Barbara, CA
Contact:

Re: Libraries used in Micropython

Post by tve » Sat Jan 11, 2020 5:42 am

I hope someone in the know gives you a more general answer, but this specific library seems to be implemented here: https://github.com/micropython/micropyt ... durandom.c

stijn
Posts: 409
Joined: Thu Apr 24, 2014 9:13 am

Re: Libraries used in Micropython

Post by stijn » Sat Jan 11, 2020 8:30 am

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 http://docs.micropython.org/en/latest/l ... index.html.

User avatar
jimmo
Posts: 1183
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: Libraries used in Micropython

Post by jimmo » Sat Jan 11, 2020 10:29 am

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 "foo.py", then it will be imported preferentially to ufoo when you write "import foo". But foo.py 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 https://github.com/micropython/micropyt ... /random.py 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 http://docs.micropython.org/en/latest/l ... -libraries I have raised https://github.com/micropython/micropython/issues/5518 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.

mybigman
Posts: 4
Joined: Tue Jan 07, 2020 11:15 am

Re: Libraries used in Micropython

Post by mybigman » Sun Jan 12, 2020 3:50 am

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.

Post Reply