I am dealing with some python code automatically generated for me. I want to avoid manually editing these python files & hence this question/issue:
foo.py:
def foo():
print "foo"
boo.py:
def boo():
foo.foo() # <-- global name 'foo' not defined
print "boo"
bar.py:
import foo
import boo
def bar():
boo.boo()
print "bar"
Execution of:
python.exe bar.py
gives an error that boo did not find foo. But bar is importing both foo & boo. Shouldn’t foo be automatically available to boo?
Is there a way to do so? As said boo.py is automatically generated for me & I want to avoid adding import foo to boo.py.
Thanks.
No it shouldn’t:
import, like any other way to bind a name, binds that name in a single, specific scope, not “in all scopes you could ever possibly want it in”.There’s one very bad hack — I wouldn’t want to live with it (I’d much rather pour my energy into getting that totally broken code generator that makes
boo.pyfixed — if it has such a huge bug as missing a crucial needed import, what other horrors can it have in store?!), but, hey, it ain’t my funeral…;-)Have
bar.pystart…:This way you’ve made identifier
fooa “fake, artificial built-in name” (the only kind of name that is available from every scope, unless shadowed by other intervening bindings of the namein closer scopes) referring to the module
foo.NOT recommended procedure, just a temporary workaround for the horrible, glaring bug in the code generator that builds
boo.py. Get that bug fixed so you can retire this hack ASAP!