Learn how to set the correct root directory in Visual Studio Code to ensure Python imports, debugging, and environments work smoothly in your workspace.
Scalable, Secure, and High-Performance Solutions for Your Business.
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.
Open the folder that contains your main.py, .venv or requirements.txt as the root directory.
Always open your top-level project folder — not just a file. This sets VS Code’s ${workspaceFolder} properly.
If you open a single .py file or a nested folder, the Python extension might not detect paths, environments or modules correctly.
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.
This is useful if your main code is inside a subdirectory like src/, or if you’re using a multi-folder workspace.
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.
Press Ctrl + Shift + P → Python: Select Interpreter → choose your virtual environment, like .venv/bin/python.
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.
This doesn’t change the root folder but lets Python find modules in custom paths.
Put it in your root folder and add:
Add this to your .vscode/settings.json:
{ “python.envFile”: “${workspaceFolder}/.env”
}
Useful when you want to manage import paths without restructuring your folders.
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.
eSparkBiz is rated 4.9 Stars
Real People, Real Stories
See why 300+ startups & enterprises trust eSparkBiz with their software outsourcingYou can copy files in python using the shutil module which provides high level file and collection of files operations. You can also combine it…
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…
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…
Let’s discuss how our dedicated experts can help.