Porting a library from Circuitpython

Discussion about programs, libraries and tools that work with MicroPython. Mostly these are provided by a third party.
Target audience: All users and developers of MicroPython.
Post Reply
hybotics
Posts: 33
Joined: Tue Apr 03, 2018 2:58 am
Contact:

Porting a library from Circuitpython

Post by hybotics » Tue Jul 13, 2021 1:39 am

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
Attachments
hybotics_ht16k33.zip
(4.21 KiB) Downloaded 130 times

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

Re: Porting a library from Circuitpython

Post by pythoncoder » Tue Jul 13, 2021 9:19 am

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

hybotics
Posts: 33
Joined: Tue Apr 03, 2018 2:58 am
Contact:

Re: Porting a library from Circuitpython

Post by hybotics » Tue Jul 13, 2021 2:14 pm

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
Attachments
hybotics_ht16k33.zip
(4.21 KiB) Downloaded 132 times

User avatar
aivarannamaa
Posts: 171
Joined: Fri Sep 22, 2017 3:19 pm
Location: Estonia
Contact:

Re: Porting a library from Circuitpython

Post by aivarannamaa » 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"
Aivar Annamaa
https://thonny.org

hybotics
Posts: 33
Joined: Tue Apr 03, 2018 2:58 am
Contact:

Re: Porting a library from Circuitpython

Post by hybotics » Tue Jul 13, 2021 11:49 pm

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

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

Re: Porting a library from Circuitpython

Post by pythoncoder » Wed Jul 14, 2021 8:56 am

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

hybotics
Posts: 33
Joined: Tue Apr 03, 2018 2:58 am
Contact:

Re: Porting a library from Circuitpython

Post by hybotics » Sat Jul 17, 2021 12:10 am

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. :)
Attachments
hybotics.zip
(3.87 KiB) Downloaded 133 times

kevinkk525
Posts: 969
Joined: Sat Feb 03, 2018 7:02 pm

Re: Porting a library from Circuitpython

Post by kevinkk525 » Sat Jul 17, 2021 4:56 am

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)
Kevin Köck
Micropython Smarthome Firmware (with Home-Assistant integration): https://github.com/kevinkk525/pysmartnode

Post Reply