Reputation: 1207
How is it possible build multiple python modules sharing the same namespace compatible for Python 2.7+ and 3.3+?
Let's call the namespace test
. Now I want to have two seperate modules called test.foo
and another one called test.bar
. However, I'm currently developing test.helloworld
which depends on both, test.foo
and test.bar
. Both are listed in the requirements.txt
file.
The modules test.foo
and test.bar
are currently using the Python 2 solution for namespace packages:
import pkg_resources
pkg_resources.declare_namespace(__name__)
Running the suggested pip-command for development mode pip install -e .
turns into: ImportError: No module named 'test.helloworld'
while importing test.foo
or test.bar
is working.
The Python 3 solution for namespace packages are Implicit Namespace Packages where the namespace package has no __init__.py
file. This is sadly not working for Python 2 versions.
How can I design a solution for both Python 2 and 3 (which allows me to use pip install -e .
)? The --egg
solution does not work for me since it is already deprecated.
Upvotes: 24
Views: 2586
Reputation: 19416
You'd want to use pkgutil-style namespace packages.
From https://packaging.python.org/guides/packaging-namespace-packages/:
pkgutil-style namespace packages
Python 2.3 introduced the pkgutil module and the extend_path function. This can be used to declare namespace packages that need to be compatible with both Python 2.3+ and Python 3. This is the recommended approach for the highest level of compatibility.
A table listing out all the possible ways of dealing with namespace packages, and which ways would work together: https://github.com/pypa/sample-namespace-packages/blob/master/table.md
Upvotes: 1
Reputation: 11
I recently had a similar issue, where I had to install a package for Python 2 and 3. I ended up having to download the code from GitHub, then ran the setup.py by calling
sudo python setup.py install
and
sudo python3 setup.py install
This results in the package being installed for both Python 2 and 3, even though the code itself was meant for Python 2. This allows me to work with the package whether I use Python 2 or 3, without any namespace conflicts.
Upvotes: 1
Reputation: 5270
See the answer at similar question for complete instructions which works on both python 2 and 3.
In short, setup.py
needs to have unique name for each module and a common namespace_packages
definition in addition to __init__.py
declaring the namespace set at namespace_packages
.
If you are still having issues, please post your setup.py
and __init__.py
for each module.
Upvotes: 0