是否可以使用两个具有相同名称的Python包? [英] Is it possible to use two Python packages with the same name?

查看:128
本文介绍了是否可以使用两个具有相同名称的Python包?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对进口有疑问。这个问题似乎有点人为,但其目的是探讨在包中使用绝对导入对所有导入的限制。 PEP8 强烈反对相对导入(编辑:和 Google Python样式指南说永远不要使用它们。

I have a question about imports. The question might seem a bit contrived, but its purpose is to explore the limitations of using absolute imports for all imports in a package. PEP8 highly discourages relative imports (edit: and the Google Python Style Guide says never to use them).

假设您有两个大型包装,它们都具有相同的名称,并且都按照PEP8使用绝对进口:

Say you are given two large-ish packages that both have the same name and that both use absolute imports in accordance with PEP8:


    /pkg1
        mod1.py (contains an absolute import: 'import pkg1.mod2')
        mod2.py
        ...

    /pkg1
        mod1.py (contains an absolute import: 'import pkg1.mod3')
        mod3.py
        ...

还要说你正在研究一个你想要使用这两个软件包的Python项目。例如,如果你想在一个项目中使用相同包的两个版本,就会出现这种情况。

Also say you are working on a Python project in which you'd like to use both packages. This could come up, say, if you want to use two versions of the same package in a project.

有没有办法将两个包合并到你的项目层次结构中所以您可以在整个项目中自由使用两个软件包中的模块吗?

Is there a way to incorporate both packages into your project hierarchy so that you can freely use modules from both packages throughout your project?

对于解决方案,可以使用导入别名和临时修改sys.path这样的事情。但是改变任一包目录的内容都不行。

For the solution it's acceptable to do things like use import aliases and modify sys.path temporarily. But it's not okay to change the contents of either package directory.

推荐答案

简短的回答是否定的,Python不接受两个具有相同名称的包。 (有些名为命名空间包的东西允许在多个目录上实现单个包,但它们需要将所涉及的包设置为相互合作)。

The short answer is no, Python doesn't accept two packages with the same name. (There are things called "namespace packages" that let a single package be implemented over multiple directories but they require the packages involved to be set up to cooperate with each other).

PEP 8阻止显式相对导入的方式是其更值得怀疑的建议之一,正是因为它使重命名包以避免命名冲突变得更加困难。如果这两个包使用相对导入,你可以重命名其中一个或将它嵌套在另一个包中并完成它。

The way PEP 8 discourages explicit relative imports is one of its more questionable pieces of advice, precisely because it makes it harder to rename a package to avoid naming conflicts. If the two packages used relative imports, you could just rename one of them or nest it inside another package and be done with it.

导入别名对你没有帮助,因为它最终在 sys.modules 中的名称,并且使用模块的名称作为导入,而不是最终被绑定的名称导入模块。

import aliases won't help you here, because it is the name that ends up in sys.modules that matters, and that uses the name of the module as imported, rather than the name that ends up being bound in the importing module.

如果你想变得异国情调,你可以编写自己的导入器(参见PEP 302和 3.x importlib 文档)。如果你决定走那么远,你可以做任何你想做的事情。

If you wanted to get really exotic, you could write your own importer (see PEP 302 and the 3.x importlib documentation). If you decide to go that far, you can do pretty much anything you want.

这篇关于是否可以使用两个具有相同名称的Python包?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