This worked for me:-

in your launch.json profile entry, specify a new entry called "env", and set PYTHONPATH yourself.

"configurations": [
    {
        "name": "Python",
        "type": "python",
        "stopOnEntry": false,
        "request": "launch",
        "pythonPath": "${config.python.pythonPath}",
        "program": "${file}",
        "cwd": "${workspaceRoot}",
        "debugOptions": [
            "WaitOnAbnormalExit",
            "WaitOnNormalExit",
            "RedirectOutput"
        ],
        "env": {
            "PYTHONPATH": "/path/a:path/b"
        }
    }
]
Answer from malbs on Stack Overflow
🌐
Reddit
reddit.com › r/vscode › made a small vscode extension to add folders to python path in just a click
r/vscode on Reddit: Made a small VSCode extension to add folders to python path in just a click
August 15, 2021 -

Adding folders to the python path is widely needed if you need to import stuff from folders outside the script's root folder. PyCharm has a nice option of right-click on the folder and select "Mark Directory As > Sources Root". In VSCode, you have to manually create a .env file and then add the paths of the folders to that file. I have created a small extension that pretty much does what PyCharm does. Install the extension, right-click on any folder from the Explorer and then select "Add to Python path". The extension is called py-add-to-path and can be downloaded from within the VSCode.

🌐
Fabian Lee
fabianlee.org › 2021 › 12 › 23 › vscode-add-a-directory-to-the-terminal-path
VSCode: Add a directory to the terminal PATH | Fabian Lee : Software Engineer
January 4, 2022 - Add your directory path, then a colon and “${env:PATH}” which refers to your system PATH. For other OS flavors, change “.linux” to “.windows” or “.osx”. If you are on Windows, the directory paths are separated by semicolons (not ...
🌐
Bacancy Technology
bacancytechnology.com › qanda › python › set-the-root-directory-for-visual-studio-code-python-extension
Set Root Directory for Visual Studio Code Python Extension
Another approach to configuring your Python root directory is by using an environment variable, set through a .env file in your project. This method is often useful if you want to avoid modifying the settings.json file manually. 1. Create a .env File: In the root of your project folder, create a .env file. This file allows you to set environment variables that VSCode will use during the development process. 2. Add the Python Path and Extra Paths: In the .env file, set the PYTHONPATH variable to include your project’s root directory (server in this case):
🌐
Reddit
reddit.com › r/learnpython › how can i set up vscode to recognize directory paths without manually changing the pythonpath?
r/learnpython on Reddit: How can I set up VSCode to recognize directory paths without manually changing the PYTHONPATH?
October 25, 2021 -

I want to be able to have a separate dir for my test suite (which have imports from the source code). My source modules have imports that reference other files in the same directory and it works when I run that code, but when I try to run the test suite, VSCode can't find the modules. For some reason running the test suite works in PyCharm, and I've been trying to figure out the behavior in Pycharm so I could change the behavior accordingly in VSCode to no avail, and figured someone here would know. I'm trying to use VSCode instead of PyCharm because the project also has React/JS and I don't even know if PyCharm would support that (and I just like VSCode better in general... except for this annoying thing).

My directory structure is as follows:

app
|____init__.py
|__client
|__server
      |____init__.py
      |___.venv
      |___data
      |___lib
           |____init__.py
	       |___config.ini
	       |___config_reader.py
	       |___sql_connection.py  (imports config_reader and passes in 'lib/config.ini' to the reader function)
      |___tests
           |____init__.py
	       |___testconfig.ini
	       |___test_sql_connection.py (i try to import config_reader here but vscode will say that the module doesn't exist)

Does anyone have any advice?

EDIT: added that i do have init.py in my folders

🌐
Xebia
xebia.com › home › blog › setting python source folders in visual studio code
Setting Python Source Folders In Visual Studio Code | Xebia
June 24, 2026 - Using the top-level module structure or by creating packages. How do you set a Python source folder in Visual Studio Code? You set a Python source folder in Visual Studio Code by configuring the PYTHONPATH environment variable.
🌐
Machinelearninghelp
machinelearninghelp.org › programming-for-machine-learning › how-to-add-directory-to-path-python-vscode
Adding Directory to Path in Python VSCode for Machine Learning
Open Your Settings: In VSCode, navigate to “File” > “Preferences” > “Settings”. Search for Path: Type python.path in the search bar. Add Directory: Click on the “Update Python Path” option and add your desired directory by clicking on the “+” icon.
🌐
GitHub
github.com › microsoft › vscode-python-environments › issues › 735
Feature Request: Easily Add Folder to PYTHONPATH from VS Code UI · Issue #735 · microsoft/vscode-python-environments
August 19, 2025 - Hi, I’m not sure if this belongs here or in vscode-python, but I’d like to request a more user-friendly way to add folders to the Python path in VS Code. Current Situation The python.analysis.extraPaths setting only affects IntelliSense,...
Find elsewhere
🌐
YouTube
youtube.com › watch
How to Add Python Path in Visual Studio Code (2023) - YouTube
In this video, I'll show you how you can add Python Path in Visual Studio Code. Before adding python path in vscode, first you need to know where python is i...
Published: November 19, 2023
Top answer
1 of 16
183

I have a situation that I believe is relatively common. I want a script to import a module from another directory. My python project is laid out as follows:

~/project/
  |
  |---modules/
        |
        |---mod.py
  |---scripts/
        |---script.py

in script.py, I have from modules import mod. So my PYTHONPATH needs to be set to ~/project/ (something that PyCharm does automatically).

VSCode is a great editor, but everywhere else, it falls short, in my opinion. This is a perfect example of that.

I create a default launch.json file to "run the current file". A "cwd": "${fileDirname}" line has to be added to make things work like they do in PyCharm (FYI, a list of the built-in variables can be found here).

Debugging

For debugging (the "play" button on the sidebar, or the F5 key), the PYTHONPATH set in launch.json or your .env file takes effect. Note that in the .env file, you cannot use variables such as ${workspaceRoot}, but you can easily append or insert to the path by using the proper separator for your platform (; for Windows and : for everyone else).

Because I want to take advantage of that variable, I put this in my launch.json:

    "env": {"PYTHONPATH": "${workspaceFolder}${pathSeparator}${env:PYTHONPATH}"}

(Thanks to @someonr for the suggestion to use ${pathSeparator}.)

It appears that you can prepend/append to whatever is inherited from the environment (this is not true for settings.json; see below).

This will also work for the hotkey Ctrl+F5 (run without debugging).

For reference, here's the full file, which replicates what PyCharm does automatically:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python: Current File",
            "type": "python",
            "request": "launch",
            "program": "${file}",
            "console": "integratedTerminal",
            "cwd": "${fileDirname}",
            "env": {"PYTHONPATH": "${workspaceFolder}${pathSeparator}${env:PYTHONPATH}"}
        }
    ]
}

