Question about details of __import__

M

Mitko Haralanov

Hi all,

I am going to do my best to describe the issue that I am having and
hopefully someone can shed some light on it:

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

core.py looks something like this (simplified version):

import log
class Core:
def __init__ (self):
self.logger = log.Logger ("core", log.ERROR,
"/home/logs/log")
try:
module = __import__ ("resources.simple",globals
(), locals (), ["simple"])
except ImportError:
print "Error importing"

self.rs = module.Resource ()

resources/simple/__init__.py look like this (again, simplified version):

import log
class Resource:
def __init__ (self):
self.logger = log.Logger ("resource", log.ERROR)

and finally, log.py:

import logging

global_info = [None, None, None]

def Logger (name, level, filename=None):
global global_info
print global_info
if not global_info[0] and not global_info[1]:
global_info[0] = name
global_info[1] = level
global_info[2] = filename

logging.basicConfig (level, format=...)
else:
if filename:
print "ERROR: logger already initialized"

return logging.getLogger (name)

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.

(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).

Could someone help with what could be going wrong here?
--
Mitko Haralanov (e-mail address removed)
Senior Software Engineer 650.934.8064
System Interconnect Group http://www.qlogic.com

==========================================
Professor: Some say I'm robbing the cradle but I say she's robbing the
grave.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,967
Messages
2,570,148
Members
46,694
Latest member
LetaCadwal

Latest Threads

Top