问题描述
我有一个大图书馆要拆分.有软件包:hdx.data
hdx.facades
hdx.utilities
I have a large library I want to split up. There are packages: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'
.
I want to move hdx.utilities to a separate project hdx-python-utilities (on PyPi) and then add it as a requirement to the project with the packages hdx.data and hdx.facades (hdx-python-api). The problem is that I get ImportError: No module named 'hdx.utilities'
when doing from hdx.utilities.session import get_session
in the project hdx-python-api.
是否有任何方法可以使它在Python 3+和2.7中都可以工作(在其中两个中都没有重命名顶级包名称hdx),从而允许hdx-python-api和hdx-python-utilities都可以在任何项目中工作会安装它们吗?
Is there any way to make this work in both Python 3+ and 2.7 (without renaming the top level package name hdx in either of them) allowing both hdx-python-api and hdx-python-utilities to work in any project that installs them?
推荐答案
- 本机(Python 3.3)
- pkgutil样式(Python 2和3,与本机兼容)
- pkg_resources样式(与以上版本不兼容,不建议使用,不推荐)
为Python 2和3创建命名空间包的推荐方法是 pkgutil样式的名称空间包:
The recommended way of doing namespaced packages for Python 2 and 3 are 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
文件仅需要包含以下内容:
The two __init__.py
files for the namespace package needs to contain only the following:
__path__ = __import__('pkgutil').extend_path(__path__, __name__)
这篇关于导入具有相同基本软件包名称的软件包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!