Page 1 of 1

Porting a library from Circuitpython

Posted: Tue Jul 13, 2021 1:39 am
by hybotics
Hello,

I am working on porting some libraries from Circuitpython and updating the ht16k33 library that currently exists for Micropython. I do not understand why the import statements do not work within the code file but they will work fine in the repl.

I need some assistance here. What am I missing?

Given the included code and library,
These work perfectly in the REPL.
However, these do NOT work within code files.

Code: Select all

   from hybotics_ht16k33 import seg

   from hybotics_ht16k33 import matrix

   from hybotics_ht16k33.matrix import HT16K33
I get this traceback:

Code: Select all

MPY: soft reboot
Traceback (most recent call last):
  File "main.py", line 13, in <module>
  File "hybotics_ht16k33/seg.py", line 1, in <module>
ImportError: no module named 'ht16k33'
MicroPython v1.16 on 2021-06-23; TinyPICO with ESP32-PICO-D4
Thank you in advance for any help,

8-Dale

Re: Porting a library from Circuitpython

Posted: Tue Jul 13, 2021 9:19 am
by pythoncoder
I suspect some kind of file naming problem. The line

Code: Select all

from hybotics_ht16k33 import seg
loads the file hybotics_ht16k33/seg, the first line of which throws the error. This reads (from your zipfile)

Code: Select all

from hybotics_ht16k33.matrix import HT16K33
The traceback doesn't correspond to that line, which makes no reference to a module 'ht16k33'

Code: Select all

Traceback (most recent call last):
  File "main.py", line 13, in <module>
  File "hybotics_ht16k33/seg.py", line 1, in <module>
ImportError: no module named 'ht16k33'
So something is not as it seems. I'd check exactly what is being imported by putting a print statement as the fist line in each file.

Re: Porting a library from Circuitpython

Posted: Tue Jul 13, 2021 2:14 pm
by hybotics
The problem I am having is that the import statements work inside the REPL, but they throw errors when in my code. This what I am trying to figure out.

Here is an updated archive with a new test program - import_test.py I believe. All it has are various forms of import statements related to my library. Try each line in the REPL and they will work. In a code file, they do not work.

8-Dale

Re: Porting a library from Circuitpython

Posted: Tue Jul 13, 2021 3:31 pm
by aivarannamaa
Don't know if it is related, but in your zip you have file named "__init__" inside the package folder, but package marker should be named "__init__.py"

Re: Porting a library from Circuitpython

Posted: Tue Jul 13, 2021 11:49 pm
by hybotics
pythoncoder wrote:
Tue Jul 13, 2021 9:19 am
So something is not as it seems. I'd check exactly what is being imported by putting a print statement as the first line in each file
I found the problem! There were TWO copies of the library - one in the root directory and the other (correct) on in /lib. I feel so stupid right now. Micropython was just taking the first copy of the library it found. All squared away!

8-Dale

Re: Porting a library from Circuitpython

Posted: Wed Jul 14, 2021 8:56 am
by pythoncoder
aivarannamaa wrote:
Tue Jul 13, 2021 3:31 pm
Don't know if it is related, but in your zip you have file named "__init__" inside the package folder, but package marker should be named "__init__.py"
Just FYI current MicroPython behaviour is not to require an empty __init__.py in packages. If present the file will be imported as per earlier versions.

Re: Porting a library from Circuitpython

Posted: Sat Jul 17, 2021 12:10 am
by hybotics
Hi,

I am still having problems with my imports, and I do not understand why.

Why does Micropython not allow me to do something like

Code: Select all

from ht16k33.matrix import HT16K33
I really do not understand this. No matter what I do. I can not seem to get my imports right. I have attached an archive with the exact setup I am using now.

Help please. :)

Re: Porting a library from Circuitpython

Posted: Sat Jul 17, 2021 4:56 am
by kevinkk525
You are missing an __init__.py in both the root and the ht16k33 directory. Then it should work.
(btw, if it works in CPython, it'll work in micropython)