import hook, overwrite import?

Discussion in 'Python' started by Torsten Mohr, Jan 26, 2005.

  1. Torsten Mohr

    Torsten Mohr Guest

    Hi,

    is there some description available to overwrite the import
    hook? By googling i found out so far that i need to overwrite
    __builtins__.__import__ with something else.

    Can i also do this with a C function that is provided when
    using an embedded python interpreter? So my own C program
    provides this and when linking with python.lib the function
    is overwritten?

    Or is there some extension hook?

    Does this also handle "from module import *" not only the normal
    "import module"?


    Thanks for any hints,
    Torsten.
     
    Torsten Mohr, Jan 26, 2005
    #1
    1. Advertising

  2. Torsten Mohr

    Kartic Guest

    Hi Torsten,

    If you want to use other methods to import (other than good ole file
    system), yes, you can create an importer class and register it as an
    importer module, that import will use to search and import.

    For example, it is possible to use zip imports (this functionality is
    already builtin) to import from a zip archive.
    py>>> import zlib # required
    py>>> import sys
    py>>> sys.path.append('/location/to/zippedmodules.zip')
    py>>> import testzip
    py>>> testzip.__file__
    '/location/to/zippedmodules.zip/testzip,py'

    To generally do it, you have to:
    1. Create a class that provides a load_module method that returns a
    module type.
    2. Install your class as a hook using
    sys.path_hooks.append(your_importer_class)

    Please take a look at the imp module :
    http://docs.python.org/lib/module-imp.html for a complete description
    on accessing the import internals. There is also a simple example in
    this section.

    Is this is what you are looking for?

    Thanks,
    --Kartic
    PS: This about how much I know...the more I find out, I will share :)
     
    Kartic, Jan 26, 2005
    #2
    1. Advertising

  3. Torsten Mohr

    Steve Holden Guest

    Kartic wrote:

    > Hi Torsten,
    >
    > If you want to use other methods to import (other than good ole file
    > system), yes, you can create an importer class and register it as an
    > importer module, that import will use to search and import.
    >
    > For example, it is possible to use zip imports (this functionality is
    > already builtin) to import from a zip archive.
    > py>>> import zlib # required
    > py>>> import sys
    > py>>> sys.path.append('/location/to/zippedmodules.zip')
    > py>>> import testzip
    > py>>> testzip.__file__
    > '/location/to/zippedmodules.zip/testzip,py'
    >
    > To generally do it, you have to:
    > 1. Create a class that provides a load_module method that returns a
    > module type.
    > 2. Install your class as a hook using
    > sys.path_hooks.append(your_importer_class)
    >
    > Please take a look at the imp module :
    > http://docs.python.org/lib/module-imp.html for a complete description
    > on accessing the import internals. There is also a simple example in
    > this section.
    >
    > Is this is what you are looking for?
    >
    > Thanks,
    > --Kartic
    > PS: This about how much I know...the more I find out, I will share :)
    >

    I will just chime in to say I too am looking for information in this
    area. I hope to put some sort of BoF or Open Space event together for
    people wishing to learn about (and teach about) the import system from
    PEP 302 at PyCon this year.

    Early bird registration rates are still available today and tomorrow!

    regards
    Steve
     
    Steve Holden, Jan 27, 2005
    #3
    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. David Pifer via .NET 247

    Hook JScript Client Events to Server Controls

    David Pifer via .NET 247, May 2, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    2,804
    David Jessee
    May 2, 2004
  2. Noam Raphael
    Replies:
    0
    Views:
    284
    Noam Raphael
    Jun 21, 2004
  3. Jeremy Sanders

    import hook

    Jeremy Sanders, Jun 11, 2006, in forum: Python
    Replies:
    4
    Views:
    395
    Jeremy Sanders
    Jun 18, 2006
  4. Andrea Crotti

    simple import hook

    Andrea Crotti, Nov 10, 2011, in forum: Python
    Replies:
    0
    Views:
    145
    Andrea Crotti
    Nov 10, 2011
  5. Andrea Crotti
    Replies:
    0
    Views:
    163
    Andrea Crotti
    Jan 19, 2012
Loading...

Share This Page