Using remote source code

Shane Geiger sgeiger at
Sun Mar 25 09:06:21 CEST 2007


I see that you aren't using ihooks.  Below is an example I found that 
uses ihooks.  I think it would be worth comparing and contrasting both 
approaches (though I am not familar enough with this aspect of Python to 
do so).  IIRC, this code addresses some path related issues of other 
import-from-file methods.

Note: This might not work from within ipython, but it works from within 


The ihooks module
This module provides a framework for import replacements. The idea is to 
allow several alternate
import mechanisms to co-exist.

Example: Using the ihooks module

import os
def writefile(f, data, perms=750): open(f, 'w').write(data) and 
os.chmod(f, perms)

foobar = """
print "this is from the foobar module"

def x():
    print "This is the x function."


writefile('/tmp/', foobar)

import ihooks, imp, os, sys
def import_from(filename):
    "Import module from a named file"
    if not os.path.exists(filename):
        sys.stderr.write( "WARNING: Cannot import file." )
    loader = ihooks.BasicModuleLoader()
    path, file = os.path.split(filename)
    name, ext = os.path.splitext(file)
    m = loader.find_module_in_dir(name, path)
    if not m:
        raise ImportError, name
    m = loader.load_module(name, m)
    return m

foo = import_from("/tmp/")

print foo.x
print foo.x()
print foo.x()

pyapplico at wrote:
> On Mar 25, 3:20 pm, a... at (Alex Martelli) wrote:
>> <pyappl... at> wrote:
>>> Is there any possible way that I can place a .py file on the internet,
>>> and use that source code in an .py file on my computer?
>> You can write an import hook in any way you like; see
>> <> .
>> Here's a trivial example (bereft of much error checking, etc).  I've
>> uploaded to toy module w/contents:
>> def foo(): return 'foo'
>> Here's a tiny program to import said module from my site:
>> import urllib2, sys, new
>> theurl = ''
>> class Examp(object):
>>     names = set([ 'foo', ])
>>     def find_module(self, fullname, path=None):
>>         if fullname not in self.names: return None
>> = urllib2.urlopen(theurl+fullname+'.py')
>>         return self
>>     def load_module(self, fullname):
>>         module = sys.modules.setdefault(fullname,
>>                                           new.module(fullname))
>>         module.__file__ = fullname
>>         module.__loader__ = self
>>         exec in module.__dict__
>>         return module
>> def hooker(pathitem):
>>     print 'hooker %r' % pathitem
>>     if pathitem.startswith(theurl): return Examp()
>>     raise ImportError
>> sys.path_hooks.append(hooker)
>> sys.path.append(theurl)
>> import foo
>> print
>> Alex
> Thanks for your help, now I can continue building my source code
> generator. :)

Shane Geiger
IT Director
National Council on Economic Education
sgeiger at  |  402-438-8958  |

Leading the Campaign for Economic and Financial Literacy

-------------- next part --------------
A non-text attachment was scrubbed...
Name: sgeiger.vcf
Type: text/x-vcard
Size: 310 bytes
Desc: not available
URL: <>

More information about the Python-list mailing list