Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > import hook, overwrite import?

Reply
Thread Tools

import hook, overwrite import?

 
 
Torsten Mohr
Guest
Posts: n/a
 
      01-26-2005
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.


 
Reply With Quote
 
 
 
 
Kartic
Guest
Posts: n/a
 
      01-26-2005
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

 
Reply With Quote
 
 
 
 
Steve Holden
Guest
Posts: n/a
 
      01-27-2005
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Overwrite Default Constructor ? Philipp Leitner Java 26 04-19-2006 07:50 AM
Deployment in 2.0 - How To Not Overwrite web.config =?Utf-8?B?Um9iZXJ0?= ASP .Net 8 11-03-2005 03:50 AM
overwrite existing value of a textbox with null shruti tiwari via JavaKB.com Java 1 04-03-2005 04:24 AM
Re: JSP form submit does not overwrite request parameter value zalla rouge Java 0 09-02-2003 03:02 PM
Re: JSP form submit does not overwrite request parameter value VisionSet Java 1 09-02-2003 11:27 AM



Advertisments