72

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

jbmusso
  • 3,436
  • 1
  • 25
  • 36
mike01010
  • 5,226
  • 6
  • 44
  • 77
  • Did you read this: https://github.com/DonJayamanne/pythonVSCode/wiki/Python-Path-and-Version#python-version-used-for-debugging ? – jbasko Jan 04 '17 at 19:22
  • 3
    @jbasko, yes, i think i read everything out there. They all indicate only that the python.pythonPath takes a single value that points to the python interpreter. In eclipse for example, you can add external source folders. But not sure how to do that for vs code. – mike01010 Jan 04 '17 at 19:25
  • Doesn't sound like you've read. So you have `"pythonPath":"${config.python.pythonPath}",` in your launch.json? – jbasko Jan 04 '17 at 19:32
  • 2
    I have it set to the path of my virtual environment's python interpreter in all the files (settings, launch and task.json). What you are reading states that if it is in the settings.json, it will be picked up if the setting is as you state in launch.json. That's fine, but not my issue. My issue as adding multiple paths (paths to external libraries) to the python path. Much like you can do in Eclipse and other editors. – mike01010 Jan 05 '17 at 20:55
  • what OS are you using? If in Windows, activate your virtual environment in powershell and just type "code[enter]" once it's active. – bwooceli Jan 24 '17 at 02:46
  • 6
    Here is a super hacky workaround until it is actually solved, add this to the top of your first python file: `import sys; sys.path.append('/path/to/my/pylib')` – Andrew Hundt Jun 10 '17 at 03:34
  • Mike, You got an answer below. Take a look and mark it as the answer if it works, or comment on it... – pashute Jan 09 '18 at 12:26
  • Related: https://stackoverflow.com/questions/53653083/how-to-correctly-set-pythonpath-for-visual-studio-code – Ciro Santilli OurBigBook.com Feb 14 '23 at 12:08

9 Answers9

65

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"
        }
    }
]
Ben B
  • 78
  • 5
malbs
  • 684
  • 6
  • 4
54

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.

WebDev
  • 1,211
  • 11
  • 17
  • 1
    This still works in 2019. 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 – Enrico Jun 20 '19 at 06:12
  • 3
    Info: this worked for me with a VSCode attached to a running container; If someone find this usefull: `"python.pythonPath": "/usr/bin/python"` & `.env` -> `PYTHONPATH="/usr/src/my-project/a;/usr/src/my-project/b"` – Manu Artero Jul 15 '19 at 15:12
  • 3
    This is the best way. `.env` files are commonly used in more applications (and languages) than VS Code. The `.env` file can travel with the source code and still be recognized by other engines/IDEs, where the `settings.json` file is specific to VS Code. – mevers303 Oct 04 '19 at 22:55
  • Importing numpy failed for me after doing it this way – daniekpo Apr 14 '21 at 17:12
  • 1
    Worked for me. Thank you. This was frustrating me for a while. – binarymason Apr 28 '21 at 13:01
  • How to add a relative path to ${workspaceFolder} to PYTHONPATH ? – rvcristiand Jun 23 '21 at 16:33
  • Worked for me as well using a relative path rather than full path – ablanch5 Oct 12 '22 at 19:37
9

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')
Haifeng Zhang
  • 30,077
  • 19
  • 81
  • 125
7

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}"
    }
}
Honghe.Wu
  • 5,899
  • 6
  • 36
  • 47
6

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.

mherzog
  • 1,085
  • 1
  • 12
  • 24
5

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

tebanep
  • 625
  • 1
  • 9
  • 11
2

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

0

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

Kabu
  • 519
  • 6
  • 16
0

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

Efren
  • 4,003
  • 4
  • 33
  • 75