简体   繁体   中英

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.

For example, if packageA requires child>=0.9 and packageB requires child<=1.1, then pip will install child==1.1.

I'm trying to write a script to scan a requirements.txt and pin all unlisted child package versions.

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. Everything left should be child packages and the highest version number that pip calculated.

However, this requires creating a Python virtual environment and installing all packages, which can take a bit a time. Is there any option in pip to do the version calculation without actually installing the packages? If not, is there pip-like tool that provides this?

You can read the package requirements for PyPI hosted packages with the json module:

python3 terminal/script:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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