Opencv Build From Source Python binding problem

Hello

While building from source with the settings below I get both config.py and config-3.10.py.

PYTHON3_EXECUTABLE …
PYTHON3_INCLUDE_DIR …
PYTHON3_LIBRARY …
BUILD_SHARED_LIBS OFF
OPENCV_ENABLE_NONFREE ON
OPENCV_EXTRA_MODULES_PATH …
BUILD_opencv_world OFF

But while building from source with the settings below config.py is generated but config-3.10.py is not generated.

PYTHON3_EXECUTABLE …
PYTHON3_INCLUDE_DIR …
PYTHON3_LIBRARY …
BUILD_SHARED_LIBS OFF
OPENCV_ENABLE_NONFREE ON
OPENCV_EXTRA_MODULES_PATH …
BUILD_opencv_world ON

Therefore when I want to import cv2 in Python I get the error:

ImportError: OpenCV loader: missing configuration file: [‘config-3.10.py’, ‘config-3.py’]. Check OpenCV installation.

There is no error while including in C++.

What am I missing?

Thank you

opencv version ? os ?

so, this makes all the difference ?
if it leads to a wrong configuration, it probably should get reported

however:

  • it is set to OFF by default (why did you change it ?)
  • it is irrelevant (or , should be !) for the python bindings in general
    (if it’s ON, c++ libs will be merged to a single, fat one)
    you correctly set BUILD_SHARED_LIBS=OFF, so it should link to the c++ libs statically, no matter if it’s 15 small or a single big one

Hello berak

Thank you for the answer.

The reason why I generate BUILD_opencv_world is in forums I observed that it is a commonly used method to enable BUILD_opencv_world.
When BUILD_opencv_world is enabled you do not need to look for which .dll should be included for different opencv functions.

Even if it is irrelevant, I think enabling BUILD_opencv_world prevents python3 bindings from being successfully built.

When I search for this error I observe others facing the same problem.
I can share the links if you want?

best regards

Which OS are you on I have no issues with OpenCV World on Ubuntu and Windows when generating python bindings. That said I don’t usually go for a static build.

Are you sure this is the issue and its not just that you haven’t cleaned your build directory after the first configuration?

Thank you for the answer @ cudawarped

My operating system is WIN 10.
OpenCV version 4.8.
I am using CMAKE GUI.
For each build, I am starting with an empty build folder.

I you look at this guide I wrote

you can see the exact CMake configuration commands I used. I will check now against the latest version of OpenCV to make sure the installation still works.

Note I always use the command line and not the GUI that way I can easily share my CMake configuration commands with others when I encounter issues/bugs.

I just want to note that I am building OpenCV from source for a PC that does note have GPU.
I am only changing the settings that I mention at the beginning of the topic.

best regards

On that page I just copied the Without CUDA command line

"C:\Program Files\CMake\bin\cmake.exe" -B"PATH_TO_BUILD_DIR" -H"PATH_TO_OPENCV_SOURCE" -DOPENCV_EXTRA_MODULES_PATH="PATH_TO_OPENCV_CONTRIB_MODULES" -G"Visual Studio 17 2022" -DINSTALL_TESTS=ON -DINSTALL_C_EXAMPLES=ON -DBUILD_EXAMPLES=ON -DBUILD_opencv_python3=ON -DPYTHON3_INCLUDE_DIR=PATH_TO_PYTHON_DIST/include -DPYTHON3_LIBRARY=PATH_TO_PYTHON_DIST/libs/python%pyVer%.lib -DPYTHON3_EXECUTABLE=PATH_TO_PYTHON_DIST/python.exe -DPYTHON3_NUMPY_INCLUDE_DIRS=PATH_TO_PYTHON_DIST/lib/site-packages/numpy/core/include -DPYTHON3_PACKAGES_PATH=PATH_TO_PYTHON_DIST/Lib/site-packages/

adding -DBUILD_opencv_world=ON and filling in the parameters as specified and it built OpenCV world and installed the python bindings to the site-packages directory.

I can then import cv2 without any errors. I even deleted the cv2 directory and installed a second time and everything still worked so I’m not sure what your issue is, maybe its because you don’t specify PYTHON3_PACKAGES_PATH. If you need futher assistance use the CMake command line and post the exact arguments you are using including the ouput from the configuration stage.

Hello @ cudawarped

Thank you very much for your effort. I will try with

DPYTHON3_NUMPY_INCLUDE_DIRS …
DPYTHON3_PACKAGES_PATH …

and give feedback.

Please note that in your configuration settings

OPENCV_ENABLE_NONFREE ON
BUILD_SHARED_LIBS OFF

are not set.

If I do not get the same result with you I will check with command line.

best regards

Hello @ cudawarped

Using the command line, I tried to build opencv from source as you suggested.
I observe that config-3.10.py is being generated.

However, the number of the folders in cv2 folder is 4.
When I built with “BUILD_opencv_world OFF” the number of folders in the cv2 folder was 61.
Should DPYTHON3_PACKAGES_PATH point the folder in my environment or can I select any folder?

