How to add my local compiled OpenCV to Python path?

Viewed 3105

I'm using Windows 10 with python 3 installed. I downloaded the opencv 4 source code and compiled it from its cmake file successfully. I also run the install.vcproj to install it.

The install path is like this:

C:\OpenCV
  bin
  etc
  include
  sample
  x86
    vc15
      bin
      lib

Then I manually add C:\OpenCV\x86\vc15\bin to the environment variable of path, so that I can run the compiled c++ samples.

Now I want to run some python samples, in C:\OpenCV\samples\python, for example, the opencv_version.py, but I have a ModuleNotFoundError at this line of code:

import cv2 as cv

It seems that my locally compile opencv can't be found by python.

I know that if I want to use the precompiled opencv, I just run pip3 install opencv-python, but now I don't want to use the precompiled version, I want to use my own compiled opencv for python.

Questions:

  1. Do I need to change some cmake configurations during my build of opencv to enable its python binding?
  2. How do I add my own opencv to the path of python, which path (in my case, which C:\OpenCV sub-folder) should be added?
2 Answers

once you have compiled from source successfully using make, purge any other versions of opencv and just run make install.

you have to add this opencv in python modules

There are several ways to add things to the PYTHONPATH.

Read http://docs.python.org/library/site.html

Set the PYTHONPATH environment variable prior to running your script.

You can do this python -m agent to run agent.py from your PYTHONPATH.

Create .pth files in your lib/site-packages directory.

Install your modules in lib/site-packages.
Related