# Opencv Build From Source Python binding problem

**URL:** https://forum.opencv.org/t/opencv-build-from-source-python-binding-problem/14919
**Category:** Python
**Tags:** build
**Created:** [October 19, 2023, 6:59am UTC](https://forum.opencv.org/t/opencv-build-from-source-python-binding-problem/14919 "2023-10-19T06:59:37Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![mfatih7](https://avatars.discourse-cdn.com/v4/letter/m/e5b9ba/32.png) [@mfatih7](https://forum.opencv.org/u/mfatih7)
#### Post date: [October 19, 2023, 6:59am UTC](https://forum.opencv.org/t/opencv-build-from-source-python-binding-problem/14919/1 "2023-10-19T06:59:37Z")

</div>

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

---

<div class="post-metadata">

### Author: ![berak](https://avatars.discourse-cdn.com/v4/letter/b/85f322/32.png) [@berak](https://forum.opencv.org/u/berak)
#### Post date: [October 19, 2023, 8:08am UTC](https://forum.opencv.org/t/opencv-build-from-source-python-binding-problem/14919/2 "2023-10-19T08:08:27Z")

</div>

opencv version ? os ?

> [@mfatih7](#):
>
> BUILD\_opencv\_world ON

so, this makes all the difference ?  
if it leads to a wrong configuration, it probably [should get reported](https://github.com/opencv/opencv/issues)

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

---

<div class="post-metadata">

### Author: ![mfatih7](https://avatars.discourse-cdn.com/v4/letter/m/e5b9ba/32.png) [@mfatih7](https://forum.opencv.org/u/mfatih7)
#### Post date: [October 19, 2023, 8:25am UTC](https://forum.opencv.org/t/opencv-build-from-source-python-binding-problem/14919/3 "2023-10-19T08:25:00Z")

</div>

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

---

<div class="post-metadata">

### Author: ![cudawarped](https://avatars.discourse-cdn.com/v4/letter/c/9dc877/32.png) [@cudawarped](https://forum.opencv.org/u/cudawarped)
#### Post date: [October 19, 2023, 8:54am UTC](https://forum.opencv.org/t/opencv-build-from-source-python-binding-problem/14919/4 "2023-10-19T08:54:50Z")

</div>

> [@mfatih7](#):
>
> 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?

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?

---

<div class="post-metadata">

### Author: ![mfatih7](https://avatars.discourse-cdn.com/v4/letter/m/e5b9ba/32.png) [@mfatih7](https://forum.opencv.org/u/mfatih7)
#### Post date: [October 19, 2023, 9:29am UTC](https://forum.opencv.org/t/opencv-build-from-source-python-binding-problem/14919/5 "2023-10-19T09:29:06Z")

</div>

Thank you for the answer @ [cudawarped](https://forum.opencv.org/u/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.

---

<div class="post-metadata">

### Author: ![cudawarped](https://avatars.discourse-cdn.com/v4/letter/c/9dc877/32.png) [@cudawarped](https://forum.opencv.org/u/cudawarped)
#### Post date: [October 19, 2023, 9:42am UTC](https://forum.opencv.org/t/opencv-build-from-source-python-binding-problem/14919/6 "2023-10-19T09:42:08Z")

</div>

I you look at this guide I wrote

> **[Build OpenCV (including Python) with CUDA on Windows: Comprehensive Guide...](https://www.jamesbowley.co.uk/qmd/opencv_cuda_python_windows.html#building-opencv-with-cmake)**
>
> Guide to building OpenCV (including Python bindings) with CUDA (optionally the Nvidia Video Codec SDK and cuDNN) from within Visual Studio or from the command line using the Ninja build system.

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.

---

<div class="post-metadata">

### Author: ![mfatih7](https://avatars.discourse-cdn.com/v4/letter/m/e5b9ba/32.png) [@mfatih7](https://forum.opencv.org/u/mfatih7)
#### Post date: [October 19, 2023, 10:01am UTC](https://forum.opencv.org/t/opencv-build-from-source-python-binding-problem/14919/7 "2023-10-19T10:01:32Z")

</div>

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

---

<div class="post-metadata">

### Author: ![cudawarped](https://avatars.discourse-cdn.com/v4/letter/c/9dc877/32.png) [@cudawarped](https://forum.opencv.org/u/cudawarped)
#### Post date: [October 19, 2023, 1:00pm UTC](https://forum.opencv.org/t/opencv-build-from-source-python-binding-problem/14919/8 "2023-10-19T13:00:40Z")

</div>

> [@mfatih7](#):
>
> I just want to note that I am building OpenCV from source for a PC that does note have GPU.

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.

---

<div class="post-metadata">

### Author: ![mfatih7](https://avatars.discourse-cdn.com/v4/letter/m/e5b9ba/32.png) [@mfatih7](https://forum.opencv.org/u/mfatih7)
#### Post date: [October 19, 2023, 1:33pm UTC](https://forum.opencv.org/t/opencv-build-from-source-python-binding-problem/14919/9 "2023-10-19T13:33:27Z")

</div>

Hello @ [cudawarped](https://forum.opencv.org/u/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

---

<div class="post-metadata">

### Author: ![mfatih7](https://avatars.discourse-cdn.com/v4/letter/m/e5b9ba/32.png) [@mfatih7](https://forum.opencv.org/u/mfatih7)
#### Post date: [October 23, 2023, 6:48am UTC](https://forum.opencv.org/t/opencv-build-from-source-python-binding-problem/14919/10 "2023-10-23T06:48:45Z")

</div>

Hello @ [cudawarped](https://forum.opencv.org/u/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.

---

<div class="post-metadata">

### Author: ![cudawarped](https://avatars.discourse-cdn.com/v4/letter/c/9dc877/32.png) [@cudawarped](https://forum.opencv.org/u/cudawarped)
#### Post date: [October 23, 2023, 8:07am UTC](https://forum.opencv.org/t/opencv-build-from-source-python-binding-problem/14919/11 "2023-10-23T08:07:54Z")

</div>

> [@mfatih7](#):
>
> 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?

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

> [@mfatih7](#):
>
> 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.

That’s correct.

---

<div class="post-metadata">

### Author: ![mfatih7](https://avatars.discourse-cdn.com/v4/letter/m/e5b9ba/32.png) [@mfatih7](https://forum.opencv.org/u/mfatih7)
#### Post date: [October 23, 2023, 8:33am UTC](https://forum.opencv.org/t/opencv-build-from-source-python-binding-problem/14919/12 "2023-10-23T08:33:45Z")

</div>

> [@cudawarped](#):
>
> `PYTHON3_PACKAGES_PATH` should point to the **site-packages** directory containing **cv2**.

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?

---

<div class="post-metadata">

### Author: ![cudawarped](https://avatars.discourse-cdn.com/v4/letter/c/9dc877/32.png) [@cudawarped](https://forum.opencv.org/u/cudawarped)
#### Post date: [October 23, 2023, 8:41am UTC](https://forum.opencv.org/t/opencv-build-from-source-python-binding-problem/14919/13 "2023-10-23T08:41:28Z")

</div>

> [@mfatih7](#):
>
> 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.

---

<div class="post-metadata">

### Author: ![mfatih7](https://avatars.discourse-cdn.com/v4/letter/m/e5b9ba/32.png) [@mfatih7](https://forum.opencv.org/u/mfatih7)
#### Post date: [October 26, 2023, 11:12am UTC](https://forum.opencv.org/t/opencv-build-from-source-python-binding-problem/14919/14 "2023-10-26T11:12:33Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![cudawarped](https://avatars.discourse-cdn.com/v4/letter/c/9dc877/32.png) [@cudawarped](https://forum.opencv.org/u/cudawarped)
#### Post date: [October 26, 2023, 11:44am UTC](https://forum.opencv.org/t/opencv-build-from-source-python-binding-problem/14919/15 "2023-10-26T11:44:00Z")

</div>

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

```auto
"brackets"

```

not

> “brackets”

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

---

<div class="post-metadata">

### Author: ![mfatih7](https://avatars.discourse-cdn.com/v4/letter/m/e5b9ba/32.png) [@mfatih7](https://forum.opencv.org/u/mfatih7)
#### Post date: [October 26, 2023, 12:18pm UTC](https://forum.opencv.org/t/opencv-build-from-source-python-binding-problem/14919/16 "2023-10-26T12:18:33Z")

</div>

Hello @ [cudawarped](https://forum.opencv.org/u/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

---

<div class="post-metadata">

### Author: ![cudawarped](https://avatars.discourse-cdn.com/v4/letter/c/9dc877/32.png) [@cudawarped](https://forum.opencv.org/u/cudawarped)
#### Post date: [October 26, 2023, 2:12pm UTC](https://forum.opencv.org/t/opencv-build-from-source-python-binding-problem/14919/17 "2023-10-26T14:12:19Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![mfatih7](https://avatars.discourse-cdn.com/v4/letter/m/e5b9ba/32.png) [@mfatih7](https://forum.opencv.org/u/mfatih7)
#### Post date: [October 26, 2023, 4:34pm UTC](https://forum.opencv.org/t/opencv-build-from-source-python-binding-problem/14919/18 "2023-10-26T16:34:08Z")

</div>

Hello @ [cudawarped](https://forum.opencv.org/u/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.

> <https://stackoverflow.com/questions/55242450/linking-opencv-4-1-0-include-works-libs-doesnt>

> **[after turning BUILD\_SHARED\_LIBS to OFF , there are too many undefined...](https://answers.opencv.org/question/198721/after-turning-build_shared_libs-to-off-there-are-too-many-undefined-references/?answer=198727)**
>
> previously during dynamic linking of opencv , I was using 'pkg-config --cflags --libs opencv' for linking, but after I rebuild my opencv with flag BUILD\_SHARED\_LIBS to OFF, pkg-config --cflags --libs opencv is giving too much undefined reference...

---

<div class="post-metadata">

### Author: ![mfatih7](https://avatars.discourse-cdn.com/v4/letter/m/e5b9ba/32.png) [@mfatih7](https://forum.opencv.org/u/mfatih7)
#### Post date: [October 26, 2023, 7:21pm UTC](https://forum.opencv.org/t/opencv-build-from-source-python-binding-problem/14919/20 "2023-10-26T19:21:51Z")

</div>

Hello @ [cudawarped](https://forum.opencv.org/u/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.

---

<div class="post-metadata">

### Author: ![cudawarped](https://avatars.discourse-cdn.com/v4/letter/c/9dc877/32.png) [@cudawarped](https://forum.opencv.org/u/cudawarped)
#### Post date: [October 27, 2023, 5:00am UTC](https://forum.opencv.org/t/opencv-build-from-source-python-binding-problem/14919/21 "2023-10-27T05:00:24Z")

</div>

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

> [@mfatih7](#):
>
> So eventhough I use .lib files do I still need some opencv .dll?

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

> [@mfatih7](#):
>
> If so maybe my error is related to not changing the environment variables.

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

> [@mfatih7](#):
>
> 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.

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.

> **[Build OpenCV (including Python) with CUDA on Windows: Comprehensive Guide...](https://www.jamesbowley.co.uk/qmd/opencv_cuda_python_windows.html#generate-python-bindings-for-a-debug-build)**
>
> Guide to building OpenCV (including Python bindings) with CUDA (optionally the Nvidia Video Codec SDK and cuDNN) from within Visual Studio or from the command line using the Ninja build system.

[Next page](https://forum.opencv.org/t/opencv-build-from-source-python-binding-problem/14919.md?page=2)
