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

PythonVisual Studio-Code

Python Problem Overview


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

Python Solutions


Solution 1 - Python

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"
        }
    }
]

Solution 2 - Python

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:\path\to\a;C:\path\to\b"

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.

Solution 3 - Python

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')

Solution 4 - Python

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

Solution 5 - Python

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.

Solution 6 - Python

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'.

Solution 7 - Python

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionmike01010View Question on Stackoverflow
Solution 1 - PythonmalbsView Answer on Stackoverflow
Solution 2 - PythonWebDevView Answer on Stackoverflow
Solution 3 - PythonHaifeng ZhangView Answer on Stackoverflow
Solution 4 - PythontebanepView Answer on Stackoverflow
Solution 5 - PythonmherzogView Answer on Stackoverflow
Solution 6 - PythonKabuView Answer on Stackoverflow
Solution 7 - PythonEfrenView Answer on Stackoverflow