1

When a.py has this code:

class A():
    def __init__(self):
        print 'hi'

I use class A with this code:

import a
b = a.A()

I need to do the same thing with __import__, and I tried this page:Why does Python's __import__ require fromlist?

__import__("a", fromlist=[])
#import a
b = a.A()

However, I got name 'a' is not defined error. What might be wrong?

Community
  • 1
  • 1
prosseek
  • 182,215
  • 215
  • 566
  • 871

1 Answers1

4

__import__ returns a module. You need to bind the result to a name:

a = __import__("a")
a.A()
mgilson
  • 300,191
  • 65
  • 633
  • 696