Hello,
My application uses a remote import scheme using the sys.path_hooks
solution as explained in PEP302
(http://www.python.org/peps/pep-0302.html)
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.
Example:
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
self.pathlist.append('RemoteImporter')
if itom not in self.pathlist:
raise ImportError
def find_module(self, fullname, path=None):
params = {}
params['fullname'] = fullname
params['load'] = True
try:
# remote request
result = remoteRequest(params)
except:
return None
if result:
if result['code']:
self.code = result['code']
self.path = result['path']
Cache.Add(self.path,fullname,result['extension'],self.code)
return self
else:
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:
try:
code = marshal.loads(code)
except LoadError, (errCode, errString):
pass
except:
pass
elif type == imp.PY_SOURCE:
pass
else:
raise ImportError
try:
exec code in mod.__dict__
except:
raise ImportError
return mod
Any pointers?
< Remy >