Page 1 of 1

import class error

Posted: Fri Nov 11, 2016 5:37 pm
by subutai6
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

Re: import class error

Posted: Fri Nov 11, 2016 9:50 pm
by dwight.hubbard
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

Re: import class error

Posted: Sat Nov 12, 2016 8:20 am
by subutai6
I solved the problem, with the __init__.py file empty everything works perfectly.

Thanks for the help