如何从Python包的require.txt中读取依赖项
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了如何从Python包的require.txt中读取依赖项,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3167字,纯文字阅读大概需要5分钟。
内容图文
![如何从Python包的require.txt中读取依赖项](/upload/InfoBanner/zyjiaocheng/669/b834b2167fbc494db70b7f38e883b3e3.jpg)
我需要依赖项,因为我想将它们添加到我的RPM元数据中.
要构建,我使用:
python setup.py bdist_rpm
当我构建cryptography-2.2.2软件包时,它将创建一个文件/src/cryptography.egg-info/requires.txt
它包含:
idna>=2.1
asn1crypto>=0.21.0
six>=1.4.1
[:platform_python_implementation != 'PyPy']
cffi>=1.7
[:python_version < '3']
enum34
ipaddress
如何读取所有依赖项,并评估[]之间的表达式?
我正在使用python 2.7(不要问)
我需要以下输出:
idna>=2.1
asn1crypto>=0.21.0
six>=1.4.1
cffi>=1.7
enum34
ipaddress
我想省略其他部分,例如[doc],[test]等.
解决方法:
require.txt是dependency metadata的一部分,因此您可以在安装egg时使用easy_install使用的相同工具.假设文件require.txt位于当前目录中:
In [1]: from pkg_resources import Distribution, PathMetadata
In [2]: dist = Distribution(metadata=PathMetadata('.', '.'))
现在,您可以使用Distribution.requires()过滤当前平台的所有依赖项:
In [3]: sys.version
Out[3]: '3.6.4 (v3.6.4:d48ecebad5, Dec 18 2017, 21:07:28) \n[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)]'
In [4]: dist.requires()
Out[4]:
[Requirement.parse('idna>=2.1'),
Requirement.parse('asn1crypto>=0.21.0'),
Requirement.parse('six>=1.4.1'),
Requirement.parse('cffi!=1.11.3,>=1.7'),
Requirement.parse('cffi>=1.7')]
如果使用Python 2.7,则列表将有所不同:
In [4]: sys.version
Out[4]: '2.7.10 (default, Oct 6 2017, 22:29:07) \n[GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.31)]'
In [5]: dist.requires()
Out[5]:
[Requirement.parse('idna>=2.1'),
Requirement.parse('asn1crypto>=0.21.0'),
Requirement.parse('six>=1.4.1'),
Requirement.parse('cffi!=1.11.3,>=1.7'),
Requirement.parse('cffi>=1.7'),
Requirement.parse('enum34'),
Requirement.parse('ipaddress')]
或PyPy:
In [2]: sys.version
Out[2]: '3.5.3 (fdd60ed87e941677e8ea11acf9f1819466521bf2, Apr 26 2018, 01:25:35)\n[PyPy 6.0.0 with GCC 4.2.1 Compatible Apple LLVM 9.1.0 (clang-902.0.39.1)]'
In [3]: d.requires()
Out[3]:
[Requirement.parse('idna>=2.1'),
Requirement.parse('asn1crypto>=0.21.0'),
Requirement.parse('six>=1.4.1'),
Requirement.parse('cffi!=1.11.3,>=1.7')]
现在,如果要生成需求字符串列表(例如,当您要为pip生成需求文件时),请将需求转换为字符串:
In [8]: os.linesep.join(str(r) for r in dist.requires())
Out[8]:
'idna>=2.1\nasn1crypto>=0.21.0\nsix>=1.4.1\ncffi!=1.11.3,>=1.7'
PEP 508
如果您还希望独立于当前平台考虑PEP 508 environment markers,事情可能会有些棘手,但仍可管理.首先,使用env标记转换需求:
In [22]: dep_map_pep508 = {k: v for k, v in dist._build_dep_map().items() if k and k.startswith(':')}
In [24]: reqs_pep508 = [str(r) + ';' + k.lstrip(':') for k, v in dep_map_pep508.items() for r in v]
In [25]: reqs_pep508
Out[25]:
["cffi>=1.7;platform_python_implementation != 'PyPy'",
"enum34;python_version >= '3'",
"ipaddress;python_version >= '3'"]
现在处理与平台无关的部门,这些部门位于dist的依赖关系图中的None键下:
In [26]: reqs_no_platform = [str(r) for r in dist._build_dep_map()[None]]
In [27]: reqs_no_platform
Out[27]: ['idna>=2.1', 'asn1crypto>=0.21.0', 'six>=1.4.1', 'cffi!=1.11.3,>=1.7']
将两个列表合并为准备写入需求文件的字符串:
In [28]: os.linesep.join(reqs_no_platform + reqs_pep508)
Out[28]: "idna>=2.1\nasn1crypto>=0.21.0\nsix>=1.4.1\ncffi!=1.11.3,>=1.7\ncffi>=1.7;platform_python_implementation != 'PyPy'\nenum34;python_version >= '3'\nipaddress;python_version >= '3'"
内容总结
以上是互联网集市为您收集整理的如何从Python包的require.txt中读取依赖项全部内容,希望文章能够帮你解决如何从Python包的require.txt中读取依赖项所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。