Upgrade python packages from requirements.txt using pip command

147,178

Solution 1

No. Your requirements file has been pinned to specific versions. If your requirements are set to that version, you should not be trying to upgrade beyond those versions. If you need to upgrade, then you need to switch to unpinned versions in your requirements file.

Example:

lxml>=2.2.0

This would upgrade lxml to any version newer than 2.2.0

lxml>=2.2.0,<2.3.0

This would upgrade lxml to the most recent version between 2.2.0 and 2.3.0.

Solution 2

I already answered this question here. Here's my solution:

Because there was no easy way for upgrading package by package, and updating the requirements.txt file, I wrote this pip-upgrader which also updates the versions in your requirements.txt file for the packages chosen (or all packages).

Installation

pip install pip-upgrader

Usage

Activate your virtualenv (important, because it will also install the new versions of upgraded packages in current virtualenv).

cd into your project directory, then run:

pip-upgrade

Advanced usage

If the requirements are placed in a non-standard location, send them as arguments:

pip-upgrade path/to/requirements.txt

If you already know what package you want to upgrade, simply send them as arguments:

pip-upgrade -p django -p celery -p dateutil

If you need to upgrade to pre-release / post-release version, add --prerelease argument to your command.

Full disclosure: I wrote this package.

Solution 3

you can try:

pip install --upgrade --force-reinstall -r requirements.txt

You can also ignore installed package and install the new one :

pip install --ignore-installed -r requirements.txt

Solution 4

I suggest freezing all of your dependencies in order to have predictable builds.

When doing that, you can update all dependencies at once like this:

sed -i '' 's/[~=]=/>=/' requirements.txt
pip install -U -r requirements.txt
pip freeze | sed 's/==/~=/' > requirements.txt

Having done the above, test your project with the new set of packages and eventually commit the requirements.txt file to the repository while still allowing for installing hot-fixes.

Solution 5

Another solution is to use the upgrade-requirements package

pip install upgrade-requirements

And then run :

upgrade-requirements

It will upgrade all the packages that are not at their latest versions, and also create an updated requirements.txt at the end.

Share:
147,178

Related videos on Youtube

abhiomkar
Author by

abhiomkar

"There are no accidents!"

Updated on February 09, 2022

Comments

  • abhiomkar
    abhiomkar about 2 years

    How do I upgrade all my python packages from requirements.txt file using pip command?

    tried with below command

    $ pip install --upgrade -r requirements.txt
    

    Since, the python packages are suffixed with the version number (Django==1.5.1) they don't seem to upgrade. Is there any better approach than manually editing requirements.txt file?

    EDIT

    As Andy mentioned in his answer packages are pinned to a specific version, hence it is not possible to upgrade packages through pip command.

    But, we can achieve this with pip-tools using the following command.

    $ pip-review --auto
    

    this will automatically upgrade all packages from requirements.txt (make sure to install pip-tools using pip install command).

    • erikreed
      erikreed over 7 years
      Pip-tools is working great -- updated syntax is pip-compile -U requirements.txt.
    • mash
      mash over 3 years
      Now it's pip install pip-review. pypi.org/project/pip-review
  • abhiomkar
    abhiomkar almost 10 years
    with that option it seems to reinstall the same version. As Andy mentioned in above answer, packages are pinned to specific version.
  • Freelancer
    Freelancer almost 10 years
    @abhiomkar you're rigth I thought you wanted to re install the same version (maybe to add backport fix)
  • AXO
    AXO about 7 years
    The second command is what I was looking for. Notice that -I and --ignore-installed are the same flags and of-course it's not valid to have a , in there. This way no downgrades will occur during install and after installation of requirements is complete one can upgrade installed packages using pip-review --auto.
  • zhukovgreen
    zhukovgreen almost 7 years
    I found helpful to do the following. 1. Deleted venv 2. Created a new one with the same name (the way to clean all pip packages) 3. Replace all == to >= in the requirements.txt 4. pip install -r requirements.txt 4.
  • Jacopofar
    Jacopofar over 6 years
    Great! It's surprising that such a function is not present directly in pip
  • pulsejet
    pulsejet about 6 years
    Elegant! Super-glad I scrolled down to this :P
  • Pierre.Sassoulas
    Pierre.Sassoulas over 5 years
    I just used this, and this is the best thing since f-strings.
  • mszaro
    mszaro over 5 years
    Great tool. Thank you.
  • Gal Avineri
    Gal Avineri over 4 years
    Very useful tool! I also found another package called pur that upgrades the pinned versions as well
  • philshem
    philshem over 4 years
    sed 's/==/>=/g' requirements.txt > TMP_FILE && mv TMP_FILE requirements.txt will replace all == with >=
  • fanni
    fanni over 4 years
    This is ideal example of the anti-pattern. What's wrong here: 1) The requirements.txt is a .txt file, but you've made it executable 2). There is a simple pip install -r requirements.txt command. So you can use requirements.txt file only for listing your project dependencies. 3) You're not using versions of the packages. 4) Not a common pattern, other developers do not nothing about how to use it. 5) Hard to use with CI/CD pipelines. Please don't use this example
  • rioted
    rioted about 4 years
    this is a bad idea becuase it will also add the requirements of all your requirements, resulting in an unnecessarily large requirements file. If you then decide to update a single dependency, you are likely to get version conflicts, unless you know which other requirements were added to your requirements file because of that package.
  • Robert Rendell
    Robert Rendell over 3 years
    Awesome! It even gave me a warning when I hadn't activated my venv. It also gives you the option of which packages to upgrade after it finds all the new versions. Excellent work!
  • Zack Plauché
    Zack Plauché over 3 years
    Full disclosure: This package is epic.
  • Cameron Gagnon
    Cameron Gagnon over 3 years
    Beautiful. Elegant. Easier than I was expecting. Use this answer if you don't mind the extra install!
  • vidstige
    vidstige over 3 years
    that's all good. So after a few months packages will have updates, how do you update those and again commit the .txt file?
  • Zephaniah Grunschlag
    Zephaniah Grunschlag over 3 years
    This is definitely the best solution, as it uses pip drrectly without having to install yet another package.
  • Emilio Ferreyra
    Emilio Ferreyra about 3 years
    Thank you very much. This package is very useful.
  • Hermes
    Hermes almost 3 years
    I updated my post so that it would better depict my approach. Assuming that the app is alive and actively developed, some changes are made to it from time to time. At some of these occasions its dependencies can be manually updated using the above approach. This may require some extra changes as there may be some incompatibilities. Other than that, the changes always go through CI/CD during which at least some hot-fixes can be applied thanks to ~= in the requirements.txt. Since with ~= no significant and breaking changes are to be expected, the builds can still be considered predictable.
  • Rich Lysakowski PhD
    Rich Lysakowski PhD almost 3 years
    conda handles all these updating issues, and guarantees entire environment integrity. pip is greedy and selfish and only installs or updates (or downgrades) what it needs to install current packages, sometimes downgrading core packages and breaking things. Conda handles package and environment management at once. Upgrading pip AND venv OR virtualenv (OR pip-whatever is one more unnecessary headache. I use pip as the last resort in conda environment when conda packages are not available. This pip upgrader package makes it easier to tolerate pip when conda packages are not available.
  • shadow
    shadow over 2 years
    Dayum, what a great package, thanks for sharing!
  • AnonymousUser
    AnonymousUser over 2 years
    It didn't work, I still have old version. It uninstalled the old version, then installed the old version again.
  • Oliver
    Oliver almost 2 years
    Pip upgrader has been discontinued per its github page.