Scoping issue with import

Calvin Spealman calvin at ironfroggy.com
Mon Feb 28 14:28:00 EST 2005


Each module is only 'aware' of the built-ins and the modules it itself
imports. So, all you need to do is add this line to my_imported_mod:

from my_main_mod import myfun

This is a fully intentional feature. Modules stand on their own.

James Stroud wrote:

> 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