Can't run `terminal.integrated.env` command in integrated terminal: "command not found" - visual-studio-code

Following this manual page:
https://code.visualstudio.com/docs/python/environments
I would have to set PYTHONPATH "through the terminal settings":
The PYTHONPATH environment variable specifies additional locations
where the Python interpreter should look for modules. In VS Code,
PYTHONPATH can be set through the terminal settings
(terminal.integrated.env.*) and/or within an .env file.
How do I do that? If I write (I ssh to a Linux server):
terminal.integrated.env.linux
I get "command not found".

It seems you tried to enter this
terminal.integrated.env.linux
into the terminal prompt itself, which treated it as a literal command.
What that guide on PYTHONPATH meant when it said "through the terminal settings" was Visual Studio Code's Integrated Terminal settings: https://code.visualstudio.com/docs/getstarted/settings. Specifically the settings under Terminal (filtered on terminal.integrated.env):
Depending on your OS/platform, you put a terminal.integrated.env.<os> block in your VS Code's settings.json file to specify the environment variables to inject when using VS Code's integrated terminal:
From https://code.visualstudio.com/docs/getstarted/settings#_default-settings:
// Object with environment variables that will be added to the
// VS Code process to be used by the terminal on Linux. Set to
// `null` to delete the environment variable.
"terminal.integrated.env.linux": {},
So if you are on Linux:
"terminal.integrated.env.linux": {
"PYTHONPATH": "<absolute path>"
},
Note: see the Python docs on PYTHONPATH on what exactly needs to be specified here.
Or, as the guide says, you can also specify a .env file.

Related

How to get vscode to use `conda activate` instead of `source activate`?

When opening a new terminal in vscode, if I have
"python.terminal.activateEnvironment": true
and I already selected a Python interpreter (see related question here and documentation here), then the terminal opens and automatically activates the environment using
source /path/to/the/environment/bin/activate /path/to/the/environment
I can turn this behavior off by setting python.terminal.activateEnvironment to false.
Of course, I can also manually activate the environment using
conda activate /path/to/the/environment
How to get vscode to use conda activate automatically instead of source activate?
Using source as vscode does sets the activated environment as the "base", e.g. if I do conda env list I will see something like:
conda env list
# conda environments:
#
/path/to/the/actual/base/environment
base * /path/to/the/environment
and this causes some issues. For example, if I try to use mamba which I have in my "true" base environment but not in the other one, it will cause a "No such file or directory" error.

Stop automatic `conda activate` when opening a terminal in VS Code

VS Code has started running conda activate every time I open a terminal in VSCode, be it PowerShell, WSL, or CMD.
I never set this up intentionally so have no idea why it does this or how to disable it. I've looked at all my settings in VS Code and cannot find anything.
How do I stop VS Code from running conda activate when a new terminal is opened?
Try putting the following in your settings.json file:
"python.terminal.activateEnvironment": false
You're getting this behaviour because the default value of that setting is true if not specified.
For more info, see VS Code's docs on Using Python environments in VS Code- in particular, the Working with Python interpreters
section, and the Environments and Terminal windows
section.
Quoting from that page:
Tip: To prevent automatic activation of a selected environment, add "python.terminal.activateEnvironment": false to your settings.json file (it can be placed anywhere as a sibling to the existing settings).
Reading the changelog, this setting was added in version 2018.9.0 of the Python extension for VS Code. The PR that added it was #1387.

Laragon terminal in VSCode with Laragon PATH environment variable

