Enabling/disabling module imports based on a setting
Adrian Holovaty
holovaty at gmail.com
Sat Nov 6 13:15:39 EST 2004
Hi, everybody,
How can I go about writing an import hook that shows or hides (i.e.,
does not allow import of) modules within a package based on a defined
setting?
Here's a basic example of what I'm trying to do, given a package
/usr/lib/python2.3/site-packages/hidden/ that has three modules in it
(one.py, two.py, three.py):
>>> ALLOWED_MODULES = ['one', 'two']
>>> from hidden import one
>>> from hidden import two
>>> from hidden import three
Traceback (most recent call last):
File "<stdin>", line 1, in ?
ImportError: No module named three
>>> ALLOWED_MODULES = ['one', 'two', 'three']
>>> from hidden import three
>>> # Because 'three' is in ALLOWED_MODULES, it worked.
The ALLOWED_MODULES setting wouldn't necessarily be changed at runtime;
I did that here just to demonstrate what I'm trying to do. I imagine
the import-hook code would do something like "from settings import
ALLOWED_MODULES" and take it from there.
Is this even possible? I'm on Python 2.3, if that helps.
Thanks,
Adrian
More information about the Python-list
mailing list