Page 1 of 2

Noob question on Modules

Posted: Sun Feb 05, 2017 7:14 pm
by spynappels
Hi All,

I have a question on using Micropython modules sourced from PyPI.

I have several esp8266 (Wemos D1 mini) boards which I have flashed with the Micropython firmware and have been using to improve my Python ability.

However, I have a question on using Micropython modules from Pypi, such as the https://pypi.python.org/pypi/micropytho ... mple/1.3.2 module. On a normal Python dev system, such as a Raspberry Pi, I'd just use virtualenv or pip install micropython-umqtt.simple and then use an import.
This obviously doesn't work on these boards, so I'm looking for some guidance on using these modules.

I believe that I could simply copy the .py files to the root filesystem of the board, but I'm just wondering on the above module as it actually is a umqtt directory with simple.py in it. Would I recreate that on the board filesystem, and if so, how would I import the module?
I was under the impression that the .py file name would have to match the module name so in this case would I import simple?

I know we have plenty of space for storing the modules on the board, my questions revolve mostly about getting them on the board and then calling them in my code in the most efficient manner.

Thanks for any help you can give me.
Stef

Re: Noob question on Modules

Posted: Mon Feb 06, 2017 7:38 am
by SpotlightKid
spynappels wrote: I believe that I could simply copy the .py files to the root filesystem of the board, but I'm just wondering on the above module as it actually is a umqtt directory with simple.py in it. Would I recreate that on the board filesystem, and if so, how would I import the module?
Yes, create a "umqtt" directory on the esp8226 and copy "simple.py" into it. Then import with:

Code: Select all

from umqtt.simple import whatever
Or just rename the module as "umqtt_simple.py", copy it to the root of the filesystem and then import with:

Code: Select all

from umqtt_simple import whatever
HTH, Chris

Re: Noob question on Modules

Posted: Mon Feb 06, 2017 8:02 am
by pythoncoder
@SpotlightKid For the first method to work the umqtt directory would need to contain a file __init__.py

The file may be empty, but marks the directory as containing a Python package.

Re: Noob question on Modules

Posted: Mon Feb 06, 2017 8:08 am
by SpotlightKid
Are you sure? AFAIK Python 3 doesn't require "__init__.py" files anymore.

I always just used the second method myself, though.

Re: Noob question on Modules

Posted: Mon Feb 06, 2017 8:09 am
by spynappels
Thanks for the responses guys, I'll try that and see how I get on. I assume that the directory including the __init__.py file is the "proper" way to do it?

Re: Noob question on Modules

Posted: Mon Feb 06, 2017 8:13 am
by SpotlightKid
In any case, the first method is the way to go, if you want to have other modules in the "umqtt" namespace, which you would then copy into the same directory and import with:

Code: Select all

from umqtt.something import whatever

Re: Noob question on Modules

Posted: Mon Feb 06, 2017 5:25 pm
by pythoncoder
@SpotlightKid The docs for Python 3.3.6 https://docs.python.org/3.3/tutorial/mo ... l#packages states "The __init__.py files are required to make Python treat the directories as containing packages...".

Re: Noob question on Modules

Posted: Mon Feb 06, 2017 6:20 pm
by SpotlightKid
Then these docs are out of date.

Micropython (being a Python 3 implementation), agrees with me:

Code: Select all

$ mkdir -p test/spamm
$ cd test
$ echo "get_spamm = lambda: 'Spamm, Spamm, Spamm'" > spamm/ham.py
$ MICROPYPATH=. micropython
MicroPython v1.8.7-187-gbd04ed3e8 on 2017-02-04; linux version
Use Ctrl-D to exit, Ctrl-E for paste mode
>>> from spamm.ham import get_spamm; get_spamm()
'Spamm, Spamm, Spamm'

Code: Select all

$ rshell -p /dev/ttyUSB0 -b 115200 mkdir /flash/spamm
Connecting to /dev/ttyUSB0 ...
$ rshell -p /dev/ttyUSB0 -b 115200 repl
Connecting to /dev/ttyUSB0 ...
Entering REPL. Use Control-X to exit.
>
MicroPython v1.8.7-170-gb32880bd on 2017-02-01; ESP module with ESP8266
Type "help()" for more information.
>>> 
paste mode; Ctrl-C to cancel, Ctrl-D to finish
=== with open('spamm/ham.py', 'w') as fp:
===     fp.write('get_spamm = lambda: "Spamm, Spamm, Spamm"\n')
=== 
42
>>> from spamm.ham import get_spamm; get_spamm()
'Spamm, Spamm, Spamm'

Re: Noob question on Modules

Posted: Tue Feb 07, 2017 7:32 am
by pythoncoder
Thanks for that - I stand corrected. So much for the official docs :o

Re: Noob question on Modules

Posted: Tue Feb 07, 2017 7:39 am
by SpotlightKid
I'm a little bit surprised myself that the official tutorial is inaccurate. But then again it has been around since the early nineties!