473,800 Members | 2,323 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

import hook, overwrite import?

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.
Jul 18 '05 #1
2 3054
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.z ip')
py>>> import testzip
py>>> testzip.__file_ _
'/location/to/zippedmodules.z ip/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_imp orter_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 :-)

Jul 18 '05 #2
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.z ip')
py>>> import testzip
py>>> testzip.__file_ _
'/location/to/zippedmodules.z ip/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_imp orter_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

Jul 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
2085
by: Grant D. Watson | last post by:
If this has been answered before, or if my terminology is off, please bear with me; my Python experience is limited to use in one class and to personal projects. I'd like to do something rather silly: I'd like to run a particular piece of code from a given module every time that the module is imported, and not just at the time that the module is originally loaded. So, every time a module says import foo or something analogous, I want...
0
1032
by: F. GEIGER | last post by:
Hi all, I use Leo for programming. Among other goodies Leo provides it let's me easily share code between different Python apps. When I have an app consisting of more than one file, they usually do from other import AClassOfIt import yetanother When I then want to investigate a specific problem I share code with a
0
1997
by: John Roth | last post by:
I've found a case where it seems that Python is importing two copies of a module without any reason or indication. It took me a while to verify that this is what is occuring: I had to write a __import__ hook to trace the actual activity. The source code for the hook is below. There are several examples of the problem in the trace; this is one particularly juicy one. -----------Part 1 ---------------- '0059' Import 'TypeAdapter'...
0
1123
by: Noam Raphael | last post by:
Hello, Do you know of a reference implementation of an import hook? I mean a class, written in Python, that imitates Python's default module loading behaviour. I mean that I would be able to append the class to sys.path_hooks, and modules will be imported in the same way they are currently imported. I want it so that it would be easy for me to change it, or subclass it, for my own needs, while preserving its behaviour. (The situation,...
0
1333
by: Mustafa Thamer | last post by:
Hi, I'm using import hooks according to PEP 302, in order to load python files from a game PAK file. The game is C++ using embedded and extended Python (v2.33) and Boost. The importing works fine, but after modules are imported I can't reload them. I've tried 'reload(foo)' and 'PyImport_ReloadModule(pModPtr)', but both return 'ImportError: No module named foo'. Is it safe to assume that reload doesn't respect the import hook? That...
97
4446
by: Kjetil Torgrim Homme | last post by:
often when re-factoring code, I need to change the indent level of some chunk of code. due to the lack of an end marker, my Emacs has to use heuristics when re-indenting, and this will occasionally lead to mistakes. of course this is really _my_ fault, not Emacs', but it's annoying all the same. a colleague uses #fi, #yrt etc. to mark the end of blocks, but I don't find this satisfactory since neither Python nor Emacs has any...
5
2481
by: Steve Holden | last post by:
This is even stranger: it makes it if I import the module a second time: import dbimp as dbimp import sys if __name__ == "__main__": dbimp.install() #k = sys.modules.keys() #k.sort() #for kk in k:
79
5295
by: pinkfloydhomer | last post by:
I want to scan a file byte for byte for occurences of the the four byte pattern 0x00000100. I've tried with this: # start import sys numChars = 0 startCode = 0 count = 0
9
2125
by: rsoh.woodhouse | last post by:
Hi, I'm trying to work out some strange (to me) behaviour that I see when running a python script in two different ways (I've inherited some code that needs to be maintained and integrated with another lump of code). The sample script is: # Sample script, simply create a new thread and run a # regular expression match in it. import re
0
9691
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9551
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10035
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9090
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7580
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5606
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4149
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3764
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2945
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.