How to set the root directory for Visual Studio Code Python Extension?

I have no trouble running and debugging my project with VSCode Python Extension (ms-python.python), but since python sub-project root directory is not the whole project directory, all imports from my sources are underlined with red color and are listed in the problems and so Go to definition and some similar features don't work properly. How can I tell the IDE where's the start point of my project:

Whole Project path:
  docs
  server
    entities
      user.py
      customer.py
  env
  viewer
  db

The server directory is where the imports path are started from:

from entities.user import User

Solution 1:

You can create a .env file with:

PYTHONPATH=server

That will add your server folder to PYTHONPATH as needed.

(You may need to restart VSCode for it to take PYTHONPATH into account correctly.)


Edited to clarify...

Create a file named .env under the repo root e.g. your_repo/.env.

Also creating the file under the folder where your consuming code is, instead of under repo root, seems to work e.g. your_repo/service/.env.

For more details, see documentation on environment variable definition files.

For me this worked without restarting VSC, perhaps this is a matter of newer VSC and extensions versions.

Solution 2:

If you are using the Pylance extension you can set your source folder via the python.analysis.extraPaths option. It also looks for common source folder names like src by default, this option is called python.analysis.autoSearchPaths.

Go to File > Preferences > Settings, search for pythonpath. Under the Pylance options you should see Extra Paths, this is where you set your source folder.

Solution 3:

The PYTHONPATH is an environment variable which you can set to add additional directories where python will look for modules and packages.

If you need to set working directory for Visual Studio Code,

The better way is to customize Settings.json and launch.json, do like this:

// vi .vscode/Settings.json
{
    "python.pythonPath": "venv/bin/python",
}

use cwd to Specifies the current working directory for the debugger, which is the base folder for any relative paths used in code. If omitted, defaults to ${workspaceFolder} (the folder open in VS Code).

// vi .vscode/launch.json
{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python: your project name",
            "type": "python",
            "request": "launch",
            "cwd": "${workspaceRoot}/server",
        }
    ]
}

If you want the server run properly without any IDE, just insert the Root Drectory in front of PYTHONPATH . Assume there is a server/run.py:

import sys
src_path = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, _src_path)   

refer: https://code.visualstudio.com/docs/editor/debugging#_launch-versus-attach-configurations

refer: https://code.visualstudio.com/docs/python/debugging#python-articles

refer: https://docs.python.org/3/using/cmdline.html#envvar-PYTHONPATH

Solution 4:

Setting PYTHONPATH is what makes it work, as noted above. I use the following .env content so that it works for any project:

PYTHONPATH=${PROJ_DIR}:${PYTHONPATH}

This is essentially what PyCharm does when you check "Add Content Roots to PYTHONPATH" in your run/debug configuration. It's a helpful setting, but it spoils you because your code fails outside PyCharm.

Or, if you run in terminal, first export:

export PYTHONPATH=...