Re: Altering imported modules

Discussion in 'Python' started by Tro, Mar 2, 2008.

  1. Tro

    Tro Guest

    On Sunday 02 March 2008, Terry Reedy wrote:
    > "Tro" <> wrote in message
    > news:...
    >
    > | 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
     
    Tro, Mar 2, 2008
    #1
    1. Advertising

  2. Tro

    Paul McGuire Guest

    On Mar 2, 3:48 pm, Tro <> wrote:
    > On Sunday 02 March 2008, Terry Reedy wrote:
    >
    >
    >
    >
    >
    > > "Tro" <> wrote in message
    > >news:...

    >
    > > | 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- Hide quoted text -
    >
    > - Show quoted text -


    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.

    -- Paul
     
    Paul McGuire, Mar 3, 2008
    #2
    1. Advertising

  3. Tro

    Tro Guest

    On Sunday 02 March 2008, Paul McGuire wrote:
    > On Mar 2, 3:48 pm, Tro <> wrote:
    > > On Sunday 02 March 2008, Terry Reedy wrote:
    > > > "Tro" <> wrote in message
    > > >news:...
    > > >
    > > > | 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
     
    Tro, Mar 3, 2008
    #3
  4. Tro

    Guest

    On Mar 3, 5:09 pm, Tro <> wrote:
    > On Sunday 02 March 2008, Paul McGuire wrote:
    >
    >
    >
    >
    >
    > > On Mar 2, 3:48 pm, Tro <> wrote:
    > > > On Sunday 02 March 2008, Terry Reedy wrote:
    > > > > "Tro" <> wrote in message
    > > > >news:...

    >
    > > > > | 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- Hide quoted text -
    >
    > - Show quoted text -


    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.
     
    , Mar 4, 2008
    #4
  5. Tro

    Tro Guest

    On Monday 03 March 2008, wrote:
    > On Mar 3, 5:09 pm, Tro <> wrote:
    > > On Sunday 02 March 2008, Paul McGuire wrote:
    > > > On Mar 2, 3:48 pm, Tro <> wrote:
    > > > > On Sunday 02 March 2008, Terry Reedy wrote:
    > > > > > "Tro" <> wrote in message
    > > > > >news:...
    > > > > >
    > > > > > | 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
    > >
    > >

    >
    > 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.

    Thanks,
    Tro
     
    Tro, Mar 4, 2008
    #5
  6. Tro

    Steve Holden Guest

    Tro wrote:
    > On Monday 03 March 2008, wrote:
    >> On Mar 3, 5:09 pm, Tro <> wrote:
    >>> On Sunday 02 March 2008, Paul McGuire wrote:
    >>>> On Mar 2, 3:48 pm, Tro <> wrote:
    >>>>> On Sunday 02 March 2008, Terry Reedy wrote:
    >>>>>> "Tro" <> wrote in message
    >>>>>> news:...
    >>>>>>
    >>>>>> | 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
    >>>
    >>>

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

    regards
    Steve
    --
    Steve Holden +1 571 484 6266 +1 800 494 3119
    Holden Web LLC http://www.holdenweb.com/
     
    Steve Holden, Mar 4, 2008
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jason

    namespace & imported modules

    Jason, Nov 24, 2004, in forum: Python
    Replies:
    3
    Views:
    371
    Peter Otten
    Nov 25, 2004
  2. Tomasz Lisowski
    Replies:
    4
    Views:
    364
    Tomasz Lisowski
    May 25, 2005
  3. Tro

    Altering imported modules

    Tro, Mar 1, 2008, in forum: Python
    Replies:
    5
    Views:
    299
  4. Dun Peal
    Replies:
    10
    Views:
    475
    Chris Rebert
    May 3, 2011
  5. Volker Nicolai
    Replies:
    9
    Views:
    1,125
    Fabian Pilkowski
    Jul 4, 2005
Loading...

Share This Page