Importing User-defined Modules

Steve Holden steve at holdenweb.com
Mon Jul 25 01:51:57 EDT 2005


Walter Brunswick wrote:
> I need to import modules with user-defined file extensions that differ from '.py', and also (if possible) redirect the bytecode 
> output of the file to a file of a user-defined extension.
> I've already read PEP 302 (http://www.python.org/peps/pep-0302.html), but I didn't fully understand it. Would someone [who 
> understands it] please be able to give me a synopsis of it, along with a few explanatory examples, or some other alternatives, if 
> any? 
> 
> 
Well, under the PEP302 regime, as well as all the usual suspects you can 
add to sys.path you can also add "importer" objects. These objects will 
have specific methods (find_module and load_module) called at specific 
times during the import process.

Because there can be a significant amount of time spent initializing an 
importer object the system now uses a cache of importers, which it's 
sensible to clear when adding a new importer.

I attach code below that implements an importer that loads modules from 
a database, plus the program that actually creates the database 
containing the modules. This was only a test of my understanding, but it 
may help you to move further towards PEP302.

regards
  Steve
-- 
Steve Holden       +44 150 684 7255  +1 800 494 3119
Holden Web LLC             http://www.holdenweb.com/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: dbload.py
Type: application/x-python
Size: 4348 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-list/attachments/20050725/ba18a018/attachment.bin>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: dbimp.py
Type: application/x-python
Size: 2104 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-list/attachments/20050725/ba18a018/attachment-0001.bin>


More information about the Python-list mailing list