How to install modules in a Jupyter Micropython kernel?

All ESP32 boards running MicroPython.
Target audience: MicroPython users with an ESP32 board.
EDW26
Posts: 9
Joined: Sat Jan 19, 2019 2:19 am

How to install modules in a Jupyter Micropython kernel?

Post by EDW26 » Sat Jan 19, 2019 2:31 am

Well, I am new to micropython and would like to install modules in the MicroPython kernel of jupyter but I do not know how. The module I want to use is the tsl2561 sensor. I have tried to use upip but it is not working or to install other libraries, it might be that something is missing to install. Anyway, I really need some help with this because I have been trying to install the library for weeks now. I installed the tsl2561 library with your requirements using pip on my linux terminal but it only appears to import into the Python3 kernel of Jupyter. Thank you for any help.
Last edited by EDW26 on Sat Jan 19, 2019 3:28 pm, edited 1 time in total.

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

Re: How to install modules without a Jupyter Micropython kernel?

Post by stijn » Sat Jan 19, 2019 8:18 am

There are multiple Jupyter kernel implementations for MicroPython so you should specify which one you use. Also please clarify what 'tried to use upip but it is not working' means. What error occurs etc?

But in any case all kernels work with the same principle so to get something to import, the directory has to be in the search path. This code

Code: Select all

import sys
print(sys.path)
shows you which directories are searched for modules to imports. If the module isn't in one of those directories you cannot import it. So one thing you can do is just download the module directory and put it in one of those directories. Or download it, and add the directory to which it's downloaded to the MICROPYPATH environment variable.

EDW26
Posts: 9
Joined: Sat Jan 19, 2019 2:19 am

Re: How to install modules in a Jupyter Micropython kernel?

Post by EDW26 » Sat Jan 19, 2019 8:09 pm

Well, I have installed my jupyter_micropython_kernel on my notebook. And I tried using upip.install ('micropython-urllib') to test the upip operation and it did not work. I have a tsl2561 sensor directory and I used the command sys.path.insert (0, "/home/usr/anaconda3/lib/python3.7/site-packages/tsl2561"), but I try to import the tsl2561 module and it appears that there is no module. It has a tsl2561.py file. I used print (sys.path) and got ['', '/ lib'].

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

Re: How to install modules in a Jupyter Micropython kernel?

Post by stijn » Sun Jan 20, 2019 9:11 am

Sorry but without more information it's pretty hard to help; you should paste error messages here instead of describing your interpretation of them. Likewise for the code you are using. Anyway, couple of problems here:
- the package which pip from conda installs is this one https://pypi.org/project/tsl2561 which is a CPython package and won't work with MicroPython
- yet, I don't understand how that directory you added to the path has tsl2561.py but you cannot import it (unless 'appears there is no module' actually means the file has errors, in which case those will be printed, which is a different error than the typical 'no module named xxx'). You do use that sys.path.insert() call before trying to import, right?
- you shoudln't be adding the tsl2561 directory itself to MICROPYPATH, but the parent directory directory: the tsl2561 directory has an __init__.py, it's a so-called package, so the whole directory is supposed to be imported not the tsl2561/tsl2561.py file itself. But adding the whole of your conda installation's site-packages directory to MICROPYPATH is a bad idea because packages in there are meant for CPython usually, not MicroPython, and names might interfere.

Long story short: first figure out what module you really need (could be https://github.com/adafruit/micropython ... it-tsl2561, or https://github.com/adafruit/Adafruit_Ci ... on_TSL2561, hard to tell). Then if it has a corresponding package which upip can install, use that. If not, or upip doesn't work, manually download the correct file (s) and make sure they are in the import path taking package directories into account if needed.

EDW26
Posts: 9
Joined: Sat Jan 19, 2019 2:19 am

Re: How to install modules in a Jupyter Micropython kernel?

Post by EDW26 » Sun Jan 20, 2019 3:10 pm

Well. I'll send my tests to you from my code:
1) I cloned the git hub folder;
2) I got the following folder: micropython-adafruit-tsl2561;
3) Then I tried to install by upip with the following code:

