Python: No module named 'cv2' when building OpenCV various versions from sources on Win11

We have now built opencv and opencv_contrib from source.After that, I added . bin to the environment variable and verified that the path from cmd was working. However, when I try to import cv2 from python, I get a ModuleNotFoundError. How can I use cv2 from python?

I have tried building with multiple versions of OpenCV and always get the same problem.

CMake and Visual Studio 2022 were used for the build.

ok, step by step:

please (re)run cmake, show us the output

Thanks for getting back to me !!!
Here is my CMake output.

-- General configuration for OpenCV 4.8.0 =====================================
--   Version control:               unknown
--
--   Platform:
--     Timestamp:                   2024-01-22T06:45:54Z
--     Host:                        Windows 10.0.22621 AMD64
--     CMake:                       3.28.1
--     CMake generator:             Visual Studio 17 2022
--     CMake build tool:            C:/Program Files/Microsoft Visual Studio/2022/Community/MSBuild/Current/Bin/amd64/MSBuild.exe
--     MSVC:                        1938
--     Configuration:               Debug Release
--
--   CPU/HW features:
--     Baseline:                    SSE SSE2 SSE3
--       requested:                 SSE3
--     Dispatched code generation:  SSE4_1 SSE4_2 FP16 AVX AVX2 AVX512_SKX
--       requested:                 SSE4_1 SSE4_2 AVX FP16 AVX2 AVX512_SKX
--       SSE4_1 (18 files):         + SSSE3 SSE4_1
--       SSE4_2 (2 files):          + SSSE3 SSE4_1 POPCNT SSE4_2
--       FP16 (1 files):            + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 AVX
--       AVX (8 files):             + SSSE3 SSE4_1 POPCNT SSE4_2 AVX
--       AVX2 (37 files):           + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 FMA3 AVX AVX2
--       AVX512_SKX (8 files):      + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 FMA3 AVX AVX2 AVX_512F AVX512_COMMON AVX512_SKX
--
--   C/C++:
--     Built as dynamic libs?:      YES
--     C++ standard:                11
--     C++ Compiler:                C:/Program Files/Microsoft Visual Studio/2022/Community/VC/Tools/MSVC/14.38.33130/bin/Hostx64/x64/cl.exe  (ver 19.38.33134.0)
--     C++ flags (Release):         /DWIN32 /D_WINDOWS /W4 /GR  /D _CRT_SECURE_NO_DEPRECATE /D _CRT_NONSTDC_NO_DEPRECATE /D _SCL_SECURE_NO_WARNINGS /Gy /bigobj /Oi  /fp:precise     /EHa /wd4127 /wd4251 /wd4324 /wd4275 /wd4512 /wd4589 /wd4819 /MP  /MD /O2 /Ob2 /DNDEBUG
--     C++ flags (Debug):           /DWIN32 /D_WINDOWS /W4 /GR  /D _CRT_SECURE_NO_DEPRECATE /D _CRT_NONSTDC_NO_DEPRECATE /D _SCL_SECURE_NO_WARNINGS /Gy /bigobj /Oi  /fp:precise     /EHa /wd4127 /wd4251 /wd4324 /wd4275 /wd4512 /wd4589 /wd4819 /MP  /MDd /Zi /Ob0 /Od /RTC1
--     C Compiler:                  C:/Program Files/Microsoft Visual Studio/2022/Community/VC/Tools/MSVC/14.38.33130/bin/Hostx64/x64/cl.exe
--     C flags (Release):           /DWIN32 /D_WINDOWS /W3  /D _CRT_SECURE_NO_DEPRECATE /D _CRT_NONSTDC_NO_DEPRECATE /D _SCL_SECURE_NO_WARNINGS /Gy /bigobj /Oi  /fp:precise     /MP   /MD /O2 /Ob2 /DNDEBUG
--     C flags (Debug):             /DWIN32 /D_WINDOWS /W3  /D _CRT_SECURE_NO_DEPRECATE /D _CRT_NONSTDC_NO_DEPRECATE /D _SCL_SECURE_NO_WARNINGS /Gy /bigobj /Oi  /fp:precise     /MP /MDd /Zi /Ob0 /Od /RTC1
--     Linker flags (Release):      /machine:x64  /INCREMENTAL:NO
--     Linker flags (Debug):        /machine:x64  /debug /INCREMENTAL
--     ccache:                      NO
--     Precompiled headers:         YES
--     Extra dependencies:
--     3rdparty dependencies:
--
--   OpenCV modules:
--     To be built:                 calib3d core dnn features2d flann gapi highgui imgcodecs imgproc ml objdetect photo stitching ts video videoio
--     Disabled:                    world
--     Disabled by dependency:      -
--     Unavailable:                 java python2 python3
--     Applications:                tests perf_tests apps
--     Documentation:               NO
--     Non-free algorithms:         NO
--
--   Windows RT support:            NO
--
--   GUI:                           WIN32UI
--     Win32 UI:                    YES
--     VTK support:                 NO
--
--   Media I/O:
--     ZLib:                        build (ver 1.2.13)
--     JPEG:                        build-libjpeg-turbo (ver 2.1.3-62)
--       SIMD Support Request:      YES
--       SIMD Support:              YES
--     WEBP:                        build (ver encoder: 0x020f)
--     PNG:                         build (ver 1.6.37)
--     TIFF:                        build (ver 42 - 4.2.0)
--     JPEG 2000:                   build (ver 2.5.0)
--     OpenEXR:                     build (ver 2.3.0)
--     HDR:                         YES
--     SUNRASTER:                   YES
--     PXM:                         YES
--     PFM:                         YES
--
--   Video I/O:
--     DC1394:                      NO
--     FFMPEG:                      YES (prebuilt binaries)
--       avcodec:                   YES (58.134.100)
--       avformat:                  YES (58.76.100)
--       avutil:                    YES (56.70.100)
--       swscale:                   YES (5.9.100)
--       avresample:                YES (4.0.0)
--     GStreamer:                   NO
--     DirectShow:                  YES
--     Media Foundation:            YES
--       DXVA:                      YES
--
--   Parallel framework:            Concurrency
--
--   Trace:                         YES (with Intel ITT)
--
--   Other third-party libraries:
--     Intel IPP:                   2021.8 [2021.8.0]
--            at:                   C:/Users/tanak/Documents/build/3rdparty/ippicv/ippicv_win/icv
--     Intel IPP IW:                sources (2021.8.0)
--               at:                C:/Users/tanak/Documents/build/3rdparty/ippicv/ippicv_win/iw
--     Lapack:                      NO
--     Eigen:                       NO
--     Custom HAL:                  NO
--     Protobuf:                    build (3.19.1)
--     Flatbuffers:                 builtin/3rdparty (23.5.9)
--
--   OpenCL:                        YES (NVD3D11)
--     Include path:                C:/opencv-4.8.0/3rdparty/include/opencl/1.2
--     Link libraries:              Dynamic load
--
--   Python (for build):            C:/Users/tanak/AppData/Local/Microsoft/WindowsApps/python3.exe
--
--   Java:
--     ant:                         NO
--     Java:                        NO
--     JNI:                         NO
--     Java wrappers:               NO
--     Java tests:                  NO
--
--   Install to:                    C:/Users/tanak/Documents/build/install
-- -----------------------------------------------------------------

