Creating Import Hooks

Jonathan Gardner jgardner at
Thu Feb 18 09:03:51 CET 2010

On Feb 17, 10:48 pm, Sreejith K <sreejith... at> wrote:
> Hi everyone,
> I need to implement custom import hooks for an application ( I want to restrict an application
> to import certain modules (say socket module). Google app engine is
> using a module hook to do this (HardenedModulesHook in google/
> appengine/tools/ But I want to allow that
> application to use an sdk module (custom) which imports and uses
> socket module. But the module hook restricts the access by sdk.
> Finding out, which file is importing a module give a solution?? ie. If
> the application is importing socket module, I want to restrict it. But
> if the sdk module is importing socket I want to allow it. Is there any
> way I can do this ?
> Application
> ========
> import sdk
> import socket               # I dont want to allow this (need to raise
> ImportError)
> ====
> import socket               # need to allow this

import socket

import SDK
import sys
socket = sys.modules['socket']

More information about the Python-list mailing list