Pip: Install Requirements – Exclude Specific Packages

The requirements.txt file that contains the Python application dependencies is usually generated by developers using the pip freeze > requirements.txt command.

If you install dependencies with the pip install -r requirements.txt command as a part of some automated pipeline or a Dockerfile and you have an issue with some of the dependencies, you may wonder how to exclude and not to install the packages that cause the issue.

In this note i will show how to exclude the specific packages while installing the Python dependencies using the pip install -r requirements.txt command.

Cool Tip: How to install specific version of a package using pip! Read More →

Pip Install Requirements – Exclude Packages

Install the dependencies from the requirements.txt file using the pip command, excluding the specific packages:

$ pip install -r $(grep -v '^ *#\|^pkg1\|^pkg2' requirements.txt | grep .)

The $(...) code above cleans the requirements.txt from comments, blank lines and excludes the undesired packages.

Leave a Reply