Altering imported modules

Tro troworld at gmail.com
Wed Mar 5 01:16:00 EST 2008


On Tuesday 04 March 2008, Steve Holden wrote:
> >> Are you trying to interfere with the default module on only your
> >> machine?  Just rename it.  If something in the std. lib. imports
> >> asyncore, they get yours too that way.
> >
> > No, I'd like it to be a generalized solution and only for this one
> > project. I'm trying to get it to work on any recent python installation
> > out of the box, so I can't rename built-in modules. What I'm trying to do
> > is allow a 3rd party module (tlslite) to import *my* version of asyncore
> > without me going into tlslite's code and changing its import statement
> > explicitly, but only for the scope of this one project.
>
> In that case try something like
>
> import myasyncore as asnycore
> sys.modules['asyncore'] = asyncore
> import tlslite

Ah. That's what I've been looking for. Thanks!

Tro



More information about the Python-list mailing list