Another thing to note is that there is no DBUILD_opencv_python3 property in CMAKE GUI.
In the command line, if I remove -DBUILD_opencv_python3=ON config-3.10.py is not generated.

PYTHON3_PACKAGES_PATH should point to the site-packages directory containing cv2.

That’s correct.

Actually, site-packages directory does not contain cv2 module(folder) initially since the environment does not contain opencv before built.
After building from source code, cv2 folder is generated.

Is there any misunderstanding?

It appears so. If you haven’t built or installed python then you are correct there will not be a cv2 directory.

To be more precise.

PYTHON3_PACKAGES_PATH should point to the site-packages directory.

For the builts with BUILD_SHARED_LIBS OFF, I get linker errors.
When I add all .lib files to Additional Dependencies in Visual Studio Linker configurations the number of linker errors I get decreases. However, the number of linker errors does not become 0.

For the builts with BUILD_SHARED_LIBS OFF, should I add more configuration parameters for cmake?

Here is the command that I used

“C:\Program Files\CMake\bin\cmake.exe” ^
-B"D:\openCV_from_source\build13" ^
-H"D:\openCV_from_source\opencv-4.8.0" ^
-DOPENCV_EXTRA_MODULES_PATH=“D:\openCV_from_source\opencv_contrib-4.8.0\modules” ^
-G"Visual Studio 17 2022" ^
-DBUILD_opencv_world=ON ^
-DBUILD_SHARED_LIBS=OFF ^
-DOPENCV_ENABLE_NONFREE=ON

My operating system is WIN 10.
OpenCV version is 4.8.

Can you post using preformated text to avoid the annoying brackets which I can’t directly copy, i.e.

"brackets"

not

“brackets”

Can you post your CMake output and the errors you are getting.

Hello @ cudawarped

Thank you for the answer

"C:\Program Files\CMake\bin\cmake.exe" -B"D:\openCV_from_source\build14" -H"D:\openCV_from_source\opencv-4.8.0" -DOPENCV_EXTRA_MODULES_PATH="D:\openCV_from_source\opencv_contrib-4.8.0\modules" -G"Visual Studio 17 2022" -DBUILD_opencv_world=OFF -DBUILD_SHARED_LIBS=OFF -DOPENCV_ENABLE_NONFREE=ON

The linker errors are too long to paste.
I can paste Cmake output a few hours later

I don’t have any issues on Windows 11 building the latest commit of OpenCV from the 4.x branch with those commands, maybe the CMake config output and the first linker errors you encounter will shed some light on the issue.

Hello @ cudawarped

Thank you for the answer

What about Visual Studio settings?
I am setting include directory and lib directory.
For debug and release I am setting proper world .lib files.
Should I do more settings for BUILD_SHARED_LIBS=OFF?

There are similar discussions without a definite answer.

Hello @ cudawarped

Thank you for the answer.

So eventhough I use .lib files do I still need some opencv .dll?
If so maybe my error is related to not changing the environment variables.
This would be nice because it means no error in the built process.
However, when I observe the bin folder under install there are a couple of .exe files but there is only one .dll which is opencv_videoio_ffmpeg480_64.dll

For Python bindings, the following configuration is fine

“C:\Program Files\CMake\bin\cmake.exe” -B"D:\openCV_from_source\build16" -H"D:\openCV_from_source\opencv-4.8.0" -DOPENCV_EXTRA_MODULES_PATH=“D:\openCV_from_source\opencv_contrib-4.8.0\modules” -G"Visual Studio 17 2022" -DBUILD_opencv_python3=ON -DPYTHON3_INCLUDE_DIR=C:/Users/THEUSER/.conda/envs/env3_10/include -DPYTHON3_LIBRARY=C:/Users/THEUSER/.conda/envs/env3_10/libs/python310.lib -DPYTHON3_EXECUTABLE=C:/Users/THEUSER/.conda/envs/env3_10/python.exe -DPYTHON3_NUMPY_INCLUDE_DIRS=C:/Users/THEUSER/.conda/envs/env3_10/Lib/site-packages/numpy/core/include -DPYTHON3_PACKAGES_PATH=C:/Users/THEUSER/.conda/envs/env3_10/Lib/site-packages -DBUILD_opencv_world=ON -DBUILD_SHARED_LIBS=OFF -DOPENCV_ENABLE_NONFREE=ON

The configuration is also fine for C++ release.
However, for C++ debug I get the error indicating that python310_d.lib is not accessible.
That is because conda environments does not include python debug libraries.
I used pure Python installer(not conda) and selected to generate debug libraries.
Somehow the same file is not found by the installer.
Since I use conda as development environment I decided to go for 2 opencv from source builts.
One for python and one for C++(both debug and release).
Having 2 saperate builts does not hurt in my opinion.

Ignore my previous answer (which I’ve deleted for clarity). I misread your post.

Yes if you want to read media file with VideoCapture and the FFMpeg backend. OpenCV uses plugins for this.

It would be helpful to understand where you are coming from if you posted the error.

This is fine but you can easily work around this and use a debug build for both by modifying two lines in your pyconfig.h as described in the guide I wrote.