1.5. Using pip

1.5.1. Installation and Usage

If you have previous/other manually installed (= not installed via pip) version of OpenCV installed (e.g. cv2 module in the root of Python’s site-packages), remove it before installation to avoid conflicts.

Select the correct package for your environment:

There are four different packages and you should select only one of them. Do not install multiple different packages in the same environment. There is no plugin architecture: all the packages use the same namespace (cv2). If you installed multiple different packages in the same environment, uninstall them all with pip uninstall and reinstall only one package.

  1. Packages for standard desktop environments (Windows, macOS, almost any GNU/Linux distribution)

run pip install opencv-python if you need only main modules run pip install opencv-contrib-python if you need both main and contrib modules (check extra modules listing from OpenCV documentation)

  1. Packages for server (headless) environments

These packages do not contain any GUI functionality. They are smaller and suitable for more restricted environments.

run pip install opencv-python-headless if you need only main modules run pip install opencv-contrib-python-headless if you need both main and contrib modules (check extra modules listing from OpenCV documentation)

Import the package:

import cv2

All packages contain haarcascade files. cv2.data.haarcascades can be used as a shortcut to the data folder. For example:

cv2.CascadeClassifier(cv2.data.haarcascades + “haarcascade_frontalface_default.xml”)

Read OpenCV documentation

Before opening a new issue, read the FAQ below and have a look at the other issues which are already open.