Using remote source code
Shane Geiger
sgeiger at ncee.net
Sun Mar 25 03:06:21 EDT 2007
Alex,
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
Python.
"""
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.py', foobar)
# File:ihooks-example-1.py
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/foobar.py")
print foo.x
print foo.x()
print foo.x()
pyapplico at gmail.com wrote:
> On Mar 25, 3:20 pm, a... at mac.com (Alex Martelli) wrote:
>
>> <pyappl... at gmail.com> 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
>> <http://www.python.org/dev/peps/pep-0302/> .
>>
>> Here's a trivial example (bereft of much error checking, etc). I've
>> uploaded tohttp://www.aleax.it/foo.pya 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 = 'http://www.aleax.it/'
>>
>> class Examp(object):
>> names = set([ 'foo', ])
>> def find_module(self, fullname, path=None):
>> if fullname not in self.names: return None
>> self.foo = 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 self.foo.read() 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 foo.foo()
>>
>> 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 ncee.net | 402-438-8958 | http://www.ncee.net
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: <http://mail.python.org/pipermail/python-list/attachments/20070325/c7c14527/attachment.vcf>
More information about the Python-list
mailing list