pythonimportnamespace-package

Import problems with Python namespace packages


I am trying to use Python namespace packages concept to split my library across multiple directories. In general, it works, but I have a problem regarding importing names to projects package level.

My project structure is following:

Example project structure

project1/coollibrary/__init__.py

from __future__ import absolute_import

from pkgutil import extend_path
__path__ = extend_path(__path__, __name__)

from .foomodule import foo

project1/coollibrary/foomodule.py

def foo():
    print ('foo')

project2/coollibrary/__init__.py

from __future__ import absolute_import

from pkgutil import extend_path
__path__ = extend_path(__path__, __name__)

from .barmodule import bar

project2/coollibrary/barmodule.py

def bar():
    print ('bar')

Both projects are in the PATH:

$ echo ${PYTHONPATH}
/home/timo/Desktop/example/project1:/home/timo/Desktop/example/project2

And I am running code from here:

$ pwd
/home/timo/Desktop/example
$ python3
>>> import coollibrary
>>> coollibrary.foo() # works
foo
>>> coollibrary.bar() # does not work (the problem)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'bar'
>>> import coollibrary.barmodule 
>>> coollibrary.barmodule.bar() # works
bar

How to fix the code so that I could import both foo and bar directly from coollibrary package. Also, is there a solution that works for both Python2.7 and Python3.4 (other versions not required).


Solution

  • Starting with Python 3.3, you can use PEP 420 -- Implicit Namespace Packages.

    Basically, you would remove your __init__.py file in both repositories, and add:

    setup(
        ...
        packages=['coollibrary.{foomodule/barmodule}'],
        namespace_packages=['coollibrary'],
        ...
    )
    

    to your setup.py.

    Can't help you with Python 2.7 though...