[Python-Dev] Relative Package Imports

Jim Fulton jim@digicool.com
Mon, 13 Sep 1999 11:21:15 -0400


Gordon McMillan wrote:
> 
> [relative package imports]
> [JimF]
> > > I'll second Marc-Andre here.
> > >
> > > A significant headache occurs when you have a package
> > > that has sub-packages.  Sub-packages need to be able to
> > > reference other sub-packages within the same package without
> > > knowing where the containing package is installed.
> [GvR]
> > You never need to know where it is installed.  When I said absolute
> > package name I meant package name (e.g. zope.foo.bar.subpack) not
> > filename.  As Tim has argued, the ability to change the name of the
> > toplevel here is a liability, not a feature.
> 
> In between. I can see relative packages as *one* way of handling
> certain problems. Consider:
> 
>  zope.Win32
>  zope.Unix
> 
> with both of these having alternate implementations of  subpackages foo
> and bar. Then for (the current) foo.a to get to (the  current) bar.b,
> using a relative import seems a natural.

I wouldn't use relative imports for this.
 
> This can, of course, be done in pure Python. So can doing things  in
> zope.__init__.py that make the appropriate implementations of  foo and
> bar appear to be zope.foo and zope.bar. On any criteria I  can think of,
> this would be a superior solution. (*)

I agree. This is not a good motivating example for relative imports.
 
> What I am against is further complicating the already over  complicated
> built in import mechanism.

The current scheme doesn't seem all that complicated to me.
I wouldn't be in favor of making it more complicated if there wasn't
a good reason.  I think that, in working on the Zope framework, 
I've found some pretty good reasons for relative imports.


> 
> (*) such as a zope.__init__.py that looks like this:
> 
> import sys
> if sys.platform[:3] == 'win':
>   nm = __name__ + '.Win32'
> else:
>   nm = __name__ + '.Unix'
> new = __import__(nm)
> sys.modules[__name__] = sys.modules[nm]

Right, this would be silly. This is not a good example.

Jim

--
Jim Fulton           mailto:jim@digicool.com   Python Powered!        
Technical Director   (888) 344-4332            http://www.python.org  
Digital Creations    http://www.digicool.com   http://www.zope.org    

Under US Code Title 47, Sec.227(b)(1)(C), Sec.227(a)(2)(B) This email
address may not be added to any commercial mail list with out my
permission.  Violation of my privacy with advertising or SPAM will
result in a suit for a MINIMUM of $500 damages/incident, $1500 for
repeats.