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
import class error
-
- Posts: 38
- Joined: Mon May 16, 2016 6:35 pm
Re: import class error
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
So inside the __init.py the import should probably be:
from .StpMotor import StpMotor
Re: import class error
I solved the problem, with the __init__.py file empty everything works perfectly.
Thanks for the help
Thanks for the help