python的命名空间_Python3中的Python命名空间包

参加聚会很晚,但在Python中帮助其他旅行者沿着名称空间的路径前进,这一点也不难!在

#1号文件:With the init.py, which of these should I be using (if any)?:

视情况而定,有三种方法可以完成所列的名称空间包here:Use native namespace packages. This type of namespace package is defined in PEP 420 and is available in Python 3.3 and later. This is recommended if packages in your namespace only ever need to support Python 3 and installation via pip.

Use pkgutil-style namespace packages. This is recommended for new packages that need to support Python 2 and 3 and installation via both pip and python setup.py install.

Use pkg_resources-style namespace packages. This method is recommended if you need compatibility with packages already using this method or if your package needs to be zip-safe.

如果使用的是#2(pkgutil-style)或#3(pkg_resources-style),则必须对__init__.py文件使用相应的样式。如果使用本机命名空间,则命名空间目录中没有__init__.py。在

#第二章:With setup.py, do I still need to add the namespace_modules parameter, and if so, would I use namespace_modules=['org.common'], or namespace_modules=['org', 'common']?

{{cd6>的名称空间不是你选择的。在

#三:Could I forgo all of the above by just implementing this differently somehow? Perhaps something simpler or more "pythonic"?

由于您最终在python中结束了一个复杂的主题,似乎您知道自己在做什么,您想要什么,并确定了创建python名称空间包是实现这一目标的方法。这将被认为是一种解决问题的Python式方法。在

除了你的问题,我还发现了以下几点:

我阅读了PEP420,Python Packaging guide,并花了大量时间了解命名空间包,并且大体上理解了它是如何工作的。我通读了几个答案here,here,here,还有这个关于StackOverflow的答案。这里和Rob共享的Gitlink上的示例。在

但是,我的问题是在我创建包之后。由于所有指令和示例代码都在setuptools.setup(package=[])函数中显式列出了包,因此我的代码失败了。我的子包/目录不包括在内。深入研究后,我发现setuptools还有一个find_namespace_package()函数,可以帮助添加子包

编辑:

编辑(2019年9月8日):

为了完成答案,让我用一个例子来重组。在

下面的解决方案假设Python3.3+支持隐式命名空间包

既然您正在寻找Python版本3.5或更高版本的解决方案,那么让我们以所提供的代码示例为例进行进一步阐述。在

假设如下:

命名空间/Python包名称:org

分发包:org_client,org_common

Python:3.3+

设置工具:40.1.0

你要做的是from org.client.client1 import mod1

from org.common import config

并保持你的顶级目录不变,即。org_client_client1_mod1和org_common_config,您可以将结构更改为以下内容

存储库1:

^{pr2}$

更新setup.pyfrom setuptools import find_namespace_packages, setup

setup(

name="org_client",

...

packages=find_namespace_packages(), # Follows similar lookup as find_packages()

...

)

存储库2:org_common_config/

setup.py

org/

common/

__init__.py

config/

__init__.py

someotherfile.py

更新setup.py:from setuptools import find_namespace_packages, setup

setup(

name="org_common",

...

packages=find_namespace_packages(), # Follows similar lookup as find_packages()

...

)

安装(使用pip):(venv) $ pip3 install org_common_config/

(venv) $ pip3 install org_client_client1_mod1/

更新后的pip列表将显示以下内容:(venv) $ pip3 list

...

org_client

org_common

...

但是它们是不可导入的,因为导入必须遵循org.client和{}符号。在

要了解原因,可以在此处浏览(假设在venv内部):(venv) $ cd venv/lib/python3.5/site-packages/

(venv) $ ls -l | grep org

您将看到没有org_client或{}目录,它们被解释为一个名称空间包。在(venv) $ cd venv/lib/python3.5/site-packages/org/

(venv) $ ls -l

client/

common/

...

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值