Now, I found that Python was missing at the point of CMake output.
How should I change my CMake settings? The current settings are shown below.

cmake -S C:/opencv-4.8.0 -B build ^
-G "Visual Studio 17 2022" -A x64 -T host=x64 ^
-D CMAKE_CONFIGURATION_TYPES="Release" ^
-D CMAKE_BUILD_TYPE="Release" ^
-D WITH_CUDA=ON ^
-D OPENCV_DNN_CUDA=OFF ^
-D CMAKE_INSTALL_PREFIX=C:/opencv_build ^
-D CUDA_FAST_MATH=ON ^
-D ENABLE_FAST_MATH=ON ^
-D OPENCV_EXTRA_MODULES_PATH=C:/opencv_contrib-4.8.0/modules ^
-D INSTALL_TESTS=OFF ^
-D INSTALL_C_EXAMPLES=OFF ^
-D BUILD_EXAMPLES=OFF ^
-D BUILD_TESTS=OFF ^
-D BUILD_PERF_TESTS=OFF ^
-D BUILD_JPEG_TURBO_DISABLE=ON ^
-D WITH_PYTHON=ON ^
-D INSTALL_PYTHON_EXAMPLES=OFF ^
-D BUILD_opencv_python2=OFF ^
-D BUILD_opencv_python3=ON ^
-D PYTHON_DEFAULT_EXECUTABLE=C:/Users/tanak/anaconda3/python.exe ^
-D PYTHON3_EXECUTABLE=C:/Users/tanak/anaconda3/python.exe ^
-D PYTHON3_INCLUDE_DIR=C:/Users/tanak/anaconda3/include ^
-D PYTHON3_LIBRARY=C:/Users/tanak/anaconda3/libs/python311.lib ^
-D PYTHON3_NUMPY_INCLUDE_DIRS=C:/Users/tanak/anaconda3/Lib/site-packages/numpy/core/include ^
-D PYTHON3_PACKAGES_PATH=C:/Users/tanak/anaconda3/Lib/site-packages ^
-D WITH_CUBLAS=ON ^
-D WITH_OPENGL=ON ^
-D WITH_CUDNN=ON ^
-D WITH_NVCUVID=ON ^
-D OPENCV_ENABLE_NONFREE=ON ^
-D OPENCV_PYTHON3_VERSION=3.11 ^
-D CPU_BASELINE="SSE3" ^
-D CUDA_GENERATION="Turing" ^
-D CUDA_ARCH_PTX=8.6 ^
-D BUILD_opencv_world=ON

hmm, may i ask you, to delete build/CmakeCache.txt , and run cmake all again ?

there’s a lot of tests / smallprint before the final report above, maybe we can see, what it hates about your anaconda311 (!) install