打包常见的Python命名空间
我希望在接下来的几天内将我必须的库打包并上传到 PyPI,但我对命名空间的方法有点不确定。
我有一些其他不相关的项目,采用类似的方法,并希望为它们提供相同的命名空间。例如:
- 库 1 命名空间:
abc.seo
- 库 2 命名空间:
abc.ajax
- 库 3 命名空间:
abc.ecommerce
- 等
问题在于我不确定两个单独的包(例如鸡蛋)是否可以与相同的父命名空间共存。这种方法有问题吗,或者有办法解决吗?最好的方法是什么?
这些库不应该打包在一起,它们太不相关了。我想在上传之前做好准备,以避免在发布“正式”版本后痛苦的名称空间更改。
(注意abc
不是真实姓名,我希望我的问题没有广告)
更新
我选择了以下内容,为了友善对于没有安装 setuptools 的人:
try:
__import__('pkg_resources').declare_namespace(__name__)
except ImportError:
__path__ = __import__('pkgutil').extend_path(__path__, __name__)
在 setup.py
中添加以下内容:
setup(
...
namespace_packages = ['rollyourown'],
...
I'm looking to package and upload a library I have to PyPI in the next few days, but I'm a little unsure about my approach to the namespace.
I have a few otherwise unrelated projects with a similar approach and wanted to give them all the same namespace. For example:
- Library 1 namespace:
abc.seo
- Library 2 namespace:
abc.ajax
- Library 3 namespace:
abc.ecommerce
- etc
The problem is that I'm not sure if it's possible for two separate packages (eg eggs) to co-exist with the same parent namespace. Is this approach problematic, or is there a way around it? What's the best approach?
The libraries should not be packaged together, they are too unrelated. I would like to get it right before uploading so as to avoid painful namespace changes after making an "official" release.
(NB abc
is not the real name, I wanted my question to be free from advertising)
UPDATE
I went with the following, to be nice to the people without setuptools installed:
try:
__import__('pkg_resources').declare_namespace(__name__)
except ImportError:
__path__ = __import__('pkgutil').extend_path(__path__, __name__)
With the following in setup.py
:
setup(
...
namespace_packages = ['rollyourown'],
...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在每个项目基目录中,创建以下结构:
/abc/__init__.py
contains :setup.py
contains :参考文档:命名空间包。
In each project base directory, create the following structure:
/abc/__init__.py
contains :setup.py
contains :Reference documentation: namespace packages.