【发布时间】:2023-03-14 19:58:02
【问题描述】:
如何构建多个 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' 同时导入test.foo 或test.bar 正在工作。
命名空间包的Python 3 solution 是隐式命名空间包,其中命名空间包没有__init__.py 文件。遗憾的是,这不适用于 Python 2 版本。
如何为 Python 2 和 3 设计解决方案(允许我使用pip install -e .)? --egg 解决方案对我不起作用,因为它已被弃用。
【问题讨论】:
-
这应该也能解决您的问题stackoverflow.com/a/7075121/681481
标签: python python-2.7 python-3.x pip setup.py