Question about details of __import__

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Thu Mar 29 14:43:46 EDT 2007


En Thu, 29 Mar 2007 14:42:33 -0300, Mitko Haralanov <mitko at qlogic.com>  
escribió:

> I have three modules that a comprising the problem:
> ./core.py
> ./log.py
> ./resources/simple/__init__.py

Surely there is a ./resources/__init__.py too?

> The problem that I am seeing is that 'global_info' in the log.py module
> is [None, None, None] on both calls of log.Logger (), even though the
> initial call (from core.py) sets it to the passed in values.
> According to the Python documentation, I am using the __import__
> statement correctly to get what I want? It seems like, the guts of the
> import/__import__ code are creating two different namespaces for the
> log.py module.

You may check if this is the case, looking at sys.modules

>  (if you are wondering why I am using __import__ in my class
> constructor, it is because the name of the module that should be
> imported is read out of a configuration file).

Anyway you could import the package and lookup the module inside using  
getattr:

import resources
name = "simple"
module = getattr(resources, name)
self.rs = module.Resource()

-- 
Gabriel Genellina




More information about the Python-list mailing list