如何构建多个 python 模块共享相同的命名空间,兼容 Python 2.7+ 和 3.3+?
让我们调用命名空间test
。现在我想要有两个单独的模块,称为 test.foo
和另一个称为 test.bar
。但是,我目前正在开发 test.helloworld
,它依赖于 test.foo
和 test.bar
。两者都列在 requirements.txt
文件中。
模块 test.foo
和 test.bar
当前使用 Python 2 solution对于命名空间包:
import pkg_resources
pkg_resources.declare_namespace(__name__)
运行 suggested pip-command for development mode pip install -e .
变成:ImportError: No module named 'test.helloworld'
while importing test.foo
or test.条形
正在工作。
Python 3 solution对于命名空间包是 Implicit Namespace Packages,其中命名空间包没有 __init__.py
文件。遗憾的是,这不适用于 Python 2 版本。
如何为 Python 2 和 3 设计解决方案(允许我使用 pip install -e .
)? --egg
解决方案对我不起作用,因为它已被弃用。
最佳答案
我最近遇到了类似的问题,我必须为 Python 2 和 3 安装一个包。我最终不得不从 GitHub 下载代码,然后通过调用运行 setup.py
sudo python setup.py install
和
sudo python3 setup.py install
这会导致为 Python 2 和 3 安装包,即使代码本身是为 Python 2 设计的。这使我无论使用 Python 2 还是 3 都可以使用该包,而不会出现任何命名空间冲突。
关于Python 2 & 3 兼容的命名空间模块(使用 pip),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31915992/