473,387 Members | 1,899 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Import 'lookup' problem with path_hooks

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 >
Jul 18 '05 #1
0 1296

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Remy Cool | last post by:
Hello, I've created an archive import class (with the help of importers.py from the python /demo/imputil dir) which imports packages and modules from a dictionary. So far so good. The problem...
0
by: Remy C. Cool | last post by:
Hello, A couple of months ago, I posted a question related to the same problem, but didn't get any replies. I've created a ugly workaround but still would like to get some insight on the matter....
0
by: Noam Raphael | last post by:
Hello, Do you know of a reference implementation of an import hook? I mean a class, written in Python, that imitates Python's default module loading behaviour. I mean that I would be able to...
4
by: Steve Holden | last post by:
I'm trying to load module code from a database, which stores for each module its full name, code, load date and a Boolean indicating whether it's a package or not. The following simple program:...
2
by: Torsten Mohr | last post by:
Hi, is there some description available to overwrite the import hook? By googling i found out so far that i need to overwrite __builtins__.__import__ with something else. Can i also do this...
5
by: Steve Holden | last post by:
This is even stranger: it makes it if I import the module a second time: import dbimp as dbimp import sys if __name__ == "__main__": dbimp.install() #k = sys.modules.keys() #k.sort() #for...
20
by: Steve Jorgensen | last post by:
Hi all, I've just finished almost all of what has turned out to be a real bear of a project. It has to import data from a monthly spreadsheet export from another program, and convert that into...
23
by: Shane Hathaway | last post by:
Here's a heretical idea. I'd like a way to import modules at the point where I need the functionality, rather than remember to import ahead of time. This might eliminate a step in my coding...
0
by: | last post by:
--- Patrick Stinson <patrickkidd.lists@gmail.com> escribió: Yes, it's different, meta_path is a generic mechanism that doesn't depend on sys.path and is tried before sys.path is traversed;...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.