Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I need to install cv2 for a script that has been written for me. I tried pip install cv2 and pip install open_cv and got the same problem - a warning message from dist.py and complains about zlib being not found. No cv2 installed. I also tried pyopenvc and pip install opencv-python .

So, I went to the opencv site and downloaded the relevant exe. Ran it - generated a heap of subdirectories and a make file and stuff.

What do I do now?

Install opencv-python (which is the official pre-built OpenCV package for Python) by issuing the following command:

pip install opencv-python
                The superior answer would be to pick opencv-contrib-python as the package of preference to install.
– Sn3akyP3t3
                Mar 25, 2022 at 21:15
                @MrDysprosium if you know the name used to import it but not the name to install it (which, stupidly, is often different) then you have to use PyPi's website or a search engine etc, because pip no longer supports search
– Hack-R
                Sep 8, 2022 at 20:26

run the following command by creating a virtual enviroment using python 3 and run

pip3 install opencv-python

to check it has installed correctly run

python3 -c "import cv2"

In pip package management, there are 4 different OpenCV packages all using the same namespace, cv2. Although they are not officially supported by OpenCV.org, they are commonly used in developers' community. You could install any of them using the following command:

pip install PACKAGE_NAME

where PACKAGE_NAME can be

  • opencv-python (only contains main modules)
  • opencv-contrib-python (contains both main and contrib modules)
  • opencv-python-headless (same as opencv-python but without GUI functionality)
  • opencv-contrib-python-headless (same as opencv-contrib-python but without GUI functionality)
  • You should only install one of them depending on your needs. If you accidentally installed multiple of them in the same environment, you can remove them using pip uninstall before installing the correct one again.

    For more details, you can refer to the project description of OpenCV on Wheels.

    As of 2021, all of these 4 packages are official OpenCV projects. Source: OpenCV Website.

    To Install the Current Latest version of OpenCV then use the below commands:

    Use this Command:

    pip install --upgrade opencv-python
    

    If you're facing problem in above command then try this :

    pip install --upgrade opencv-contrib-python
    

    To check the version of installed OpenCV:

    import cv2
    print(cv2.__version__)
    

    Simply use this for the so far latest version 4.1.0.

    pip install opencv-contrib-python==4.1.0.25
    

    For the default version use this:

    pip install opencv-contrib-python
    

    If you have a new Raspberry Pi and want to install OpenCV then this tutorial would be a good choice.

    For Ubuntu/Linux users:

    sudo apt install python3-opencv
    

    Everybody struggles initially while installing OpenCV. OpenCV requires a lot of dependencies in the backend. The best way to start with OpenCV is, install it in a virtual environment. I suggest that you use the Python Anaconda distribution and create a virtual environment using it. Then inside the virtual environment, you can install OpenCV using this command:

    conda install -c conda-forge opencv
                    While I do agree with the suggestion to use Conda, that particular package has been unmaintained for more than three years!
    – AMC
                    Apr 16, 2020 at 1:39
    

    To install open_cv you can go to this website or do this,

    pip install opencv-contrib-python --upgrade
    pip install opencv-python 
    

    You can test it by:

    C:\> python
    >>> import cv2
    >>> print(cv2.__version__)
    '4.5.1' # your version may be a newer one
    
  • Open anaconda command prompt and type in below command.

    conda install -c conda-forge opencv

  • Once the 'Solving environment' is done. It will ask to download dependencies. Type 'y'.

  • It will install all the dependencies and then you are ready to code.

    I recommend this for Python 3: Please install it this way with pip

    pip3 install opencv-python
    

    This will download and install the latest version of OpenCV.

    pip install opencv-contrib-python

    It will basically download the compatible version. If this command fails, you could upgrade you pip using below command-

    python -m pip install –upgrade pip

    If you need a pictorial guide, head over to Simple Steps to Install OpenCV in Windows

    You can also try installing OpenCV from prebuilt binaries from the official OpenCV site.

  • Open terminal
  • Run the following command pip install --trusted-host=pypi.org --trusted-host=files.pythonhosted.org opencv-python.
  • Hope it will work.
  • As a reference it might help someone... On Debian system I hard to do the following:

    apt-get install -y libsm6 libxext6 libxrender-dev
    pip3 install opencv-python
    python3 -c "import cv2"
    

    Installing cv2 or opencv-python using pip is sometimes a problem. I was having the same problem of installing cv2 with pip. The installation wasn't a problem the problem was to import cv2 after installation. I was getting an Import Error so to fix this i import main from pip to install opencv-python. Try to run the following code in your python file then opencv-python will be installed

    from pip._internal import main as install
        import cv2
    except ImportError as e:
        install(["install", "opencv-python"])
    finally:
    

    I hope this will help someone

    How that actually installs the library depends on which package management tool you're using, and this can be done from the command line. – AMC Apr 16, 2020 at 1:41

    In case you use aarch64 platform with ARM64 cpu - and/or docker

    On a development board on ARM64, no python-opencv version were found at all

    version: NONE. I've had to build from source. This allowed to include CUDA support.

    In my case it was already available on the board but it wasn't found on the development environment.

    If compiling from source is out of reach, there are Dockers

    Of course compiling will take some time (few hours on ARM core), but it is worthy process to know as most open source tools can be built this way in case of issues.

    I've had this problem in Google Colab, It only worked with this specific package version.

    !pip install "opencv-python-headless<4.3"
            

    Thanks for contributing an answer to Stack Overflow!

    • Please be sure to answer the question. Provide details and share your research!

    But avoid

    • Asking for help, clarification, or responding to other answers.
    • Making statements based on opinion; back them up with references or personal experience.

    To learn more, see our tips on writing great answers.

    How can I use cv2.face and cv2 with creating a window by using QT, without getting errors? See more linked questions
  •