简体   繁体   English

为什么某些 Python 包名称与其导入名称不同?

[英]Why are some Python package names different than their import name?

Some packages are imported with a string which is different from the name of the package on PyPI, eg:一些包是使用与 PyPI 上包的名称不同的字符串导入的,例如:

$ pip list | grep -i "yaml\|qt"  
PyYAML      3.13               
QtPy        1.5.2
  • pyyaml (pip instal pyyaml), but import yaml pyyaml (pip install pyyaml),但导入 yaml
  • qtpy (pip install qtpy), yes import is qtpy but package is QtPy qtpy (pip install qtpy),是的,导入是 qtpy 但包是 QtPy

Several tools can't not handle that, eg sphinx:一些工具无法处理,例如 sphinx:

$ make html
WARNING: autodoc: failed to import module 'wireshark' from module 'logcollector.plugins'; the following exception was raised:
No module named 'qtpy'

I don't remember it right now, but same is for tools which scan the requirements.txt file and print warnings that the yaml package isn't installed (but it is and its name is pyyaml).我现在不记得了,但同样适用于扫描requirements.txt文件并打印未安装yaml包的警告(但它是,它的名称是 pyyaml)。

Because these two concepts are not really related.因为这两个概念并没有真正的关联。
One is a python concept of package/module names, the other one a package manager concept.一个是包/模块名称的python概念,另一个是包管理器概念。

Look at a simple packaging command with zip:看一个带 zip 的简单打包命令:

zip -r MyCoolTool.zip tool.py

The Tool is named tool , which probably is not unique and if you do not know that its MyCoolTool you do not know which tool it is.该工具名为tool ,这可能不是唯一的,如果您不知道它的 MyCoolTool,您就不会知道它是哪个tool When I upload it somewhere I name it MyCoolTool , so you now a more unique name, that may be a bit more descriptive.当我将它上传到某个地方时,我将其命名为MyCoolTool ,因此您现在使用了一个更独特的名称,这可能更具描述性。

The other point is, that a pip package may include more modules than just one.另一点是,一个 pip 包可能包含更多的模块,而不仅仅是一个。 PyYAML could for example include a second python module yaml2xml in addtion to yaml .例如, PyYAML可以在yaml包含第二个 python 模块yaml2xml

Finally there can be several implementations.最后可以有多种实现方式。 PyYAML sounds like a pure python implementation. PyYAML听起来像一个纯 python 实现。 Now assume you need a really fast parser, then you may program CYAML with a C-backend, but the same interface at the name yaml .现在假设您需要一个非常快速的解析器,然后您可以使用 C 后端编程CYAML ,但名称为yaml接口相同。

在 sphinx 的情况下,您可以使用以下命令模拟 3rd 方包: autodoc_mock_imports

There are multiple reasons why authors choose to use different names in different environments:作者在不同环境中选择使用不同名称的原因有多种:

  • Drop-in replacements : Sometimes it is helpful when you can install a fork and keep the rest of your code the same.直接替换:有时当您可以安装一个 fork 并保持其余代码相同时会很有帮助。 I guess the most famous example is pyyaml / yaml.我想最著名的例子是pyyaml / yaml。 I did it when I created propy3 which can be used as a drop-in replacement for propy .我做到了,当我创建propy3它可以作为一个下拉更换为propy I would say that this is also what happened with pillow .我想说这也是pillow发生的事情。
  • Convenience : beautifulsoup4 can be imported as bs4 (+ package parking for bs4 )方便beautifulsoup4可以导入作为bs4 (+为包停车bs4
  • Lost credentials : I don't know of an example where the import name was changed as well, but I think for flask-restx the package name and the import name were changed.丢失凭据:我不知道导入名称也更改的示例,但我认为对于flask-restx ,包名称和导入名称已更改。

A word of caution一句小心的话

As Ziyad Edher has pointed out in a related discussion , typosquatting is an issue on PyPI ( source ).正如Ziyad Edher相关讨论中指出的那样,域名抢注是 PyPI 上的一个问题( 来源)。 If you add packages with different names, this gets more likely.如果您添加具有不同名称的包,则更有可能出现这种情况。

Other examples其他例子

Name in the docs vs "import" package name vs pypi package name vs anaconda packages vs Debian :文档中的名称 vs "import" 包名 vs pypi 包名vs anaconda 包vs Debian

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM