Home > front end >  Getting pip dependency versions without installing?
Getting pip dependency versions without installing?

Time:03-26

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?

CodePudding user response:

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()

CodePudding user response:

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

  • Related