Nimp: Nested Imports (a la Java)

Stefan Behnel stefan_ml at behnel.de
Thu Jun 16 16:17:39 EDT 2011


Tomer Filiba, 16.06.2011 10:48:
> Nimp (Nested Imports) is a little meta-importer hook for Python 2.3-2.7 and 3.0-3.2 that enables the use of *nested packages* (`com.ibm.foo.bar`), as is commonplace in Java and various other languages. It works by collecting all packages that begin with a common prefix on your `sys.path` and "merging" them into logical packages.
>
> Homepage: http://pypi.python.org/pypi/nimp
> Install: pip install nimp
>
> Example
> -------
> Consider the following package layout (say, under site-packages, or anywhere on your python path):
>
>    site-packages/
>      com-ibm-storage/
>        ... package files ...
>      com-ibm-storage-plugins/
>        ... package files ...
>      com-google-protobuf/
>        ... package files ...
>      com-google-appengine/
>        ... package files ...
>
> Using Nimp is easy:
>
>    import nimp
>    nimp.install()
>
> You can place these two lines in your `site.py`; after calling `nimp.install()`, the following imports will "magically" work:
>
>    import com # a namespace package (empty)
>    import com.google.protobuf
>    import com.ibm.storage
>    from com.ibm.storage.plugins import MySQLPlugin

So, this isn't really about "nested imports" but rather about merging 
distinct packages, right? This allows me to let packages that are stored in 
different places appear within their common package prefix.

Does is really require the "-" naming convention for packages, or would it 
also work with normal directories? E.g.

    ...path1/org/humbug/test/...
    ...path2/org/humbug/toast/...

That seems like a more common use case: different install directories, egg 
directories and zip files that contain the same prefix package directories.

I guess there are issues with "__init__.py" files in this case, though - 
which ones should be executed if the package structure is multiplied?

Stefan




More information about the Python-list mailing list