Question about details of __import__

Mitko Haralanov mitko at qlogic.com
Thu Mar 29 13:42:33 EDT 2007


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					 mitko at qlogic.com
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.



More information about the Python-list mailing list