Skip to main content

Nested Imports for Python

Project description

Nested Imports (Nimp)

Allows nested imports, a la Java. Nimp installs a harmless meta import-hook that adds support for nested packages, i.e., multiple packages that “live” under a common namespace. This is the idiom in Java, where you have packages like and, as well as in Haskell and several other languages. Nimp basically allows packages to “inject” themselves into shared namespaces.

Compatible with Python 2.3-2.7 and 3.0 and up.

You can read more about nimp and __lazy__ on my blog.


import nimp

You can add these two lines to your script, so you’ll get Nimp out of the box.


  • Allows for distributing “plug-in” packages independently: consider a project like twisted, made of several add-on packages (but without hacking distutils too much).
  • A project may depend on package X.Y without having to depend on X.
  • These nested-packages can be found anywhere on your python path (they don’t have to be located in the same directory)
  • A small (less than 100 LoC) standalone module that does all the magic for you – no third-party cooperation required.


distutils has problems with directory names that contain dots (as in, so Nimp supports either dots or hyphens (e.g., com-ibm-storage). This allows you to distribute your package nicely, using commonplace distribution mechanisms.

The recommended way is to use hyphens, but you may continue to use dots, as was the case in previous versions. However, never mix dots and hyphens (e.g., – it will not work.

Example Layout

Consider the following directory structure, somewhere on your python path (say, site-packages):





After calling nimp.install(), the following imports will work as expected:

import com                              # a namespace package (empty)
import                          # a namespace package (empty)
import              # a real package,2,3)

# and of course `from x import y` works too
from import ScsiDisk

# note how the `plugins` package is "injected" into `storage`
from import MySQLPlugin

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for nimp, version 0.9.2
Filename, size File type Python version Upload date Hashes
Filename, size nimp-0.9.2.tar.gz (3.9 kB) File type Source Python version None Upload date Hashes View
Filename, size nimp-0.9.2.win32.exe (202.2 kB) File type Windows Installer Python version any Upload date Hashes View

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring DigiCert DigiCert EV certificate Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page