-2

I have a requirements.txt with dependencies pinned using ==. The pinning using == is a requirement to ensure reproducibility. I'd like to update all of them to the most recent version. I do not want to install any of them, I only want to modify requirements.txt

Philippe
  • 1,715
  • 4
  • 25
  • 49
  • Please add some more details, or an example of what you are wanting to do. As written, this question is likely to be closed due to lack of details. – blackbrandt May 23 '23 at 13:00
  • Well, packages mentioned in requirements.txt usually installed when you do `pip install -r requirements.txt `. – Abdul Niyas P M May 23 '23 at 13:00
  • 1
    To always refer to latest version then you simply don't pin to a specific version number. – TheMonarch May 23 '23 at 13:35
  • Short of gathering all the latest version numbers and updating requirements.txt, I don't think pip has a ready command to do what you want. This discussion might give you pointers on how you can work out a solution https://stackoverflow.com/questions/24764549/upgrade-python-packages-from-requirements-txt-using-pip-command – TheMonarch May 23 '23 at 14:18

1 Answers1

1

There are many options for getting the latest package versions from PyPI described in Python and pip, list all versions of a package that's available?. If I take one that has an API that can be used within Python, e.g., the answer describing using the luddite package, then you could do the following (after installing luddite itself):

Let's assume you have a requirements.txt file containing:

numpy==1.20.1
scipy==1.5.2
panda==1.5.3

then you could do:

import luddite

requirements = []

# read in packages and get the latest versions
with open("requirements.txt", "r") as fp:
   for line in fp.readlines():
       package = line.strip().split("==")[0]

       # get latest version here
       latestversion = luddite.get_versions_pypi(package)[-1]
       requirements.append(f"{package}=={latestversion}")

# overwrite requirements.txt with latest versions
with open("requirements.txt", "w") as fp:
    for i, req in enumerate(requirements):
        fp.write(req)
        if i < len(requirements) - 1:
            fp.write("\n")

It should then (as of 23 May 2023) contain:

numpy==1.24.3
scipy==1.10.1
pandas==2.0.1
Matt Pitkin
  • 3,989
  • 1
  • 18
  • 32