英文:
requirements.txt: any version < 1.5 (incl dev and rc versions)
问题
我正在寻找一个适用于pip和Python 3.10的requirement.txt的模式,它将覆盖所有可用的版本,直到版本1.5,例如:
- 1.4.2.dev5+g470a8b8
- 1.4.dev22+g2be722f
- 1.4
- 1.4rc0
- 1.5rc1
另外,有没有一种巧妙的方法可以在不在新的虚拟环境中实际运行“pip install”的情况下进行测试?
英文:
I am looking for a pattern for Python's requirement.txt (for usage with pip and python 3.10), which will cover all versions available up to a version 1.5, e.g.
- 1.4.2.dev5+g470a8b8
- 1.4.dev22+g2be722f
- 1.4
- 1.4rc0
- 1.5rc1
And: is there a clever way to test this without actually running "pip install" in a fresh venv?
答案1
得分: 1
以下是您要求的翻译内容:
You should be able to test with python -m pip install --dry-run
without actually installing anything.
您应该能够使用 python -m pip install --dry-run
进行测试,而不会真正安装任何内容。
You can also install and test with packaging
which is the library used by pip internally.
您还可以使用 packaging
安装并进行测试,这是 pip 内部使用的库。
I guess !=1.5,<=1.5
should be what you want.
我猜 !=1.5,<=1.5
应该是您想要的。
So you could use it like this:
因此,您可以像这样使用它:
python -m pip install --pre 'somepackage!=1.5,<=1.5'
或者在 requirements.txt
文件中:
--pre
somepackage !=1.5, <=1.5
相关:
英文:
You should be able to test with python -m pip install --dry-run
without actually installing anything.
You can also install and test with packaging
which is the library used by pip internally.
I guess !=1.5,<=1.5
should be what you want.
import packaging.specifiers
import packaging.version
version_strings = [
'1',
'1.0'
'1.1',
'1.4',
'1.4.2',
'1.4.2.dev5+g470a8b8',
'1.4.dev22+g2be722f',
'1.4',
'1.4rc0',
'1.5rc1',
'1.5',
'1.5.1',
'1.6',
'2',
'2.0',
]
versions = [packaging.version.Version(v) for v in version_strings]
specifier = packaging.specifiers.SpecifierSet('!=1.5,<=1.5', prereleases=True)
for version in versions:
print(f'{version} in {specifier}: {version in specifier}')
$ python test.py
1 in !=1.5,<=1.5: True
1.1.1 in !=1.5,<=1.5: True
1.4 in !=1.5,<=1.5: True
1.4.2 in !=1.5,<=1.5: True
1.4.2.dev5+g470a8b8 in !=1.5,<=1.5: True
1.4.dev22+g2be722f in !=1.5,<=1.5: True
1.4 in !=1.5,<=1.5: True
1.4rc0 in !=1.5,<=1.5: True
1.5rc1 in !=1.5,<=1.5: True
1.5 in !=1.5,<=1.5: False
1.5.post0 in !=1.5,<=1.5: False
1.5.0 in !=1.5,<=1.5: False
1.5.1 in !=1.5,<=1.5: False
1.6 in !=1.5,<=1.5: False
2 in !=1.5,<=1.5: False
2.0 in !=1.5,<=1.5: False
So you could use it like this:
python -m pip install --pre 'somepackage!=1.5,<=1.5'
or in a requirements.txt
file:
--pre
somepackage !=1.5, <=1.5
Related:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论