__import__ not working

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
Lebowski
Posts: 7
Joined: Fri Dec 25, 2020 7:07 am

__import__ not working

Post by Lebowski » Thu May 13, 2021 9:14 am

Hi,
im using MicroPython v1.14-457-g3bf6c7f12

I've a module foo.bar.foobar

When running

Code: Select all

import foo.bar.foobar
everything is fine, i can use the module.
When running (for some reason i've to do it this way)

Code: Select all

__import__("foo.bar.foobar")
the module i not import. Ther is nor rrrormessage while running this command. But i get an
AameError: name 'foo' isn't defined
Any ideas?

Thank you

stijn
Posts: 735
Joined: Thu Apr 24, 2014 9:13 am

Re: __import__ not working

Post by stijn » Thu May 13, 2021 1:32 pm

Cannot reproduce this. Can you add more information: directory structure, value of sys.path, which port you are using?

Lebowski
Posts: 7
Joined: Fri Dec 25, 2020 7:07 am

Re: __import__ not working

Post by Lebowski » Thu May 13, 2021 3:37 pm

Hi,

i think i got it: __import__("foo.bar.foobar") returns a modules object.

Code: Select all

m=__import__("foo.bar.foobar")
To create an object from foo.bar.foobar.Foo you have to use

Code: Select all

f=m.Foo()

Post Reply