import namespace issues

Harald Kirsch kirschh at lionbioscience.com
Tue Jul 10 05:07:29 EDT 2001


THE PROBLEM:
I have to separate a module's function into two .py-files. Now I would
like to import both in a way that they behave like they are one file.

FIRST TRY:
Suppose the files are called first.py and second.py. I put a

  from second.py import *

into first.py. This does not work as necessary since functions in
second.py don't find functions defined in first.py. Same story when
using __import__().

FINDINGS:
What I gathered from experiments is that even with `from ... import'
as well as when using __import__('second', my_globals, my_locals)
there will *always* be a *new* dictionary used for the globals() of
the imported functions. Example:

Given that second.py contains

  def doStuff():
    print hex(id(globals()))

it will not report the same id as

  print my_globals

even if imported with __import__ as shown above.

QUESTION 1:
How can I achieve to merge two .py into one globals() as if the were
in one imported file while maintaining the file-search semantics of
`import'? 

QUESTION 2:
Is there a detailed description somewhere in the docs about the
namespaces (or dictionaries) used/created/shuffled during an import?

Thanks,
  Harald Kirsch
-- 
----------------+------------------------------------------------------
Harald Kirsch   | kirschh at lionbioscience.com | "How old is the epsilon?"
LION bioscience | +49 6221 4038 172          |        -- Paul Erdös
       *** Please do not send me copies of your posts. ***



More information about the Python-list mailing list