How to use the Laragon terminal in VSCode with Laragon PATH environment variable (on Windows)?
I want to use the Laragon terminal in Visual Studio Code.
I want to use the local Laragon PHP, Composer, MySQL, Node.js, etc. in the terminal.
Open Laragon, right click > Tools > Path > Manage Path.
Copy everything between the --- Laragon --- and the next section (probably --- User ---).
I also put the three lines starting with C:\\Users\\[YOUR_USERNAME]\\AppData\\ at the start, so the Laragon composer and npm are used instead of the global ones.
Put all the paths on one line (they should be separated by a semicolon), and double all the backslashes.
Open your project settings.json, and add these values:
"terminal.integrated.shell.windows": "C:\\[PATH_TO_LARAGON]\\Laragon\\bin\\git\\bin\\bash.exe",
"terminal.integrated.env.windows": {
"PATH": "[PASTE_THE_PATHS_HERE];${env:PATH}"
}
The first line makes the Laragon terminal default.
The second one makes the terminal use the Laragon paths/binaries.
6. You can also add the Laragon php path as the language validation executable:
"php.validate.executablePath": "C:\\[PATH_TO_LARAGON]\\Laragon\\bin\\php\\php-8.0.3-Win32-vs16-x64\\php.exe"
On my computer settings.json looks like this:
{
"terminal.integrated.shell.windows": "C:\\[PATH_TO_LARAGON]\\Laragon\\bin\\git\\bin\\bash.exe",
"terminal.integrated.env.windows": {
"PATH": "C:\\Users\\[YOUR_USERNAME]\\AppData\\Local\\Yarn\\config\\global\\node_modules\\.bin;C:\\Users\\[YOUR_USERNAME]\\AppData\\Roaming\\Composer\\vendor\\bin;C:\\Users\\[YOUR_USERNAME]\\AppData\\Roaming\\npm;C:\\[PATH_TO_LARAGON]\\Laragon\\bin;C:\\[PATH_TO_LARAGON]\\Laragon\\bin\\apache\\httpd-2.4.35-win64-VC15\\bin;C:\\[PATH_TO_LARAGON]\\Laragon\\bin\\composer;C:\\[PATH_TO_LARAGON]\\Laragon\\bin\\git\\bin;C:\\[PATH_TO_LARAGON]\\Laragon\\bin\\git\\cmd;C:\\[PATH_TO_LARAGON]\\Laragon\\bin\\git\\mingw64\\bin;C:\\[PATH_TO_LARAGON]\\Laragon\\bin\\git\\usr\\bin;C:\\[PATH_TO_LARAGON]\\Laragon\\bin\\laragon\\utils;C:\\[PATH_TO_LARAGON]\\Laragon\\bin\\mysql\\mysql-5.7.24-winx64\\bin;C:\\[PATH_TO_LARAGON]\\Laragon\\bin\\nginx\\nginx-1.16.0;C:\\[PATH_TO_LARAGON]\\Laragon\\bin\\ngrok;C:\\[PATH_TO_LARAGON]\\Laragon\\bin\\nodejs\\node-v12;C:\\[PATH_TO_LARAGON]\\Laragon\\bin\\notepad++;C:\\[PATH_TO_LARAGON]\\Laragon\\bin\\php\\php-8.0.3-Win32-vs16-x64;C:\\[PATH_TO_LARAGON]\\Laragon\\bin\\putty;C:\\[PATH_TO_LARAGON]\\Laragon\\bin\\redis\\redis-x64-3.2.100;C:\\[PATH_TO_LARAGON]\\Laragon\\bin\\telnet;C:\\[PATH_TO_LARAGON]\\Laragon\\usr\\bin;${env:PATH}"
},
"php.validate.executablePath": "C:\\[PATH_TO_LARAGON]\\Laragon\\bin\\php\\php-8.0.3-Win32-vs16-x64\\php.exe"
}
You can check what's in the PATH environment variable by opening the terminal and typing env.
To check which path to binary is used for php, composer, etc., type which php, which composer, etc.
Of course, [YOUR_USERNAME] should be your username, and [PATH_TO_LARAGON] should be the path to the folder containing Laragon.
Don't forget to change the paths if you start using another versphp ion (of php, apache, ...).
On other operating systems, the solution should be similar, but this answer is for Windows.

How to use Python3 on the VScode terminal?

