versioning schemes in package imports (was Re: [Import-sig] imputil)

M.-A. Lemburg mal@lemburg.com
Thu, 15 Mar 2001 18:35:01 +0100


Peter Funk wrote:
> 
> Hi,
> 
> sorry for jumping in on this thread that late.
> 
> > Michel Sanner wrote:
> > > I am trying to come up with a way to handle versions of packages
> [...]
> > > package structure:
> > > A/
> > >         A_1_0/
> > >         A_1_1/
> > >         A_1_2/
> [...]
> > > >>> A.importer.set_version("1_0")
> > > >>> from A import B # should import A.A_1_0.B
> [...]
> 
> Gordon McMillan answered:
> > Actually, in this case you don't really *need* imputil.
> [...]
> 
> What I want to add is: The package Pmw (Python Mega Widgets,
> see <http://Pmw.sourceforge.net/>.) written by Greg McFarlane
> <mailto:gregm@iname.com> contains a versioning mechanism comparable
> to what Michel Saner seems to want.
> 
> BTW: Pmw is a Python extension very useful for everybody interested
> in programming X-platform portable GUIs.
> 
> After 'import Pmw' the Pmw/__init__.py file replaces the Pmw module with
> an instance of a very interesting class 'PmwLoader', which fullfills
> two purposes:
> 
> 1. Select a specific version of Pmw from a subdirectory called
>    for example Pmw_0_8_3 or Pmw_0_8_4 or so.
> 2. Implement a "lazy importer" which delays actual importing of
>    submodules contained in the package until a feature contained
>    in it is actually used by the application which increases startup
>    performance.
> 
> I personally never understood, why people bother with versioning
> schemes and focussed on the second idea, which I found very smart.
> It was easy for me to rip the versioning stuff out of the PmwLoader
> class and to build a my own simplified lazy importer class for use
> in my own packages.

You may want to have a look at mx.Misc.LazyModule in the
egenix-mx-base package. It is used by most mx subpackages
to enable submodule/-package load without additional import.

Note that is doesn't try to manipulate sys.modules, but instead
works in the namespace defining the lazy module.

> May be Michel could use the same approach to implement his own
> loader class doing this kind of version selection, he outlined
> above?  The basic idea to do all such things is outlined in the
> last three lines of Pmw/__init__.py which I quote here:
> 
>   # Create the dynamic loader and install it into sys.modules.
>   sys.modules['_Pmw'] = sys.modules['Pmw']
>   sys.modules['Pmw'] = _mod.PmwLoader(_dir, _instdirs, _listdir)
> 
> But this approach (replacing the module in sys.modules with a class
> instance after import) has some disadvantages: For example: The
> new documentation tool pydoc.py written by Ping (now in the standard
> library) breaks down when encountering such a "module", because it
> has no __file__ attribute.
> 
> Regards, Peter
> --
> Peter Funk, Oldenburger Str.86, D-27777 Ganderkesee, Germany, Fax:+49 4222950260
> office: +49 421 20419-0 (ArtCom GmbH, Grazer Str.8, D-28359 Bremen)
> 
> _______________________________________________
> Import-sig mailing list
> Import-sig@python.org
> http://mail.python.org/mailman/listinfo/import-sig

-- 
Marc-Andre Lemburg
______________________________________________________________________
Company & Consulting:                           http://www.egenix.com/
Python Pages:                           http://www.lemburg.com/python/