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
🌐
Visual Studio Code
code.visualstudio.com › docs › python › environments
Python environments in VS Code
November 3, 2021 - Workspace ├── Python Project: backend/ │ └── Environment: .venv (Python 3.12) │ └── Manager: venv │ ├── Python Project: frontend-utils/ │ └── Environment: .venv (Python 3.10) │ └── Manager: venv │ └── Python Project: ml-pipeline/ └── Environment: ml-env (Python 3.11) └── Manager: conda ... Test Explorer: each project gets its own test tree with its own interpreter (see Multi-Project Testing) ... Pylance and Jupyter currently use a single interpreter per workspace, not per-project environments. See Known Limitations. ... When you add a project, its folder is automatically added to the environment search path.
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.

Discussions

Automatically set pythonpath correctly for default project setups
see vs-code Issue 203607 which pointed here the current project directory "." should be part of the PYTHONPATH automatically and the necessary settings should be hidden and the user not t... More on github.com
🌐 github.com
20
February 1, 2024
Feature Request: Easily Add Folder to PYTHONPATH from VS Code UI
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.extra... More on github.com
🌐 github.com
5
August 19, 2025
Change PYTHONPATH before debug
Hello, I am trying to set the PYTHONPATH and other environment variables before launching the debugger. The PYTHONPATH is being set using a shell script, which I managed to run using a task with th... More on github.com
🌐 github.com
3
2
Visual Studio Code - How to add multiple paths to python path? - Stack Overflow
I add that if you are using pipenv, you have to set the path to the /User/.virtualenvs/{$projectname} in order to have the corrent environment with the installed libraries 2019-06-20T06:12:23.167Z+00:00 ... Info: this worked for me with a VSCode attached to a running container; If someone find this usefull: "python... More on stackoverflow.com
🌐 stackoverflow.com
🌐
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

🌐
Donjayamanne
donjayamanne.github.io › pythonVSCodeDocs › docs › python-path
Python Path and Version | Python in Visual Studio Code
Ensure to specify the fully qualified name of the python executable (Mac and Linux supported). ... There are two approaches to to getting this extension working in a particular Virtual Environment. Option 1: Ensure the path to the python interpreter is set in python.pythonPath as defined previously.
🌐
Visual Studio Code
code.visualstudio.com › docs › python › settings-reference
Python settings reference
November 3, 2021 - The Python extension settings support predefined variables. Similar to the general VS Code settings, variables use the ${variableName} syntax. Specifically, the extension supports the following variables: ${cwd} - the task runner's current working directory on startup · ${workspaceFolder} - the path of the folder opened in VS Code
🌐
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
Find elsewhere
🌐
Visual Studio Marketplace
marketplace.visualstudio.com › items
Python Path - Visual Studio Marketplace
Visual Studio Code>Programming Languages>Python PathNew to Visual Studio Code?
🌐
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
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):
🌐
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
🌐
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. '...
🌐
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 - This should add the folder to the Python path of the currently selected virtual environment in VS Code.
Author   kipavy
🌐
GitHub
github.com › mgesbert › vscode-python-path
GitHub - mgesbert/vscode-python-path: VS Code extension for handling python module names
VS Code extension for handling python module names - mgesbert/vscode-python-path
Starred by 10 users
Forked by 4 users
Languages   JavaScript 100.0% | JavaScript 100.0%
🌐
GitHub
github.com › microsoft › vscode-python › discussions › 21837
Change PYTHONPATH before debug · microsoft/vscode-python · Discussion #21837
Hello, I am trying to set the PYTHONPATH and other environment variables before launching the debugger. The PYTHONPATH is being set using a shell script, which I managed to run using a task with th...
Author   microsoft
🌐
MacLochlainns Weblog
blog.mclaughlinsoftware.com › 2024 › 01 › 14 › vscode-pythonpath
How to incorporate PYTHONPATH in VSCode IDE
January 15, 2024 - student@student-virtual-machine:~$ /bin/python /home/student/Code/python/hello_whom5.py Traceback (most recent call last): File "/home/student/Code/python/hello_whom5.py", line 5, in <module> from input import parse_input ModuleNotFoundError: No module named 'input' student@student-virtual-machine:~$ export set PYTHONPATH=/home/student/Lib student@student-virtual-machine:~$ /bin/python /home/student/Code/python/hello_whom5.py Hello World!
🌐
Medium
medium.com › nishkoder › setting-the-python-interpreter-path-in-visual-studio-code-a-detailed-guide-2a53f9f069f1
Setting the Python Interpreter Path in Visual Studio Code: A Detailed Guide | by Nishant Gupta | DataScience with Python — NishKoder | Medium
March 29, 2023 - Inside the “.vscode” folder, open the “settings.json” file. If it doesn’t exist, create a new file named “settings.json”. Add the following setting to the JSON object, replacing /path/to/your/python/interpreter with the correct path to your Python executable:
🌐
GitHub
github.com › microsoft › vscode-python › issues › 10533
Explain once and for all how to set python.pythonPath using environment variables or remove this from docs · Issue #10533 · microsoft/vscode-python
March 12, 2020 - ➜ tree -aL 1 . ├── data ├── .env ├── fig ├── .git ├── .gitignore ├── mnist ├── Pipfile ├── Pipfile.lock ├── .pylintrc ├── README.md ├── run.ps1 ├── src └── .vscode · Output for Python in the Output panel (View→Output, change the drop-down the upper-right of the Output panel to Python) [Info - 6:41:44 p.m.] Analysis cache path: /home/chigozirim/.cache/Microsoft/Python Language Server [Info - 6:41:44 p.m.] Microsoft Python Language Server version 0.5.31.0 [Info - 6:41:44 p.m.] Workspace root: /home/chigozirim/proj
Author   smac89
🌐
Redreamality's Blog
redreamality.com › blog › pythonpathvs-code
Setting PYTHONPATH Environment Variable: A Comprehensive Guide for All Platforms & VS Code Development
August 21, 2025 - Typically, VS Code’s Python extension will automatically recognize and use the .env file. If it does not work, you can ensure it is loaded by specifying the .env file path in launch.json (for debugging) or settings.json. Open or create the .vscode/settings.json file and add: