[Distutils] modules in different folders but under same namespace

Robert Kern robert.kern at gmail.com
Thu Feb 4 18:06:07 CET 2010

On 2010-02-04 10:53 AM, Riccardo-Maria BIANCHI wrote:
> Hi,
> I have a package structured like this:
> package/__init__.py
> src/ __init__.py
> mod1.py
> share/__init__.py
> mod2.py
> Now I can import them as:
> package.src.mod1
> package.share.mod2
> How can I use Distutils to be able to import both of them under the same
> "package" namespace as:
> import package.mod1
> import package.mod2
> ?
> Thanks a lot in advance for your kind help!

Remove the package/src/__init__.py and package/share/__init__.py . In your 
package/__init__.py, append the src/ and share/ directories to the __path__ 
list. E.g.

   import os
   import pkgutil

   for subdir in ['src', 'share']:
       __path__.append(os.path.join(os.path.dirname(__file__), subdir))

Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
  that is made terrible by our own mad attempt to interpret it as though it had
  an underlying truth."
   -- Umberto Eco

More information about the Distutils-SIG mailing list