ImportPython Error

pip requirements conflict

ERROR: Cannot install -r requirements.txt and package because these package versions have conflicting dependencies

Traceback

terminal
Traceback (most recent call last):
  File "main.py", line 3, in <module>
    djangorestframework==3.12  # needs different Django version
ERROR: Cannot install -r requirements.txt and package because these package versions have conflicting dependencies

What causes this error

The packages specified in requirements.txt have contradictory dependency requirements. The pinned versions cannot coexist in a single environment.

How to fix it

Use `pip-compile` to generate compatible requirements. Remove version pins that cause conflicts. Use `pip check` to verify installed package compatibility. Consider Poetry or PDM for better dependency management.

Code that causes this error

Broken
# requirements.txt
django==4.2
djangorestframework==3.12  # needs different Django version

Fixed code

Fixed
# requirements.in (input for pip-compile)
django>=4.2
djangorestframework>=3.14

# Then run:
# pip-compile requirements.in
# pip-sync requirements.txt

About pip requirements conflict

This error occurs when the packages listed in a requirements file have incompatible dependency constraints. When pip tries to install all packages from `requirements.txt`, it may discover that the pinned versions create an impossible dependency tree. This often happens when requirements.txt was generated from a different Python version, when it contains packages that have since updated their dependency ranges, or when it was manually curated without checking compatibility.

Tools like `pip-compile` (from pip-tools) generate requirements files with fully resolved dependency trees, preventing conflicts. `pip check` verifies that installed packages have compatible dependencies. For complex projects, dependency management tools like Poetry or PDM handle resolution more robustly and maintain a lock file that guarantees reproducible installations.

Common scenarios

1

Installing packages in a different virtual environment than the one in use

2

Circular imports between modules that depend on each other

3

Typos in module or package names in import statements

4

Naming a local file the same as a standard library module

Related errors