import class error

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
subutai6
Posts: 5
Joined: Fri Nov 11, 2016 5:25 pm

import class error

Post by subutai6 » Fri Nov 11, 2016 5:37 pm

Hi everyone,

I have a problem importing a class that I wrote.
I created a folder named HW in the flash inside that folder there are __init__.py and a StpMotor.py files.
inside __init__.py there is:
from StpMotor import StpMotor

inside StpMotor.py there is
class StpMotor:
def __init__(self):
....

When I import from the REPL with:
from HW import StpMotor
I get the following error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "HW/__init__.py", line 1, in <module>
ImportError: no module named 'StpMotor'
but if I repeat the command:
from HW import StpMotor

it works well!

Any ideas why and how to get it work at first attempt?

Thanks in advance

M

dwight.hubbard
Posts: 38
Joined: Mon May 16, 2016 6:35 pm

Re: import class error

Post by dwight.hubbard » Fri Nov 11, 2016 9:50 pm

The import in your __init__.py should probably be relative, so it knows it's importing the version in the same directory as __init__.py instead of following the path which causes it to re-import the __init__.py

So inside the __init.py the import should probably be:
from .StpMotor import StpMotor

subutai6
Posts: 5
Joined: Fri Nov 11, 2016 5:25 pm

Re: import class error

Post by subutai6 » Sat Nov 12, 2016 8:20 am

I solved the problem, with the __init__.py file empty everything works perfectly.

Thanks for the help

Post Reply