I have three python files
one.py, two.py, three.py
one.py
from one.py I call
import two as two
two.three()
in two.py I have
def two():
"catch the error here then import and call three()"
import three as three
in three.py I have
def three():
print "three called"
so naturally I am getting:
AttributeError: ‘function’ object has no attribute ‘three’
My question is:
Is there a way to have two.py capture the error then import three.py and then call three()?
______________edit________________V
I can call it like this:
two().three()
def two():
import three as three
return three
but I would like to call it like so:
two.three()
So essentially it would auto exec def two():
Here’s the solution I came up with. I confess that I was inspired by your question to try to figure this out, so I don’t completely understand it myself. The magic happens in two.py where the attempt to access and then call the
threemethod oftwois handled by the__getattr__method of themethod_routerclass. It uses__import__to import the indicated module by name (a string), and then imitates thefrom blah import blahby callinggetattr()again on the imported module.one.py
two.py
three.py