Run in terminal

If I hit the "play" button that appears on the top right of the editor window (when a python file is the active tab), it will not work. This runs the current file in a terminal, which doesn't pay attention to launch.json at all. To make that work, you have to define PYTHONPATH in a settings.json file, by adding this:

    "terminal.integrated.env.osx": {"PYTHONPATH": "${workspaceFolder}"}

(Note there are different values for each platform.) If you've selected a python interpreter (e.g. from a virtual environment), you will already have a settings.json file in the .vscode directory. Mine looks like this:

{
    "python.pythonPath": "/Users/me/project/venv/bin/python3",
    "terminal.integrated.env.osx": {"PYTHONPATH": "${workspaceFolder}"}
}

You can't append or insert values into the inherited PYTHONPATH via the settings.json file. It will only take one string, and it will not parse separators. So even though you could get the value using ${env:PYTHONPATH}, you won't be able to do anything with it.

Moreover, you can't set the current working directory. Even though it would seem you could set it with "terminal.integrated.cwd": "${workspaceFolder}", it doesn't work. So if any of your scripts do anything with paths relative to their location in the tree, they won't work. The working directory will be your project root.

Note that any changes to the settings.json file will require that you exit the integrated terminal and restart it.

Linting

Nothing I do to launch.json regarding PYTHONPATH makes any difference to pylint, which will red-underline from modules import mod, despite the fact I can put the cursor on mod, hit F12, and the file opens. Snooping around linting settings, the defaults for mypy include --ignore-missing-imports. To replicate this behavior with pylint, add this to your settings.json:

    "python.linting.pylintArgs": [
        "--disable=F0401"
    ] 

