Scoping issue with import

James Stroud jstroud at mbi.ucla.edu
Mon Feb 28 18:56:39 EST 2005


Say I have a module, we'll call it "my_imported_mod". It contains a function 
in it that calls another function, "myfun". The "myfun" function is in the 
module "my_main_mod", that imports "my_imported_mod".

The code of "my_main_mod" might look like this:
==============
from my_imported_mod import *

def myfun():
  print "stuff"
==============

the code of "my_imported_mod" might look like this:
==============
def somefun():
  myfun()
==============

When trying to execute the function somefun(), I get a
NameError: global name 'myfun' is not defined

How to rectify this with minimal code change? How to let imported modules know 
about the namespace they are getting imported into? I do not want to 
restructure my code right now.

Thanks in advance for help.

James



More information about the Python-list mailing list