[Zope3-dev] Importing

Jim Fulton jim@zope.com
Tue, 11 Dec 2001 13:23:00 -0500


Chris Withers wrote:
> 
> Jim Fulton wrote:
> >
> > > +1.  Suitably obscure and clear at the same time.
> >
> > Do you think that the name abreviation is useful?
> 
> Hmmm... Guido and Jim in one thread vaguely to do with importing, I might as
> well toss this in...
> 
> If you have a file structure like:
> 
> /something/__init__.py
> /something/somemodule.py
> /something/abit/__init__.py
> /something/abit/somemodule2.py
> 
> ...you'd import the package like:
> 
> import something
> 
> BUT, how would you import a method/class in somemodule.py _from_ somemodule2.py?

You have to say something like:

from something.somemodule import someclass

> I found the zope notion of a global name 'Products' very useful for this, and I
> could only do the same in a non-Zope environment doing the evillest of hacks,
> along the lines of:
> 
> something_package = string.join(string.split(__name__,'.')[:-2],'.')
> exec "import %s.abit" % something_package
> 
> Now, how should I be doing this? (and in Zope 3?)

Is the point that in 'abit', you don't want to know the name of 
the package(s) that contain it? If so, you can't get there
from here. There's no way to import from a containing package
without knowing it's name. This limitation is controversal. :)

Jim

--
Jim Fulton           mailto:jim@zope.com       Python Powered!        
CTO                  (888) 344-4332            http://www.python.org  
Zope Corporation     http://www.zope.com       http://www.zope.org