Is there a way to force VS Code to use only python3? It always defaults to python2.7 no matter what I try. I've tried selecting the correct interpreter as python3.7. When I open up terminal, it immediately uses python2.7, In the settings it is pointing at 3.7, but the built in terminal which is nice, always defaults to 2.7.
First, understand that the integrated terminal of VSCode, by default, uses the same environment as the Terminal app on Mac.
The shell used defaults to $SHELL on Linux and macOS, PowerShell on
Windows 10 and cmd.exe on earlier versions of Windows. These can be
overridden manually by setting terminal.integrated.shell.* in user
settings.
The default $SHELL on Mac is /bin/bash which uses python for Python2.7. So VS Code will just use the same python to mean Python2.7. When you open a bash shell, it will load your ~/.bash_profile to apply custom aliases and other configurations you added into it.
One solution to your problem is edit your ~/.bash_profile to alias python to python3. But I do not recommend this because this affects all your bash sessions, even those outside of VS Code. This can lead to nasty side effects when you run scripts that need python to be the system Python2.7.
You can instead configure VSCode to load its own aliases, for its own integrated terminal. First, create a file named vscode.bash_profile in your home directory:
$ cat ~/vscode.bash_profile
alias python=$(which python3)
On my env, python3 is Python3.7. You can set it to the what's applicable on your env (ex. maybe python3.7). Then, in VS Code, look for the Terminal shell args setting:
and then open your settings.json and add these lines:
"terminal.integrated.shellArgs.osx": [
"--init-file",
"~/vscode.bash_profile",
]
Finally, restart VS Code. The next time you open the VS Code terminal, python should now be using your Python 3 installation. This should not affect your bash session outside of VS Code.
Note that, if you have some custom settings from the default ~/.bash_profile, you may want to copy it over to your ~/vscode.bash_profile, so that you can still use it on VS Code (ex. changes to PATH, git-completion scripts..).

Set global $PATH environment variable in VS Code

