Visual Studio Code – How to add multiple paths to python path?

Question:

I am experimenting with Visual Studio Code and so far, it seems great (light, fast, etc).

I am trying to get one of my Python apps running that uses a virtual environment, but also uses libraries that are not in the site-package of my virtual environment.

I know that in settings.json, I can specify a python.pythonPath setting, which I have done and is pointing to a virtual environment.

I also know that I can add additional paths to python.autoComplete.extraPaths, where thus far I am adding the external libraries. The problem is, when I am debugging, it’s failing because it’s not finding the libraries specified in python.autoComplete.extraPaths.

Is there another setting that must be used for this?

Thanks

Asked By: mike01010

||

Answers:

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"
        }
    }
]
Answered By: malbs

You could add a .pth file to your virtualenv’s site-packages directory.

This file should have an absotute path per line, for each module or package to be included in the PYTHONPATH.

https://docs.python.org/2.7/install/index.html#modifying-python-s-search-path

Answered By: tebanep

bash escamotage (works with debugger AND autocomplete); need to install code command in PATH (vsc shell command: install…)

#!/bin/bash

#
# vscode python setup
#

function fvscode {
  # you just want one of this:
  export PYTHONPATH=<your python installation ../bin/python3>
  # you may want many of these:
  export PYTHONPATH=<your lib dir here>:$PYTHONPATH
  # launch vscode
  code 
}
alias vscode='fvscode'

the launch VSC by typing ‘vscode’.

Answered By: Kabu

I had the same issue, malbs answer doesn’t work for me until I change semicolon to a colon,you can find it from ZhijiaCHEN’s comments

"env": { "PYTHONPATH": "/path/to/a:/path/to/b" }

Alternatively, I have a hack way to achieve the same:

# at the top of project app script:
import sys
sys.path.append('/path/to/a')
sys.path.append('/path/to/b')
Answered By: Haifeng Zhang

The Python Extension in VS Code has a setting for python.envFile which specifies the path to a file containing environment variable definitions (Refer to: https://code.visualstudio.com/docs/python/environments#_environment-variable-definitions-file). By default it is set to:

"python.envFile": "${workspaceFolder}/.env"

So to add your external libraries to the path, create a file named .env in your workspace folder and add the below line to it if you are using Windows:

PYTHONPATH="C:pathtoa;C:pathtob"

The advantage of specifying the path here is that both the auto-complete as well as debugging work with this one setting itself. You may need to close and re-open VS Code for the settings to take effect.

Answered By: WebDev

According to the environments doc, the places the extension looks for environments include some defaults and also the setting value for python.venvPath in the workspace settings

eg: "python.venvPath": "~/.virtualenvs"

This allows you to find several (eg: virtualenvs) as mentioned:

To select a specific environment, use the Python: Select Interpreter
command from the Command Palette

Answered By: Efren

Based on https://github.com/microsoft/vscode-python/issues/12085, I added the following to the settings portion of the workspace config file. I’m using Linux. For Windows, use terminal.integrated.env.windows.

"terminal.integrated.env.linux": {
    "PYTHONPATH": "addl-path-entry1:addl-path-entry2"
}

I also added an .env file as described by many posts/comments above.

Finally, I added the PyLance extension per https://stackoverflow.com/a/64103291/11262633.

I also reloaded my workspace.

These two changes allowed me to run Python programs using the debugger and the Run menu. AutoComplete is aware of the added path, and my VSCode linter (was the default linter pylint, now “pylance“`) now works.

Answered By: mherzog

I made it work through adding "python.analysis.extraPaths" when using Pylance and IntelliCode.

Answered By: Jack sparrow

In 2022, the configuration is as file .vscode/settings.json:

{
    "python.analysis.extraPaths": ["C:/Program Files/obs-studio/data/obs-scripting/64bit"],
    "terminal.integrated.env.windows": {
        "PYTHONPATH": "C:/Program Files/obs-studio/data/obs-scripting/64bit;${env:PYTHONPATH}",
        "PATH": "C:/Program Files/obs-studio/data/obs-scripting/64bit;${env:PATH}"
    }
}
Answered By: Honghe.Wu
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.