469,916 Members | 1,953 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,916 developers. It's quick & easy.

Import 'lookup' problem with path_hooks


My application uses a remote import scheme using the sys.path_hooks
solution as explained in PEP302

The importer works fine for packages, modules, modules in packages and
packages in packages, but there's a problem when a module in a package
want's to import a module from the same package.


package spam contains the modules ham and eggs.

__init__.py of spam contains:
import ham
import eggs

ham contains:
import eggs

When I import the package spam locally, everything goes as planned,
but the remote importer wants to import eggs from the root and doesn't
even 'look' in the 'current' package path as oposed to the standard
import method.

Changing the import statement in ham to import spam.eggs works for
this case, but that isn't useable when spam is a package in another
package and including all base packages in the import statement isn't
doable because it prevents the packages to be developed and tested as
a single standalone unit.

This is the remote importer code:

class RemoteImporter:

def __init__(self, path):
# decl
self.path = None

itom = path.split('.')[0]
if not hasattr(self, 'pathlist'):
raise ImportError
if itom not in self.pathlist:
raise ImportError

def find_module(self, fullname, path=None):
params = {}
params['fullname'] = fullname
params['load'] = True
# remote request
result = remoteRequest(params)
return None
if result:
if result['code']:
self.code = result['code']
self.path = result['path']
return self
return None

def load_module(self, fullname):

code,type = Cache.Get(self.path,fullname)
mod = imp.new_module(fullname)
sys.modules[fullname] = mod
mod.__file__ = "<%s>" % self.__class__.__name__
mod.__loader__ = self
mod.__path__ = self.path

if type == imp.PY_COMPILED:
code = marshal.loads(code)
except LoadError, (errCode, errString):
elif type == imp.PY_SOURCE:
raise ImportError
exec code in mod.__dict__
raise ImportError
return mod

Any pointers?

< Remy >
Jul 18 '05 #1
0 1164

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Remy Cool | last post: by
reply views Thread by Remy C. Cool | last post: by
reply views Thread by Noam Raphael | last post: by
4 posts views Thread by Steve Holden | last post: by
2 posts views Thread by Torsten Mohr | last post: by
5 posts views Thread by Steve Holden | last post: by
23 posts views Thread by Shane Hathaway | last post: by
reply views Thread by | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.