Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python update package version in requirements.txt

Tags:

python

pip

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?

like image 752
Ramesh Murugesan Avatar asked Apr 16 '26 17:04

Ramesh Murugesan


2 Answers

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
like image 182
radzak Avatar answered Apr 19 '26 06:04

radzak


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]
like image 29
Josh Smeaton Avatar answered Apr 19 '26 06:04

Josh Smeaton



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!