upip.install ('micropython-adafruit-tsl2561')
Answer:
Installing to: / lib /
Error installing 'micropython-adafruit-tsl2561': list index out of range, packages may be partially installed.

4) I tried to install through the terminal of my linux:

pip install micropython-adafruit-tsl2561
Answer:
Could not find a version that satisfies the requirement micropython-adafruit-tsl2561 (from versions:)
No matching distribution found for micropython-adafruit-tsl2561

5) the same procedure for micropython-adafruit-tsl2561-master.

EDW26
Posts: 9
Joined: Sat Jan 19, 2019 2:19 am

Re: How to install modules in a Jupyter Micropython kernel?

Post by EDW26 » Sun Jan 20, 2019 8:53 pm

I installed it by python in the linux terminal ... but the file appears in the pip, I tried to import but I can not, I used up up again but it did not work, the same sentence appears from above.

eduarda@eduarda-Lenovo-ideapad-320-15IKB:~/Downloads/micropython-adafruit-tsl2561-master$ python setup.py install
running install
running build
running build_py
creating build
creating build/lib
copying tsl2561.py -> build/lib
running install_lib
copying build/lib/tsl2561.py -> /home/eduarda/anaconda3/lib/python3.7/site-packages
byte-compiling /home/eduarda/anaconda3/lib/python3.7/site-packages/tsl2561.py to tsl2561.cpython-37.pyc
running install_egg_info
Writing /home/eduarda/anaconda3/lib/python3.7/site-packages/micropython_adafruit_tsl2561-1.0-py3.7.egg-info

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

Re: How to install modules in a Jupyter Micropython kernel?

Post by stijn » Mon Jan 21, 2019 8:47 am

You should report the upuip problem on github (https://github.com/micropython/micropython) because it looks like a bug, it shouldn't report vague errors like 'index out of range'. It should report 'package not found' or so instead. It's also weird that it wants to install to /lib/ instead of ~/.micropython/lib. Is this the latest version of upip? And note if you use pip you don't have to clone anything first: upip downloads what it needs already and as far as I know cannot install from local repositories anyway.

But as said, if upip fails, manually download the package and add the top directory to sys.path, either in code or by modifying the MICROPYPATH environment variable. You cannot mix CPython and microPython, so running 'python setup.py' makes little sense, it's no use getting that installed in your Anaconda environment.

EDW26
Posts: 9
Joined: Sat Jan 19, 2019 2:19 am

Re: How to install modules in a Jupyter Micropython kernel?

Post by EDW26 » Tue Jan 22, 2019 12:16 am

Hi! Thanks for all the help so far. I do not know why sys.path is in / lib as well. Could you explain me how to manually download, what it takes ... an example code form would be very good ... And I did not understand how to identicate the main directory. THX

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

Re: How to install modules in a Jupyter Micropython kernel?

Post by stijn » Tue Jan 22, 2019 12:13 pm

In this case I'd just clone from https://github.com/adafruit/micropython ... it-tsl2561, say to ~/.micropython/lib/micropython-adafruit-tsl2561, delete setup.py from that directory (just so you don't accidentally import setup), sys.path.append('~/.micropython/lib/micropython-adafruit-tsl2561'), import tsl2561. Whether that will work or not depends on the version of MicroPython you have probably: the README.md says the library is deprecated.

But anyway: even if this works it doesn't really help you if you want another module. In the long run it's better to figure out what that upip bug is, and where the '/ lib' comes from. (Is there really a space in that name??)

EDW26
Posts: 9
Joined: Sat Jan 19, 2019 2:19 am

Re: How to install modules in a Jupyter Micropython kernel?

Post by EDW26 » Sun Jan 27, 2019 9:11 pm

So I could see the error that was occurring with upip. I used the ESP32 microcontroller and the I was not connecting the ESP to the Wifi, from the upip worked for the facilities like the 'os' library. But then I tried to install the tsl2561 module and I could not.

upip.install ('micropython-tsl2561')
Answer:
Installing to: / lib /
.Error installing 'micropython-adafruit-tsl2561': Package not found, packages may be partially installed

Post Reply