6 Nov. 2025
Make VS Code Python Imports Behave Like They Do in PyCharm
If, like me, you started your Python journey in PyCharm, you probably got spoiled by how “just working” the imports feel. You can freely import modules using the project root as the base — no fiddling with sys.path.append() or messy relative imports.
Then you switch to VS Code, and suddenly your imports start throwing ModuleNotFoundError at runtime. That’s because, by default, VS Code doesn’t automatically treat your project’s root folder as part of the Python import path.
Fortunately, you can fix this easily and make VS Code behave a lot more like PyCharm.
TL;DR
Create a .vscode/settings.json in the root of your project with content as follows:
{
"python.terminal.executeInFileDir": false,
"python.terminal.cwd": "${workspaceFolder}",
"terminal.integrated.env.windows": {
"PYTHONPATH": "${workspaceFolder}"
},
"terminal.integrated.env.linux": {
"PYTHONPATH": "${workspaceFolder}"
},
"terminal.integrated.env.osx": {
"PYTHONPATH": "${workspaceFolder}"
}
}
Then close and reopen your terminal in VS Code to apply the changes.
If You are Interested, Here is What This Actually Does
Let’s break down what’s going on here:
-
python.terminal.executeInFileDir: false Normally, VS Code runs Python scripts from the directory that contains the file. Setting this tofalsemeans it will instead use the workspace root (your project folder) as the working directory — just like PyCharm does. -
python.terminal.cwd: "${workspaceFolder}" This ensures your terminal always starts in the workspace’s root directory when you open it inside VS Code. -
PYTHONPATH: "${workspaceFolder}" This is the magic part. By setting thePYTHONPATHenvironment variable, you tell Python:“Treat my project’s root folder as part of
sys.path.” That means you can use imports relative to the project root no matter where your file is located. For example, you can do this from anywhere in your project:from utils.helpers import do_somethinginstead of:
from ..utils.helpers import do_something
The settings are duplicated under windows, linux, and osx so that this works consistently across all operating systems.
Bonus Tip
If you’re using VS Code’s Python debugger, make sure your launch configuration (.vscode/launch.json) also uses the workspace folder as the working directory:
{
"configurations": [
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"cwd": "${workspaceFolder}"
}
]
}
This keeps both the terminal and debugger consistent.