Ramesh Murugesan
Ramesh Murugesan

Reputation: 5013

Python update package version in requirements.txt

I have a requirement.txt file with the list of python package to install. One of the packages is psycopg2==2.6.2 I need to update this package to psycopg2==2.7. I tried to install by pip3 install psycopg2 But it doesn't affect requirement.txt file. Can you please point me in the right direction?

Upvotes: 9

Views: 15935

Answers (4)

htaccess
htaccess

Reputation: 3110

Piping the output of pip freeze into requirements.txt leads to a file that includes your codes dependencies and the dependencies of the dependencies, leading to a bloated requirements.txt file.

If you:

  • like to maintain a lean requirements.txt file with only your dependencies
  • you wish to update all packages in requirements.txt to their latest versions
  • you want to update the packages installed in your venv with the updated versions

then you can do the following:

pip install --user pur
pur # updates versions listed in requiremnts.txt to the latest version
pip install --upgrade -r requirements.txt

At this point you may need to selectively downgrade packages to avoid package dependency conflicts.

See https://pypi.org/project/pur/ for more info.

Upvotes: 1

Codertjay
Codertjay

Reputation: 997

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

Upvotes: 0

Josh Smeaton
Josh Smeaton

Reputation: 48720

As you've discovered, pip doesn't update the requirements file. So the workflow you'd likely want to use is:

  • Update the version of psycopg2 in your requirements file from 2.6.2 to 2.7
  • Run pip install with the upgrade flag

pip3 install -U -r requirements.txt

If you're familiar with tools like npm that do update the version in the catalog file, you may be interested in using pipenv, which manages your dependencies and the virtual environment for you, much like npm does.

If you don't know the latest version of your package, then use pip to figure it out:

$ pip list --outdated | grep psycopg2                                                                           
psycopg2 (2.7.3.2) - Latest: 2.7.4 [wheel]

Upvotes: 8

radzak
radzak

Reputation: 3118

Notice that running pip3 install psycopg2 doesn't respect the requirements.txt file. To upgrade this package you need to use -U option:

pip3 install -U psycopg2

which is a shorthand for:

pip3 install --upgrade psycopg2

After that, you can update your requirements.txt with the following command:

pip freeze > requirements.txt

If you're looking for a solution to automatically update the requirements.txt file after you upgrade package/packages, you can use pip-upgrader.

Installation:

pip install pip-upgrader

Usage:

pip-upgrade

The above command auto-discovers the requirements file and prompts for selecting upgrades. You can also specify a path to the requirements file or/and specify a package to upgrade:

pip-upgrade /path/to/requirements.txt -p psycopg2

Upvotes: 14

Related Questions