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

8.9k Views

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

• 1
• @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.
• Doesn't sound like you've read. So you have "pythonPath":"${config.python.pythonPath}", in your launch.json? • 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. • 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') 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"
}
}
]
• 1
• Does the forward slash work in windows as well? I'm running with corrected pythonpath (after restarting VSCode) and still get missing import.
• It doesn't work until I change semicolon to a colon

The Python Extension in VS Code has a setting for python.envFile which specifies the path to a file containing environment variable definitions. 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. Reply Report • 2 • 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" • 1 • 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. • 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

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

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:
# you may want many of these:
# launch vscode
code
}
alias vscode='fvscode'

the launch VSC by typing 'vscode'.