简体   繁体   English

获取 pip 依赖版本而不安装?

[英]Getting pip dependency versions without installing?

When you tell pip to install multiple packages simultaneously, it looks up all child dependencies and installs the most recent version that is allowed by all parent package restrictions.当您告诉 pip 同时安装多个包时,它会查找所有子依赖项并安装所有父 package 限制允许的最新版本。

For example, if packageA requires child>=0.9 and packageB requires child<=1.1, then pip will install child==1.1.例如,如果 packageA 要求 child>=0.9,而 packageB 要求 child<=1.1,那么 pip 将安装 child==1.1。

I'm trying to write a script to scan a requirements.txt and pin all unlisted child package versions.我正在尝试编写一个脚本来扫描requirements.txt并固定所有未列出的子 package 版本。

One way to do this would be to simply install everything from a requirements.txt file via pip install -r requirements.txt , then parse the output of pip freeze , and strip out all the packages from my requirements.txt file.一种方法是通过pip install -r requirements.txt简单地安装requirements.txt文件中的所有内容,然后解析 pip pip freeze的 output,并从我的requirements.txt文件中删除所有包。 Everything left should be child packages and the highest version number that pip calculated.剩下的应该是子包和 pip 计算出的最高版本号。

However, this requires creating a Python virtual environment and installing all packages, which can take a bit a time.但是,这需要创建一个 Python 虚拟环境并安装所有包,这可能需要一点时间。 Is there any option in pip to do the version calculation without actually installing the packages? pip 中是否有任何选项可以在不实际安装软件包的情况下进行版本计算? If not, is there pip-like tool that provides this?如果没有,是否有提供此功能的类似 pip 的工具?

You can read the package requirements for PyPI hosted packages with the json module:您可以使用 json 模块阅读 PyPI 托管包的 package 要求:

python3 terminal/script: python3 终端/脚本:

import csv
import json
import requests

your_package = str('packageA')
pypi_url = 'https://pypi.python.org/pypi/' + your_package + '/json'
data = requests.get(pypi_url).json()

reqs = data['info']['requires_dist']
print(reqs)
print('Writing requirements for ' + your_package + ' to requirements.csv')
f = open('requirements.csv', 'w')
w = csv.writer(f, delimiter = ',')
w.writerows([x.split(',') for x in reqs])
f.close()

If i get your question correctly you're trying to install a particular version of a package, one way to do this is after specifying the version in ur requirement.txt file, it can be installed by using pip install then the dependency plus the version example pip install WhatsApp v3.5如果我正确地回答了你的问题,你正在尝试安装 package 的特定版本,一种方法是在你的 requirement.txt 文件中指定版本之后,它可以通过使用 pip 安装然后依赖项加上版本来安装例如 pip 安装 WhatsApp v3.5

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

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