导入具有相同基本包名称的包
Importing package with same base package name
我有一个很大的图书馆,我想分开。有套餐:
hdx.data
hdx.facades
hdx.utilities
我想将 hdx.utilities 移动到一个单独的项目 hdx-python-utilities(在 PyPi 上),然后将其作为要求添加到包含包 hdx.data 和hdx.facades (hdx-python-api)。问题是我在项目 hdx-python-api.
中执行 from hdx.utilities.session import get_session
时得到 ImportError: No module named 'hdx.utilities'
有没有什么方法可以在 Python 3+ 和 2.7 中使用它(无需在其中任何一个中重命名顶级包名称 hdx)同时允许 hdx-python-api 和 hdx-python-实用程序在任何安装它们的项目中工作?
There are three ways of doing namespaced packages:
- 原生 (Python 3.3)
- pkgutil 样式(Python 2 和 3,与本机兼容)
- pkg_resources-style(与上述不兼容,弃用,不推荐)
为 Python 2 和 3 命名空间包的推荐方法是 pkgutil-style namespace packages:
您将为 hpx-python-api
创建以下内容
setup.py
hpx/
__init__.py # namespace init, see content below
data/
__init__.py
...
facades/
__init__.py
...
和以下 hpx-python-utilities
setup.py
hpx/
__init__.py # namespace init, see content below
utilities/
__init__.py
...
命名空间包的两个 __init__.py
文件只需要包含以下内容:
__path__ = __import__('pkgutil').extend_path(__path__, __name__)
我有一个很大的图书馆,我想分开。有套餐:
hdx.data
hdx.facades
hdx.utilities
我想将 hdx.utilities 移动到一个单独的项目 hdx-python-utilities(在 PyPi 上),然后将其作为要求添加到包含包 hdx.data 和hdx.facades (hdx-python-api)。问题是我在项目 hdx-python-api.
中执行from hdx.utilities.session import get_session
时得到 ImportError: No module named 'hdx.utilities'
有没有什么方法可以在 Python 3+ 和 2.7 中使用它(无需在其中任何一个中重命名顶级包名称 hdx)同时允许 hdx-python-api 和 hdx-python-实用程序在任何安装它们的项目中工作?
There are three ways of doing namespaced packages:
- 原生 (Python 3.3)
- pkgutil 样式(Python 2 和 3,与本机兼容)
- pkg_resources-style(与上述不兼容,弃用,不推荐)
为 Python 2 和 3 命名空间包的推荐方法是 pkgutil-style namespace packages:
您将为 hpx-python-api
setup.py
hpx/
__init__.py # namespace init, see content below
data/
__init__.py
...
facades/
__init__.py
...
和以下 hpx-python-utilities
setup.py
hpx/
__init__.py # namespace init, see content below
utilities/
__init__.py
...
命名空间包的两个 __init__.py
文件只需要包含以下内容:
__path__ = __import__('pkgutil').extend_path(__path__, __name__)