How to Set the Root Directory for Visual Studio Code Python Extension?

Learn how to set the correct root directory in Visual Studio Code to ensure Python imports, debugging, and environments work smoothly in your workspace.

Setting the correct root directory ensures the Python extension in VS Code works smoothly for linting, IntelliSense, debugging, and environment detection. Here are two ways to set or manage the root directory in VS Code for Python development.

Method 1: Open the Correct Folder in VS Code

Open the folder that contains your main.py, .venv or requirements.txt as the root directory.

Step 1: Go to File > Open Folder

Always open your top-level project folder — not just a file. This sets VS Code’s ${workspaceFolder} properly.

Step 2: Avoid Opening Just a File

If you open a single .py file or a nested folder, the Python extension might not detect paths, environments or modules correctly.

Step 3: Confirm VS Code Detects Your Root

Once the folder is opened, VS Code treats it as the root and applies settings, interpreter paths and debugging configurations relative to it. Opening the full folder ensures all paths, imports and virtual environments work as expected.

Method 2: Set Root Manually Using VS Code Settings

This is useful if your main code is inside a subdirectory like src/, or if you’re using a multi-folder workspace.

Step 1: Create or Edit .vscode/settings.json

Inside your root project folder, create or open the .vscode/settings.json and add:
{  "python.analysis.extraPaths": ["./src"]}
This helps the language server (Pylance) recognize the src/ folder for imports.
Also Read: Pros & Cons of Python Web Development in 2025

Step 2: Select the Correct Interpreter

Press Ctrl + Shift + PPython: Select Interpreter → choose your virtual environment, like .venv/bin/python.

Step 3: Update launch.json (Optional for Debugging)

If you’re running files inside a subfolder, set the working directory:
{  "version": "0.2.0",   "configurations": [     {       "name": "Python: Current File",       "type": "python",       "request": "launch",       "program": "${file}",       "cwd": "${workspaceFolder}/src"     }   ] }
This is essential when debugging scripts that live in nested folders.

Bonus Method: Use .env to Set PYTHONPATH (Optional)

This doesn’t change the root folder but lets Python find modules in custom paths.

Step 1: Create a .env File

Put it in your root folder and add:
PYTHONPATH=./src

Step 2: Link .env in Settings

Add this to your .vscode/settings.json:
{  "python.envFile": "${workspaceFolder}/.env" }
Useful when you want to manage import paths without restructuring your folders.

Tip

If your code lives in a subfolder (like src/), set "python.analysis.extraPaths" and "cwd" in .vscode to avoid unresolved import errors during editing and debugging.
Related

The demand for Python skills in Europe is growing as companies adopt AI driven solutions, enterprise platforms and interactive web applications. Businesses need developers who…

20 Jan, 2026

Amazon CloudWatch allows you to run log insights queries using the logs client in Boto3. Below is a step by step guide to querying logs…

28 Oct, 2025

The Kalman Filter is an efficient recursive algorithm used to estimate the state of a system from noisy measurements. In computer vision, it’s commonly used…

21 Oct, 2025