IntelliJ IDEA 2021.1 Help

Manage dependencies using requirements.txt

IntelliJ IDEA provides integration with the major means of requirements management and makes it possible to track the unsatisfied requirements in your projects.

Define requirements

  1. From the Tools menu, select Sync Python Requirements.

  2. In the opened dialog, specify the name of the requirements file. The recommended name for the requirements file is requirements.txt. When a file with this name is added to the root project directory, it is automatically detected by Python Integrated tools.

    Define requirements.txt
  3. Select the method of handling versions of the required libraries. The version numbers can be defined:

    MethodExample
    Strong equalityDjango==3.0.3
    Greater or equalDjango>=3.0.3
    Compatible versionDjango~=3.0.3
  4. Define the requirements management policy:

    PolicyAction
    Remove unused requirementsDeletes records that correspond to unused libraries and packages.
    Modify base filesAllows modifications in the base requirements files (if any is referenced in the requirements.txt file).
    Keep existing version specifier if it matches the current versionLeaves the version number unchanged if it satisfied the selected method versions handling.
  5. Click OK and inspect the generated file.

    Generate requirements.txt file

You can also run pip freeze > requirements.txt in the command line to generate a requirements.txt file for your project. See https://pip.pypa.io/en/stable/reference/pip_freeze/ for more details.

If the name of the requirements file differs from requirements.txt or when you have several requirements files in one project, you have to notify IntelliJ IDEA about the requirements file you want to apply.

Configure the default requirements file

  1. Press Ctrl+Alt+S to open IDE settings and select Tools | Python Integrated Tools.

  2. In the Package requirements file field, type the name of the requirements file or click the browse button and locate the desired file.

  3. Click OK to save the changes.

Though you can always run the Sync Python Requirements to update the requirements file, IntelliJ IDEA provides quick fixes that enable populating this file.

Update a requirements file

  1. In an import statement of a Python file, click a package which is not yet imported. IntelliJ IDEA suggests a quick-fix:

    Quick fix to add a requirement to the requirement.txt file
  2. Select and apply the suggested quick-fix. The package is added to the dependency management file.

IntelliJ IDEA provides quick fixes and notifications related to the unsatisfied dependencies.

Install the required packages

  1. Open a project with the requirements file specified, a notification bar is displayed on top of any Python or requirements file opened in Editor:

    Notification on the requirements
  2. Click one of the provided links to satisfy or ignore requirements.

If you have selected the Ignore option, you can always change your mind, and add the previously ignored packages.

Add the ignored dependencies

  1. Press Ctrl+Alt+S to open project Settings/Preferences.

  2. Go to Editor | Inspections.

  3. In the list of the inspections, select Package requirements.

  4. Preview the list of the ignored requirements and click the Add icon (Add a package) to add them.

    Adding an ignored package

Last modified: 15 June 2021