Enabling/disabling module imports based on a setting

  • Thread starter Adrian Holovaty
  • Start date
A

Adrian Holovaty

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 said:
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
 

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,763
Messages
2,569,562
Members
45,038
Latest member
OrderProperKetocapsules

Latest Threads

Top