Shame that we just have to work around this, but the autocomplete helps a lot when writing the import statements to begin with.

Conclusion

There are many layers to VSCode and it's hard to get things to work together. It seems multiple environments are floating around. In the end:

  1. I cannot "run in terminal" because I can't set the current working directory to be the path containing the current file.
  2. I cannot set PYTHONPATH for pylint as that runs in some environment different than the integrated terminal and whatever is controlled by launch.json, so I can only tell pylint to ignore import errors.
  3. Running with F5 works if you set PYTHONPATH either via an .env file or in launch.json
2 of 16
60

The documentation is missing some important details.

Example

Suppose your project layout is like this

myproject/
    .vscode/
        settings.json
    .env
    src/
        a_module.py
    tests/
        test_a.py

Open the settings.json file and insert these lines

// On linux use "terminal.integrated.env.linux": {
"terminal.integrated.env.windows": {
    "PYTHONPATH": "${workspaceFolder}/src;${workspaceFolder}/tests"
},
// The next line can be omitted, unless you've modified the global default
"python.envFile": "${workspaceFolder}/.env",

Note that ${workspaceFolder} evaluates to myproject, it is not to the .vscode folder.

In the .env file enter this

PYTHONPATH=src;test
# Paths are relative to the workspace folder, so above is equivalent to
# WORKSPACE_FOLDER=C:/full/path/to/myproject
# PYTHONPATH=${WORKSPACE_FOLDER}/src;${WORKSPACE_FOLDER}/tests

Note that on all platforms, including Windows, the slashes in the path lean forward, like so /. Different paths are separated with a ; on Windows, on other platforms with a :.

Final step, restart VS Code.

This blog was helpful.

🌐
Python Forum
python-forum.io › thread-42516.html
SOLVED: VS Code: Adding external directories to project?
July 31, 2024 - I'm not sure if this is the right place to ask, but I can't find anywhere better... I'm trying to switch from PyCharm to VS Code as my Python IDE. There are a few reasons why, but lets just say I want the experience of using both IDEs. Some of my p...
🌐
Reddit
reddit.com › r/learnpython › add the working directory to python sys.path in vscode.
r/learnpython on Reddit: Add the working directory to python sys.path in vscode.
November 10, 2021 -

My directory structure is as the following:

rootdir
  |-layer1
    |-sublayer1
      |-test.py
  |-modules
    |-math.py

When I run test.py by hitting the run icon in vscode, it only adds the directory ./layer1/sublayer1 into sys.path. Is it possible to add rootdir in sys.path? Because it imports modules.math.

🌐
Redreamality
redreamality.com › blog › pythonpathvs-code
How to Set PYTHONPATH on Windows, Linux, macOS and VS Code
August 21, 2025 - It does not select the Python executable: that is the job of your shell’s PATH or the interpreter selected in VS Code. For a project containing src/my_package/, add the src directory, not src/my_package or an individual .py file.
🌐
YouTube
youtube.com › watch
Install Python and Visual studio Code & Set Python path environment variable on Windows. #python - YouTube
This video will guide you on how to achieve the following: 1. To start coding in python you require python installed. Python installation files are found in...
Published: July 23, 2022
🌐
GitHub
github.com › microsoft › vscode-python › issues › 22824
Automatically set pythonpath correctly for default project setups · Issue #22824 · microsoft/vscode-python
February 1, 2024 - # Create or overwrite the .env file echo "PYTHONPATH=$PROJECT_ROOT" > "$PROJECT_ROOT/.env" echo "Setting up VS Code configuration files..." # Create .vscode directory if it doesn't exist mkdir -p "$PROJECT_ROOT/.vscode" # Create or overwrite settings.json cat > "$PROJECT_ROOT/.vscode/settings.json" << EOF { "python.pythonPath": "$VENV_DIR/bin/python", "python.envFile": "\${workspaceFolder}/.env", "python.testing.unittestEnabled": true, "python.testing.pytestEnabled": false, "python.testing.nosetestsEnabled": false, "python.testing.unittestArgs": [ "-v", "-s", "./tests", "-p", "test_*.py" ] } E
🌐
eSparkBiz
esparkinfo.com › root directory for visual studio code python
Proper Way to Set Python Root Directory in VS Code
July 17, 2025 - 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”]}