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()




[EMAIL PROTECTED] wrote:
On Mar 25, 3:20 pm, [EMAIL PROTECTED] (Alex Martelli) wrote:
<[EMAIL PROTECTED]> 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
[EMAIL PROTECTED]  |  402-438-8958  |  http://www.ncee.net

Leading the Campaign for Economic and Financial Literacy

begin:vcard
fn:Shane Geiger
n:Geiger;Shane
org:National Council on Economic Education (NCEE)
adr:Suite 215;;201 N. 8th Street;Lincoln;NE;68508;United States
email;internet:[EMAIL PROTECTED]
title:IT Director
tel;work:402-438-8958
x-mozilla-html:FALSE
url:http://www.ncee.net
version:2.1
end:vcard

-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to