Removing modules from the global namespace
Chris Jaeger
cjaeger at ensim.com
Mon May 14 17:06:48 EDT 2001
Hi all,
I was wondering if there was a way to remove
imported modules from a namespace. For instance, I
have an __init__.py that loads a dynamic list of modules
based on what it finds present in the directory. In order
to determine the contents of the directory, I need to import
os, string, etc. Before I return from __init__.py, I'd like
to remove all the modules from the namespace for this package
except for the ones that come from this directory. Can this
be done?
Here is my current __init__.py code:
import os
import re
import string
local_dir = os.path.dirname(__file__)
entries = os.listdir(local_dir)
for entry in entries:
if re.match(r"*.\.py$",entry) or re.match(r"*.\.pyc",entry):
module = string.join(string.split(entry,'.')[:-1],'.')
if module != "__init__":
__import__(module, globals(), locals(), [])
# this doesn't work
delattr(globals(), 'os')
delattr(globals(), 're')
delattr(globals(), 'string')
More information about the Python-list
mailing list