importing over network

Cliff Wells clifford.wells at comcast.net
Fri Oct 15 08:50:03 EDT 2004


On Fri, 2004-10-15 at 13:40 +0200, Boštjan Jerko wrote:
> Is there a possibility to do import over the network?
> I would like to put all my modules on a server and import them over the 
> network (internet or LAN).

You could use something like this (assuming the remote module is named
'foo.py' and has a function named 'foo'):

import sys, urllib, imp

remoteModules = {
    'foo': 'http://www.IBetterKnowThisURLIsSecure.com/foo.py',
    }

for name, url in remoteModules.items():
    code = urllib.urlopen(url).read()
    module = imp.new_module(name)
    exec code in module.__dict__
    globals()[name] = module

foo.foo()

This is pretty limited (doesn't support packages for one thing, and I'm
sure there's probably more restrictions).  I'd personally take a look at
the ihooks module and see if you can use that instead.

You might also use some library that can pull down entire web sites
(such as HarvestMan) to pull the modules or packages locally and then
just use __import__ to import them.  This would also give you some
caching abilities.

Regards,
Cliff

-- 
Cliff Wells <clifford.wells at comcast.net>




More information about the Python-list mailing list