VS Code guide

OpenCV in VS Code on Windows — Python & C++ setup guide

Configure OpenCV in Visual Studio Code on Windows. Python interpreter selection for IntelliSense, C++ include paths in c_cpp_properties.json and build tasks.

This guide covers Python OpenCV in VS Code. For C++ IntelliSense with OpenCV see the C++ extension configuration section below.

OpenCV Python in VS Code

  • 1

    Install the Python extension

    Open VS Code → Extensions (Ctrl+Shift+X) → search Python → install the Microsoft Python extension.

  • 2

    Select the correct Python interpreter

    Press Ctrl+Shift+P → type Python: Select Interpreter → choose the Python where OpenCV is installed. You should see opencv-python in the list if it is installed there.

  • 3

    Install OpenCV in that environment

    VS Code Terminal (Ctrl+`)
    C:\> pip install opencv-python
    # Or use the venv from the workspace:
    C:\> python -m venv .venv
    C:\> .venv\Scripts\activate
    (.venv) C:\> pip install opencv-python
  • 4

    Verify IntelliSense works

    Create a new file test.py, type import cv2 then cv2. — you should see autocomplete suggestions. If not, reload VS Code window: Ctrl+Shift+P → Developer: Reload Window.

OpenCV C++ IntelliSense in VS Code

For C++ projects, configure the c_cpp_properties.json file to point at the OpenCV include directory:

.vscode/c_cpp_properties.json
{
"configurations": [{
"name": "Win32",
"includePath": [
"${workspaceFolder}/**",
"C:/opencv/build/include",
"C:/opencv/build/include/opencv2"
],
"compilerPath": "C:/Program Files/Microsoft Visual Studio/2022/Community/VC/Tools/MSVC/14.39.33519/bin/Hostx64/x64/cl.exe",
"intelliSenseMode": "windows-msvc-x64"
}],
"version": 4
}

Build and run C++ from VS Code

.vscode/tasks.json
{
"tasks": [{
"type": "cppbuild",
"label": "Build OpenCV C++",
"command": "cl.exe",
"args": [
"/I", "C:/opencv/build/include",
"${file}",
"/link", "C:/opencv/build/x64/vc16/lib/opencv_world4120.lib",
"/LIBPATH:C:/opencv/build/x64/vc16/lib"
]
}]
}

VS Code questions

import cv2 works in terminal but fails in VS Code
VS Code is using a different Python interpreter than your terminal. Press Ctrl+Shift+P → "Python: Select Interpreter" and choose the Python where OpenCV is installed. Check the interpreter shown in the bottom status bar.
cv2 shows as unresolved import in VS Code
This is a type-stub issue, not a runtime error. Install the stubs: pip install opencv-stubs. VS Code Pylance will use these for autocomplete. Alternatively select the correct interpreter and the issue usually resolves itself.
C++ IntelliSense shows errors but code compiles fine
The c_cpp_properties.json include path is not set. Make sure the OpenCV include directory is in includePath. Also check intelliSenseMode is windows-msvc-x64 for Visual Studio compiler.

Need to set up C++ build?

Full Visual Studio configuration with linker settings.

C++ guide