If you only need the $PATH to be set in the integrated terminal, you can use VS Code's terminal.integrated.env.<platform> setting (added in version 1.15). Find the setting for your platform in the settings, click "Edit in settings.json", and add a "PATH" field like so:
"terminal.integrated.env.osx": {
"PATH": "${env:PATH}:/my/path"
}
(Replace .osx with .linux or .windows as needed.)
The expression ${env:PATH} causes the existing contents of the $PATH environment variable to be inserted. This way you can extend it without hard-coding it.
As for having the $PATH available everwhere in VS Code, so that it will
be used by extensions that call binaries, the only workaround I've found so far is this:
Configure your shell to have the
$PATHyou want. For example, I'm using Bash, and my~/.bash_profilehas the following line:PATH="$PATH:$HOME/bin"In VS Code, press ⇧⌘P and type
install 'code' commandif you haven't done so before.Quit VS Code.
Launch VS Code not by clicking its icon in the dock, but by opening Terminal.app and typing
code. Your newly set path will be active in VS Code until you quit it.If VS Code restarts, for example due to an upgrade, the
$PATHwill reset to the system default. In that case, quit VS Code and re-launch it by typingcode.
Update: VS Code on Mac and Linux now apparently tries to automatically resolve the shell environment when it is started by clicking the icon (rather than via code). It does this by temporarily starting a shell and reading the environment variables. I haven't tested this though.
If you only need the $PATH to be set in the integrated terminal, you can use VS Code's terminal.integrated.env.<platform> setting (added in version 1.15). Find the setting for your platform in the settings, click "Edit in settings.json", and add a "PATH" field like so:
"terminal.integrated.env.osx": {
"PATH": "${env:PATH}:/my/path"
}
(Replace .osx with .linux or .windows as needed.)
The expression ${env:PATH} causes the existing contents of the $PATH environment variable to be inserted. This way you can extend it without hard-coding it.
As for having the $PATH available everwhere in VS Code, so that it will
be used by extensions that call binaries, the only workaround I've found so far is this:
Configure your shell to have the
$PATHyou want. For example, I'm using Bash, and my~/.bash_profilehas the following line:PATH="$PATH:$HOME/bin"In VS Code, press ⇧⌘P and type
install 'code' commandif you haven't done so before.Quit VS Code.
Launch VS Code not by clicking its icon in the dock, but by opening Terminal.app and typing
code. Your newly set path will be active in VS Code until you quit it.If VS Code restarts, for example due to an upgrade, the
$PATHwill reset to the system default. In that case, quit VS Code and re-launch it by typingcode.
Update: VS Code on Mac and Linux now apparently tries to automatically resolve the shell environment when it is started by clicking the icon (rather than via code). It does this by temporarily starting a shell and reading the environment variables. I haven't tested this though.
In:
> Preferences: Open Settings (JSON)
add to the JSON file:
"terminal.integrated.env.windows": {
"PATH": "${env:PATH}"
},
-> terminal.integrated.env should end with .osx, .linux or .windows depending on your OS.
In order to check if it works execute in your VS Code Terminal:
# For PowerShell
echo $env:PATH
# For bash
echo "$PATH"
Integrated terminal does not load PATH environment variable from user environment variables on windows 10
Is there any way to set environment variables in Visual Studio Code? - Stack Overflow
How to start with the right environment variables: VSCode with Microsoft C++
Use environment variable to define a path in vscode workspace
Videos
Assuming you mean for a debugging session(?) then you can include a env property in your launch configuration.
If you open the .vscode/launch.json file in your workspace or select Debug > Open Configurations then you should see a set of launch configurations for debugging your code. You can then add to it an env property with a dictionary of string:string.
Here is an example for an ASP.NET Core app from their standard web template setting the ASPNETCORE_ENVIRONMENT to Development :
{
"version": "0.2.0",
"configurations": [
{
"name": ".NET Core Launch (web)",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build",
// If you have changed target frameworks, make sure to update the program path.
"program": "${workspaceFolder}/bin/Debug/netcoreapp2.0/vscode-env.dll",
"args": [],
"cwd": "${workspaceFolder}",
"stopAtEntry": false,
"internalConsoleOptions": "openOnSessionStart",
"launchBrowser": {
"enabled": true,
"args": "${auto-detect-url}",
"windows": {
"command": "cmd.exe",
"args": "/C start ${auto-detect-url}"
},
"osx": {
"command": "open"
},
"linux": {
"command": "xdg-open"
}
},
"env": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"sourceFileMap": {
"/Views": "${workspaceFolder}/Views"
}
},
{
"name": ".NET Core Attach",
"type": "coreclr",
"request": "attach",
"processId": "${command:pickProcess}"
}
]
}
You can load an environment file by setting the envFile property like this:
{
"version": "0.2.0",
"configurations": [
{
"name": "Launch",
"type": "go",
"request": "launch",
"mode": "debug",
"remotePath": "",
"port": 2345,
"host": "127.0.0.1",
"program": "${workspaceFolder}",
"envFile": "${workspaceFolder}/.env", // HERE
"args": [],
"showLog": true
}
]
}
Place the .env file in your folder and add vars like this:
KEY1="TEXT_VAL1"
KEY2='{"key1":val1","key2":"val2"}'
Further Reading: Debugging go in vscode with environment variables
My friend is just starting with C++ and I'm coming back to it from a long time ago. Set it up with both Microsoft C++ (cl.exe) and g++.
Basically unless you start VSCode from the Developer Command Prompt for VS2019 (or 2022), the build task will not work, giving the error "cl.exe" not found. That makes sense since the tasks.json file is using command "cl.exe". So the Dev. Prompt is setting up the environmental variables when you start VSCode from there.
It seems super unwieldy to always have to start VSCode from the developer prompt every time. How do you get around this?
I have a multi root workspace and I want to define the path for each of the folders using an environment variable. So something like this
{
"folders": [
{
"name" : "Foo"
"path": "${env:FOO_PATH}"
},
{
"path": "my-folder-b"
}
]
}I'm opening this workspace from a developement environment where I have defined this environement variable in the Dockerfile. The environement variable doesnt seem to be visible in the workspace file Has any had a similar issue or is this not supported
I had the same problem, and I have a workaround solution.
I just add one line
export PATH="/Users/username/anaconda3/bin:$PATH" into .bash_profile or .zshrc
The orifinal $PATH in vscode is
/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Users/username/anaconda3/bin:/Users/username/anaconda3/condabin
And then my $PATH in vscode become /Users/username/anaconda3/envs/py36/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Users/username/anaconda3/bin:/Users/username/anaconda3/condabin
Please give it a try. I wish the workaround might help. Thanks :)
It sounds like your Workspace setting python.pythonPath is set explicitly to the /usr/bin. Set that to match your preferred $PATH and you should be good to go.
Go to Preferences in VS Code, search for python.pythonPath, switch to Workspace, and set as desired.
You can use Environment variables
The syntax is like ${env:USERNAME}
You can use the attribute "env" in settings.json. For example:
{
"version": "0.2.0",
"configurations": [
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"justMyCode": true,
// ENVINRONMENT VARIABLES
"env": {
"VAR_A": "value_a",
"VAR_B": "value_b"
}
}
]
}