Noob question on Modules

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
spynappels
Posts: 10
Joined: Wed Jan 25, 2017 11:34 am

Noob question on Modules

Post by spynappels » Sun Feb 05, 2017 7:14 pm

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

SpotlightKid
Posts: 463
Joined: Wed Apr 08, 2015 5:19 am

Re: Noob question on Modules

Post by SpotlightKid » Mon Feb 06, 2017 7:38 am

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

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Noob question on Modules

Post by pythoncoder » Mon Feb 06, 2017 8:02 am

@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.
Peter Hinch
Index to my micropython libraries.

SpotlightKid
Posts: 463
Joined: Wed Apr 08, 2015 5:19 am

Re: Noob question on Modules

Post by SpotlightKid » Mon Feb 06, 2017 8:08 am

Are you sure? AFAIK Python 3 doesn't require "__init__.py" files anymore.

I always just used the second method myself, though.

spynappels
Posts: 10
Joined: Wed Jan 25, 2017 11:34 am

Re: Noob question on Modules

Post by spynappels » Mon Feb 06, 2017 8:09 am

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?

SpotlightKid
Posts: 463
Joined: Wed Apr 08, 2015 5:19 am

Re: Noob question on Modules

Post by SpotlightKid » Mon Feb 06, 2017 8:13 am

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

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Noob question on Modules

Post by pythoncoder » Mon Feb 06, 2017 5:25 pm

@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...".
Peter Hinch
Index to my micropython libraries.

SpotlightKid
Posts: 463
Joined: Wed Apr 08, 2015 5:19 am

Re: Noob question on Modules

Post by SpotlightKid » Mon Feb 06, 2017 6:20 pm

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'

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Noob question on Modules

Post by pythoncoder » Tue Feb 07, 2017 7:32 am

Thanks for that - I stand corrected. So much for the official docs :o
Peter Hinch
Index to my micropython libraries.

SpotlightKid
Posts: 463
Joined: Wed Apr 08, 2015 5:19 am

Re: Noob question on Modules

Post by SpotlightKid » Tue Feb 07, 2017 7:39 am

I'm a little bit surprised myself that the official tutorial is inaccurate. But then again it has been around since the early nineties!

Post Reply