I'm defining a custom $PATH environment variable in my ~/.bash_profile (on a Mac), like so:
PATH="$HOME/.cargo/bin:$PATH:$HOME/bin"
However, VS Code of course does not run my .bash_profile, so it does not have my custom paths. In fact, if I Toggle Developer Tools and check process.env.PATH, it doesn't even seem to have /usr/local/bin.
How do I globally set the $PATH environment variable in VS Code?
(I want to set it globally, not per project or per task, since I'm maintaining a lot of small packages.)
If you only need the $PATH to be set in the integrated terminal, you can use VS Code's terminal.integrated.env.<platform> variable (added in version 1.15). Press Cmd+Shift+P (or Ctrl+Shift+P) and search for "Preferences: Open Settings (JSON)". Then add the following entry to the settings file:
"terminal.integrated.env.osx": {
"PATH": "...:/usr/bin:/bin:..."
}
(Replace .osx with .linux or .windows as needed.)
To see your system's $PATH, type echo "$PATH" in Terminal.app, and copy and paste it into the settings snippet above.
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 (bash by default) to have the $PATH you want. For example, my ~/.bash_profile has the following line:
PATH="$PATH:$HOME/bin"
In VS Code, press ⇧⌘P and type install 'code' command if you haven't done so before.
Quit VS Code.
Launch VS Code not by clicking the icon in the dock or in Launchpad, 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 $PATH will reset to the system default. In that case, quit VS Code and re-launch it by typing code.
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"
I am using vscode on macos for C/C++ development in conjunction with CMake.
The vscode extension CMake Tools allows to manipulate environment variables via the configuration properties cmake.configureEnvironment, cmake.buildEnvironment and cmake.environment (acting respectively on the CMake configuration phase, the build phase and both - see docs).
Then you can extend your system PATH with custom paths by adding the following snippet to your user or project settings.json:
"cmake.environment": {
"PATH": "~/.myTool/bin:${env:PATH}"
},
Visual Studio Code is the problem.
No matter how you set your PATH variable in the shell, there are cases where Visual Studio Code will not inherit your PATH setting. If you're using an application launcher like LaunchBar to start Visual Studio Code, your PATH variable will not be inherited.
Here is a system-wide fix:
In the /etc/paths.d directory, create a file with your Unix username. In that file, place the additional paths that Visual Studio Code needs to work. In my case, this is the contents of my /etc/paths.d file:
/usr/ucb
/opt/local/bin
/opt/local/sbin
~/go/bin
Note: Your /etc/paths.d file will be processed system-wide. Since most systems are single-user, this shouldn't be a problem for most developers.
Since this is the top Google search result for variants of "VS Code path", I will add my answer here.
I'm running Linux and my problem was that VS Code couldn't find some executable needed to build my project. I was running VS Code from the quick launcher (ALT+F2), and not from a Terminal. I tried modifying the PATH variable in many different places, but I couldn't seem to get it right.
In the end, placing the right PATH inside of ~/.zshenv is what worked. It's because .zshenv is the only file that gets sourced for non-interactive shell command execution like from inside of VS Code (more detailed explanation here https://unix.stackexchange.com/questions/71253/what-should-shouldnt-go-in-zshenv-zshrc-zlogin-zprofile-zlogout )
This was even easier to fix than the above answers suggested.
Open VSCode Settings (Ctrl + ,) and search for terminal.defaultProfile.
I updated my Terminal > Integrated > Default Profile: Windows.
It was set to null by default. As soon as I changed it to PowerShell and restarted the terminal, it picked up my system's path variables!
What did the trick in my case (Linux Mint 19.3 Cinnamon, VS code installed via snap) was to put my appended PATH in ~/.profile . Since this file is read at the beginning of a user session, don't forget to logout/login or reboot after editing this file.
I'm working with ubuntu 18.04. I had a similar problem, my enviroment variables were defined and the terminal knows the $PATH but when I tried to debug with golang, go libraries were not found in $PATH variable.
So, to solve it I uninstall the default version from ubuntu software and install manually using the following instructions:
https://code.visualstudio.com/docs/setup/linux
It works for me.
As of VS Code v1.63.2, you can proceed with Ctrl + Shift + P and then type Open Settings (JSON), and simply add the following line.
"terminal.integrated.inheritEnv": true
In my case the code was already there, but set to false. After changing it, everything was fine.
Getting Code to load your existing ~/.bash_profile would be best. I think the docs here are the relevant reference:
https://code.visualstudio.com/docs/editor/integrated-terminal#_linux-os-x
Typically $SHELL is your primary shell on Unix-like systems so you
probably won't want to change the shell. You can pass arguments to the
shell when it is launched.
For example, to enable running bash as a login shell (which runs
.bash_profile), pass in the -l argument (with double quotes):
// Linux "terminal.integrated.shellArgs.linux": ["-l"]
// OS X "terminal.integrated.shellArgs.osx": ["-l"]
Although, it looks like that setting is the default on my current VS Code (OS X) setup. Integrated terminal is running my ~/.bash_profile without any changes to the configuration. Perhaps try adding echo Executing .bash_profile... to test if it's running when a new terminal is opened in Code.
Add the following to your ~/.bash_profile:
launchctl setenv PATH $HOME/.cargo/bin:$PATH:$HOME/bin
Or run a Bash script when needed, e.g.:
#!/bin/bash
set -Eeuxo pipefail
proj_path=$( cd $( dirname ${BASH_SOURCE[0]} ) && pwd )
launchctl setenv PATH $proj_path/bin:${PATH:-}
For me it's resolved by editing the .desktop file.
Originally I have
Exec=/usr/bin/code-oss --unity-launch %F
. Just changed to
Exec=zsh -c "source ~/.zshrc && /usr/bin/code-oss --unity-launch %F"
since I use zsh, instead of bash. But if you have the same problem with bash, simply replace zsh with bash. And shortcuts from your desktop environment should be fixed.