[Python-Dev] Relative Package Imports

M.-A. Lemburg mal@lemburg.com
Mon, 13 Sep 1999 17:27:31 +0200

Gordon McMillan wrote:
> [Jim Fulton]
> > Here's an example that I hope will be motivating:
> >
> > Suppose Marc-Andre has a package mx with subpackages DateTime
> > and stringtools.
> ...
> > Zope has a notion of products which are *self contained* packages that
> > are sub-packages of the Products package.  So, suppose someone wants
> > to write a NiftyDB product, which is a Zope product that provides
> > access to an external database.  Now the author of the NiftyDB product
> > wants to use the mx package.  The mx package is not a standard part of
> > Zope, or of Python, so they simpley include it in the NiftyDB product
> > directory.
> This would motivate me to strangle the author of NiftyDB. He
> should say "This requires Marc-Andre's mx package", and put error
> handling on the import. Otherwise I might end up with a gazillion
> separate copies of the mx package installed on my system and no
> way to clean it up.

The mx package installed in product dir won't be visible to outside
scripts: it is self-contained and only works in the context of
Zope. There's nothing bad about it if you take the POV of a typical
Zope user who doesn't know about the internals too much.

OTOH, if the user does have the skill to install packages
himself, he could install the mx package top-level and then
leave it out of the NiftyDB product dir. Both setups will work
without any source code change... that's the real advantage.

Currently, only the top-level installation of the mx package
will work and this of course introduces other problems like
version control, e.g. the NiftyDB product (relying on mx 1.9)
may not support version 2.0 of mx which the sysadmin installed
at top-level. There would be no way (apart from patching NiftyDB)
to get back to a working setup.
> Your notion of "self contained packages" is flawed. Reducto ad
> adsurdum, it leads to downloading the OS with every app you
> install. (Crap, I hope I haven't given Sun any new ideas...).

While you do of course have a point in that packages should not
be loaded twice (a smarter import mechanism written in Python
could handle these situations along with the relative import),
you should consider that Zope is an application. It could go the
WinXX way and simply hack around in the OS or wrap everything
it needs up in the package itself. Both have their pros and
cons, both are possible using relative imports (I'm starting to
feel like one of those marketing guys ;).

BTW, I really don't care much about this feature being coded
in Python or C. All I would like is to have it enabled and
available per default, so that writing self-contained packages
becomes a standard in the Python world.

Marc-Andre Lemburg
Y2000:                                                   109 days left
Business:                                      http://www.lemburg.com/
Python Pages:                           http://www.lemburg.com/python/