[ Python import conflict ]
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
Answer 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.