1

I recently build a Python package and installed it via Debian packager. It is installed on my system so I can use it in other projects. In my new project I have the same name of a package folder, namely, opal. I scanned the web including this site and everything pointed towards using:

from __future__ import absolute_import

Unfortunately, it couldn't resolve the problem, I suspect I am missing a detail.

I am using Python 2.7.3 and here are my project folder structures:

Installed package structure:

- opal
     __init__.py
     core.py

New Project structure:

- opal
  - __init__.py
  - net.py

In the new project I cannot do:

from opal.core import OpalClient

I always get an error that core is not found under opal!!!

If __future__ is the solution, can somebody provide me an example. I tried several ways and always got errors. I found this an awkward problem for such a neat language ;)

Cheers

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
user2449584
  • 73
  • 1
  • 11

1 Answers1

1

Python does not merge namespaces without additional help. You have a opal top-level package in one place that is being found before the other top-level opal package is found. That first package found has opal.net as a contained module, but not the opal.core module.

You'll need to use a setuptools namespace setup to enable this behaviour; install your packages with a setuptools compliant setup.py and let it register and manage namespaces for you.

If you are using Python 3.3 or newer, you can use the new PEP 420 namespace support, where you'd leave the top-level directory (or directories) empty to create a namespace that can then be merged. Also see Namespace packages in the Python 3 import system documentation.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • what is the solution for a debian package? Our users and developers have both options, install the package via apt-get on Linux platforms or the Python way (setuptools). – user2449584 Jun 19 '13 at 15:19
  • I believe that the [Debian Python tools](http://wiki.debian.org/Python/FAQ#Python_eggs) can handle eggs directly. The correct metadata should be present so that the setuptools extensions can find the different `opal` packages and make sure that Python will see them as one top-level namespace. – Martijn Pieters Jun 19 '13 at 15:25
  • In Python 3.3 and later you can make namespaces without setuptools. https://www.python.org/dev/peps/pep-0420 But then you can't use Python 3.2 or more importantly Python 2. – Lennart Regebro Dec 05 '14 at 12:18
  • 1
    @LennartRegebro: I was in the process of writing something about that already. :-) – Martijn Pieters Dec 05 '14 at 12:21