Hiding modules

Miki Tebeka miki.tebeka at zoran.com
Mon Feb 16 06:59:10 EST 2004


Hello Tommy,


> Is there anything that can be done to import modules conditionally?
> Any ideas?
Writing a custom REPL (read-eval-print-loop) should be easy enough:
import re
split = re.compile("\s+").split

forbidden = ("os", "sys")

def repl():
    while 1:
        s = raw_input(">>> ").strip()
        ok = 1
        if s.startswith("import"):
            for module in split(s.strip())[1:]:
                if module in forbidden:
                    print "can't use %s module" % module
                    ok = 0
                    break
        if ok:
            exec(s)

while 1:
    repl()

HTH.
Miki



More information about the Python-list mailing list