简体   繁体   中英

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?

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

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]

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM