Noob question on Modules
-
- Posts: 10
- Joined: Wed Jan 25, 2017 11:34 am
Noob question on Modules
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
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
-
- Posts: 463
- Joined: Wed Apr 08, 2015 5:19 am
Re: Noob question on Modules
Yes, create a "umqtt" directory on the esp8226 and copy "simple.py" into it. Then import with: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?
Code: Select all
from umqtt.simple import whatever
Code: Select all
from umqtt_simple import whatever
- pythoncoder
- Posts: 5956
- Joined: Fri Jul 18, 2014 8:01 am
- Location: UK
- Contact:
Re: Noob question on Modules
@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.
The file may be empty, but marks the directory as containing a Python package.
Peter Hinch
Index to my micropython libraries.
Index to my micropython libraries.
-
- Posts: 463
- Joined: Wed Apr 08, 2015 5:19 am
Re: Noob question on Modules
Are you sure? AFAIK Python 3 doesn't require "__init__.py" files anymore.
I always just used the second method myself, though.
I always just used the second method myself, though.
-
- Posts: 10
- Joined: Wed Jan 25, 2017 11:34 am
Re: Noob question on Modules
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?
-
- Posts: 463
- Joined: Wed Apr 08, 2015 5:19 am
Re: Noob question on Modules
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
- pythoncoder
- Posts: 5956
- Joined: Fri Jul 18, 2014 8:01 am
- Location: UK
- Contact:
Re: Noob question on Modules
@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.
Index to my micropython libraries.
-
- Posts: 463
- Joined: Wed Apr 08, 2015 5:19 am
Re: Noob question on Modules
Then these docs are out of date.
Micropython (being a Python 3 implementation), agrees with me:
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'
- pythoncoder
- Posts: 5956
- Joined: Fri Jul 18, 2014 8:01 am
- Location: UK
- Contact:
Re: Noob question on Modules
Thanks for that - I stand corrected. So much for the official docs
Peter Hinch
Index to my micropython libraries.
Index to my micropython libraries.
-
- Posts: 463
- Joined: Wed Apr 08, 2015 5:19 am
Re: Noob question on Modules
I'm a little bit surprised myself that the official tutorial is inaccurate. But then again it has been around since the early nineties!