Перейти к содержимому

Как обновить requirements txt python

  • автор:

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?

48.4k 24 24 gold badges 132 132 silver badges 165 165 bronze badges
asked Apr 6, 2018 at 11:50
Ramesh Murugesan Ramesh Murugesan
4,927 7 7 gold badges 42 42 silver badges 70 70 bronze badges

Sure, if you want to find out on your own, check out pip freeze and you need to know a bit about redirecting stdout to a file.

Commented Apr 6, 2018 at 11:56

4 Answers 4

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 .


pip install pip-upgrader 



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 

answered Apr 6, 2018 at 12:03
3,076 1 1 gold badge 20 20 silver badges 28 28 bronze badges

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 ( - Latest: 2.7.4 [wheel] 

Python Requirements.txt – How to Create and Pip Install Requirements.txt in Python

Tantoluwa Heritage Alabi

Tantoluwa Heritage Alabi

Python Requirements.txt – How to Create and Pip Install Requirements.txt in Python

There are many Python packages we use to solve our coding problems daily. Take, for instance, the library «Beautiful Soup,» – it doesn’t come with Python by default and needs to be installed separately.

Many projects rely on libraries and other dependencies, and installing each one can be tedious and time-consuming.

This is where a ‘requirements.txt’ file comes into play. requirements.txt is a file that contains a list of packages or libraries needed to work on a project that can all be installed with the file. It provides a consistent environment and makes collaboration easier.

Format of a requirements.txt File


The above image shows a sample of a created requirements.txt file, containing a list of packages and versions of the installation.

Key Terms

I’ve mentioned a few terms so far that you may not know. Here’s what they mean, along with some other important terms you’ll come across when working with requirements.txt:

  • Dependencies are software components that a program needs to run correctly. They can be libraries, frameworks, or other programs.
  • Packages are a way to group together related dependencies. They make it easier to install and manage dependencies.
  • Virtual Environments is a directory that contains a copy of the Python interpreter and all of the packages that are required for a particular project.
  • Pip: This is a package manager for Python. You can use Pip to install, uninstall, and manage Python packages.

How to Create a requirements.txt File

To create a requirements file, you must set up your virtual environment. If you use Pycharm, there’s a virtual environment already setup (.venv). But with Visual Studio code, you have to create the virtual environment yourself.

You can use your terminal or command prompt to create your requirements file. These are the steps to follow when creating the file:

First, open your terminal or command prompt. Then check to see if the file path shown is your working directory. Use the following command to do that:

$ cd folder-name #cd - change directory 

In the command above, replace ‘folder-name’ with the directory name you want to access.


Next, run this command:

$ pip freeze > requirements.txt 

And you’ll see that the requirements file gets added

Here’s the output:


And here’s your newly created requirements.txt file:


The image above shows the dependencies you can work with along with their versions.

How to Work with a requirements.txt File

Now that we have the requirements file, you can see that it consists of a long list of different packages.

To work with the packages, you have to install them. You can do this by using the command prompt or terminal.

Type this command:

pip install -r requirements.txt 

It will look like this:


Now that all the dependencies are installed, you can work with requirements.txt.

Example of using requirements.txt

In this example, we will be working with two libraries, beautifulsoup4 and requests , to return some information from a site.


In the image above, we see that the two libraries are present in the requirements.txt file and their version. Now we can work with the libraries because we installed them previously.

  • Import the library BeautifulSoup from the package name bs4 (beautifulsoup4) and also import the library requests.
from bs4 import BeautifulSoup import requests 
  • To fetch information from the website URL, we use the .get() method to tap into the requests library.
web_data = requests.get("https://www.lithuania.travel/en/category/what-is-lithuania", headers=) 
  • Now that we have access to the the URL, the Beautiful Soup library accepts the web_data and returns all HTML contents present in it.
soup = BeautifulSoup(web_data.content, features="html.parser") 
  • The final result I chose to return is elements with the

    tag in the first position [0].

news_info = soup.findAll("p")[0] print(news_info.text 

Bringing it all together:

from bs4 import BeautifulSoup import requests web_data = requests.get("https://www.lithuania.travel/en/category/what-is-lithuania", headers=) soup = BeautifulSoup(web_data.content, features="html.parser") news_info = soup.findAll("p")[0] print(news_info.text) 

And here’s the output:


Benefits of Using a requirements.txt File

  • Managing dependencies: By listing the dependencies of your project in a requirements.txt file, you can easily see what packages are required and what versions they need to be.
  • Sharing your project with others: If you share your project with others, you can include the requirements.txt file so that they can easily install the required packages. This can save them time and frustration and can help to ensure that everyone is using the same versions of the packages.


In the article, we learned how to create a requirements.txt file and outlined the benefits of using it.

You should also try it out and work on a few projects with it. If you have any questions, you can reach out to me on Twitter ��.

Requirements File Format¶

Requirements files serve as a list of items to be installed by pip, when using pip install . Files that use this format are often called “pip requirements.txt files”, since requirements.txt is usually what these files are named (although, that is not a requirement).

The requirements file format is closely tied to a number of internal details of pip (e.g., pip’s command line options). The basic format is relatively stable and portable but the full syntax, as described here, is only intended for consumption by pip, and other tools should take that into account before using it for their own purposes.


# This is a comment, to show how #-prefixed lines are ignored. # It is possible to specify requirements as plain names. pytest pytest-cov beautifulsoup4 # The syntax supported here is the same as that of requirement specifiers. docopt == 0.6.1 requests [security] >= 2.8.1, == 2.8.* ; python_version  "2.7" urllib3 @ https://github.com/urllib3/urllib3/archive/refs/tags/1.26.8.zip # It is possible to refer to other requirement files or constraints files. -r other-requirements.txt -c constraints.txt # It is possible to refer to specific local distribution paths. ./downloads/numpy-1.9.2-cp34-none-win32.whl # It is possible to refer to URLs. http://wxpython.org/Phoenix/snapshot-builds/wxPython_Phoenix-3.0.3.dev1820+49a8884-cp34-none-win_amd64.whl 


Each line of the requirements file indicates something to be installed, or arguments to pip install . The following forms are supported:

For details on requirement specifiers, see Requirement Specifiers . For examples of all these forms, see Examples .


Requirements files are utf-8 encoding by default and also support PEP 263 style comments to change the encoding (i.e. # -*- coding: -*- ).

Line continuations¶

A line ending in an unescaped \ is treated as a line continuation and the newline following it is effectively ignored.


A line that begins with # is treated as a comment and ignored. Whitespace followed by a # causes the # and the remainder of the line to be treated as a comment.

Comments are stripped after line continuations are processed.

Supported options¶

Requirements files only supports certain pip install options, which are listed below.

Global options¶

The following options have an effect on the entire pip install run, and must be specified on their individual lines.

  • -i, —index-url
  • —extra-index-url
  • —no-index
  • -c, —constraint
  • -r, —requirement
  • -e, —editable
  • -f, —find-links
  • —no-binary
  • —only-binary
  • —prefer-binary
  • —require-hashes
  • —pre
  • —trusted-host
  • —use-feature

To specify —pre , —no-index and two —find-links locations:

--pre --no-index --find-links /my/local/archives --find-links http://some.archives.com/archives 

Per-requirement options¶

Added in version 7.0.

The options which can be applied to individual requirements are:

  • —global-option
  • —config-settings
  • —hash (for Hash-checking Mode )

Referring to other requirements files¶

If you wish, you can refer to other requirements files, like this:

-r more_requirements.txt 

You can also refer to constraints files , like this:

-c some_constraints.txt 

Using environment variables¶

Added in version 10.0.

pip supports the use of environment variables inside the requirements file.

You have to use the POSIX format for variable names including brackets around the uppercase name as shown in this example: $ . pip will attempt to find the corresponding environment variable defined on the host system at runtime.

There is no support for other variable expansion syntaxes such as $VARIABLE and %VARIABLE% .

You can now store sensitive data (tokens, keys, etc.) in environment variables and only specify the variable name for your requirements, letting pip lookup the value at runtime. This approach aligns with the commonly used 12-factor configuration pattern.

Influencing the build system¶

This disables the use of wheels (cached or otherwise). This could mean that builds will be slower, less deterministic, less reliable and may not behave correctly upon installation.

This mechanism is only preserved for backwards compatibility and should be considered deprecated. A future release of pip may drop these options.

The —global-option option is used to pass options to setup.py .

These options are highly coupled with how pip invokes setuptools using the setup.py (legacy) build system interface. It is not compatible with newer pyproject.toml build system interface.

This is will not work with other build-backends or newer setup.cfg-only projects.

If you have a declaration like:

FooProject >= 1.2 --global-option="--no-user-cfg"

The above translates roughly into running FooProject’s setup.py script as:

python setup.py --no-user-cfg install

Note that the only way of giving more than one option to setup.py is through multiple —global-option options.

Upgrading python and its packages to a newer version for my project

For some context, my project uses python version 3.8 and has some packages in a requirements.txt file. I’m trying to upgrade the python version to 3.10 and also all the packages in my requirements.txt file to the latest possible version such that there are no conflicts between dependencies. Is there a way to do this?

asked Jul 3, 2022 at 5:49
13 1 1 silver badge 3 3 bronze badges
Could you provide a little more info on what OS you’re using?
Commented Jul 3, 2022 at 7:38
I’m using macOS
Commented Jul 3, 2022 at 9:10

5 Answers 5

It is a bit hard to say what will work best for you since you’ve given no info on your OS, and that thing you need is done differently on macOS and Windows.

If you’re on macOS (this one could also work for Linux I guess), the best way to manage python versions is with pyenv and to update python packages with pip-review , both of which you can install via brew .

So first, you want to create a list of all your packages installed. To do that, you type pip3 freeze > requirements.txt in the terminal. The command creates a txt file in your home folder. In the file, you can see the names of all your packages installed, and it will look like this:

astroid==2.11.6 async-generator==1.10 attrs==21.4.0 autopep8==1.6.0 beautifulsoup4==4.11.1 

Secondly, to update your python version, you’ll need to type pyen install -l in the terminal to get a list of all currently available versions of python. To install the one you need, let’s say it’s the most recent at the moment 3.10.5, type pyenv install 3.10.5 . To set this version as a default one, type pyenv global 3.10.5 . If you don’t need your previous version of python, just type pyenv uninstall and the number of the version you want to delete after a space (it’s really just like installing one).

Thirdly, to move all your previous packages in the newly installed python version, just type pip3 install -r requirements.txt in the terminal. The command will get the packages of the same versions you had before.

Lastly, to upgrade the packages to the most recent version, type pip-review —auto . It will fetch the latest versions available and install them with no more commands from you.

Now here’s why I think pip-review works better than anything else I’ve tried so far: in case some dependencies are broken (i. e. a package you are using needs another package of an older version than you’ve just upgraded to), it will point that out in the terminal, and you could get the right one.

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *