Altering imported modules

Tro troworld at gmail.com
Tue Mar 4 00:09:31 CET 2008


On Sunday 02 March 2008, Paul McGuire wrote:
> On Mar 2, 3:48 pm, Tro <trowo... at gmail.com> wrote:
> > On Sunday 02 March 2008, Terry Reedy wrote:
> > > "Tro" <trowo... at gmail.com> wrote in message
> > >news:200803011856.27611.troworld at gmail.com...
> > >
> > > | Hi, list.
> > > |
> > > | I've got a simple asyncore-based server. However, I've modified the
> > >
> > > asyncore
> > >
> > > | module to allow me to watch functions as well as sockets. The
> > > | modified asyncore module is in a specific location in my project and
> > > | is imported
> > >
> > > as
> > >
> > > | usual from my classes.
> > > |
> > > | Now I'd like to use the tlslite library, which includes an asyncore
> > > | mixin class. However, tlslite imports "asyncore", which doesn't
> > > | include my own modifications.
> > > |
> > > | I'd like to know if it's possible to make tlslite load *my* asyncore
> > >
> > > module
> > >
> > > | without changing any of the tlslite code.
> > >
> > > If your module is also 'asyncore' and comes earlier in the search path,
> > > I would expect the import to get yours.
> >
> > It's not. It has a package prefix like my.package.asyncore. I think I can
> > either move my version of asyncore up a couple of levels or add the
> > my.package directory to sys.path.
> >
> > My version of asyncore imports several functions from the built-in
> > asyncore. Now that my version of it is imported as asyncore, how would it
> > import the built-in version from python2.5/site-packages?
> >
> > Thanks,
> > Tro
> >
> > 
>
> What happens if you do "import my.package.asyncore as asyncore"?
>
> If that doesn't work (trying the simplest hack first), I know that
> there are various hooks in the import mechanism that should help.

In the classes that use my version of asyncore currently, that is how I do it. 
I import my version as "import my.package.asyncore as asyncore". In my 
asyncore module I do "import asyncore", because I override a few functions 
from the asyncore module included with python. However, if I were to 
add "my.package" to sys.path, then I wouldn't be able to "import asyncore" 
from my own asyncore module. I'd have to do some trickery with sys.path to 
take the "my.package" component out, import standard asyncore, readd 
the "my.package" component, so that other modules can "import asyncore" and 
get my version.

Is there a way to import the standard python asyncore module in this scenario?

Thanks,
Tro



More information about the Python-list mailing list