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
Answer from darda on Stack Overflow
Top answer
1 of 16
182

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.

🌐
Donjayamanne
donjayamanne.github.io › pythonVSCodeDocs › docs › python-path
Python Path and Version | Python in Visual Studio Code
{ "python.pythonPath": "/home/xxx/dev/ala/venv/bin/python" } Finally, restart VS Code, necessary for intellisense to work (future release will not require a restart) Ensure the libraries/modules you plan on using for linting are also installed within this virtual environment. Option 2: Activate the Virtual Environment from your Terminal/Command Window and then launch VS Code. Ensure none of the Python paths are configured in the settings.json file (leave them to their defaults).
Discussions

JSON - setting path for Python in visual studio code - Stack Overflow
I am working with a YouTube instruction video and there person on the video has search the json settings files with "python.p" and python.python.path line comes up that he uses to set the More on stackoverflow.com
🌐 stackoverflow.com
Details on the A/B experiment removing `python.pythonPath` support
Although it is true that pythonPath would likely be different for different people, I'd argue that is true for the entire (or most of) .vscode/settings.json. More on github.com
🌐 github.com
46
June 12, 2020
`pythonPath` on `settings.json` automatically added to workspace settings
This seems appears to be similar if not identical to this, but maybe I am missing something. I will try to be as clear as I can. TLDR pipenv interpreter absolute path is automatically added to work... More on github.com
🌐 github.com
3
December 10, 2018
Interpreter not Picked up From `settings.json`, and Multiple Paths not Working
Environment data VS Code version: 1.51.1 Extension version (available under the Extensions sidebar): v2020.11.371526539 OS and version: Debian GNU/Linux 10 (actually a python:3.9 Docker image runni... More on github.com
🌐 github.com
6
November 22, 2020
🌐
Plone Community
community.plone.org › plone support
VS Code editor removing pythonPath from workspace/settings.json - Plone Support - Plone Community
July 15, 2020 - Only relevant for people using VS code as their IDE for Python projects. I got a popup after updating VS code this morning that my PythonPath parameter in .vscode/settings.json is deprecated and if VS code should remove…
🌐
Visual Studio Code
code.visualstudio.com › docs › python › environments
Python environments in VS Code
November 3, 2021 - When you assign an environment to a project, the extension writes to your workspace settings (.vscode/settings.json):
🌐
Visual Studio Code
code.visualstudio.com › docs › python › settings-reference
Python settings reference
November 3, 2021 - The Python Extension for Visual Studio Code is highly configurable. This page describes the key settings you can work with.
🌐
GitHub
github.com › microsoft › vscode-python › issues › 12313
Details on the A/B experiment removing `python.pythonPath` support · Issue #12313 · microsoft/vscode-python
June 12, 2020 - We also had a long-standing request to not write a user's environment path to .vscode/settings.json to allow for it to be committed to version control without be specific to any one user's machine. We set up an A/B experiment to measure impact on users in dropping support for python.pythonPath.
Author   karrtikr
Find elsewhere
🌐
GitHub
github.com › microsoft › vscode-python › issues › 3617
`pythonPath` on `settings.json` automatically added to workspace settings · Issue #3617 · microsoft/vscode-python
December 10, 2018 - When code . os launched from a pipenv shell, a pythonPath entry is being automatically added to .vscode/settings.json, storing my personal env path to the project's workspace settings.
Author   gtalarico
🌐
GitHub
github.com › microsoft › vscode-python › issues › 14798
Interpreter not Picked up From `settings.json`, and Multiple Paths not Working · Issue #14798 · microsoft/vscode-python
November 22, 2020 - Also, one should be able to specify multiple paths in the "python.pythonPath" in settings.json. In particular, I would like to point to: ... Every time I open VSCode or I try to run/debug my Python files, I am shown the following prompt: "You need to select a Python interpreter before you start debugging."
Author   RishabhMalviya
🌐
GitHub
github.com › microsoft › vscode-python › issues › 12289
Setting pythonPath with ${workspaceFolder} in launch.json configs doesn't work · Issue #12289 · microsoft/vscode-python
June 11, 2020 - Note that it works fine with my workspace's settings.json with: "python.pythonPath": "${workspaceFolder}/build/my/own/python", I happen to use a custom interpreter and I actually have a variety of workspace folders that draw their settings from ...
Author   koenlek
🌐
Reddit
reddit.com › r/vscode › selecting a python interpreter doesn't update settings.json
r/vscode on Reddit: Selecting a python interpreter doesn't update settings.json
March 13, 2021 -

I open a folder in vscode, this folder does not have a settings.json and I run ctrl + shift p, and select an interpreter. This does not create a settings.json file and add settings to it.

I have also tried opening file -> settings, editing workspace settings so I have a settings.json and selecting interpreter but still nothing is added.

I have also tried creating a ${folder}.code-workspace settings file and selecting interpreter but nothing is added.

How do I make my folder a valid workspace so I can select an interpreter and the settings.json will update?

🌐
Visual Studio Magazine
visualstudiomagazine.com › articles › 2020 › 05 › 14 › vs-code-python.aspx
VS Code Python Update Tweaks Interpreter Path Functionality -- Visual Studio Magazine
But it also then adds a line to settings.json indicating the path for the virtual environment which can cause issues for configs shared among a team. The python.pythonPath variable will not be constant for each user working on the repo (eg "python.pythonPath": "/home/daniel/.local/share/virtualenvs/ontariofresh_backend-NQNAEc6t/bin/python", note my home directory in the path) and thus prevents the settings.json file from being committed to the remote repository.
Top answer
1 of 2
2

It currently isn't directly supported, but we have a feature request that you can upvote if you would like to see it prioritized.

2 of 2
1

One workaround would be to:

  • remove that line from the local workspace settings.json
  • copy-paste it to your User settings.json

See "VSCode User and Workspace Settings".

That way, your Git codebase can keep a generic settings.json without local path.

Since this would work for only one project, you can instead reference all your projects in several Multi-root workspaces.

Then, regarding settings in that environment, you have three files:

  • Preferences: Open User Settings - Open your global User settings
  • references: Open Workspace Settings - Open the settings section of your Workspace file.
  • Preferences: Open Folder Settings - Open the settings for the active folder.

That means you could switch workspaces, and in each multi-root workspace (each one composed of only one root), you would keep:

  • the global pythonPath path in user settings (applies to everything, everywhere)
  • the specific pythonPath for a given multi-root workspace in the Workspace setting (outside of the project folder which is the only root for that "multi-root" workspace)
  • the public versioned settings.json project-specific settings in the project folder (which is the only root of the workspace)

Again, by switching workspace, you can differentiate between:

  • workspace-specific private settings, local to your computer, like a pythonPath
  • project specific settings, in the project folder, that you can version and publish.
🌐
Redreamality's Blog
redreamality.com › blog › pythonpathvs-code
Setting PYTHONPATH Environment Variable: A Comprehensive Guide for All Platforms & VS Code Development
August 21, 2025 - In the opened .vscode/settings.json file, add the following configuration: { "terminal.integrated.env.windows": { "PYTHONPATH": "${workspaceFolder}\\src;${env:PYTHONPATH}" }, "terminal.integrated.env.linux": { "PYTHONPATH": "${workspaceFolder}/src:${env:PYTHONPATH}" }, "terminal.integrated.env.osx": { "PYTHONPATH": "${workspaceFolder}/src:${env:PYTHONPATH}" } }
🌐
Python Forum
python-forum.io › thread-40540.html
How to set PYTHONPATH in Visual Studio Code?
Hello! Belows are my dev environment, OS : Windows 11 python : Anaconda3 Apache Spark : 3.4.1 IDE : Visual Studio Code 1.18.1And I try to integrate apache spark pyspark into VS code. So I set the python path into settings.json file of vs code. '...
🌐
Microsoft Developer Blogs
devblogs.microsoft.com › dev blogs › microsoft for python developers blog › python in visual studio code – july 2021 release
Python in Visual Studio Code – July 2021 Release - Microsoft for Python Developers Blog
July 20, 2021 - If you still want to define an interpreter path to be used by the Python extension by default in your settings.json, you can set the “python.defaultInterpreterPath” instead. When set on the workspace scope, the value of this setting will define which interpreter the Python extension will select when it loads the workspace for the first time (and for the first time only).
🌐
Xebia
xebia.com › home › blog › setting python source folders in visual studio code
Setting Python Source Folders In Visual Studio Code | Xebia
3 weeks ago - PYTHONPATH=${PYTHONPATH}:./src # Use path separator ';' on Windows. ... There is no need to reload the workspace. Just open any Python file and enjoy the editors’ capabilities. Please note that it’s safe to include the settings.json file in source control.
🌐
GitHub
github.com › microsoft › vscode-python › issues › 22824
Automatically set pythonpath correctly for default project setups · Issue #22824 · microsoft/vscode-python
February 1, 2024 - the current project directory "." should be part of the PYTHONPATH automatically and the necessary settings should be hidden and the user not to be forced to investigate the inner workings of vscode and the extension
Author   WolfgangFahl