How to use Python3 on the VScode terminal? - visual-studio-code

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

Related

Change terminal used by Code-Runner extension in VS Code to cmd while workspace default integrated terminal is powershell?

Is there a setting in VS Code for the Code Runner extension that lets you run in a shell that's different than the workspace's default shell?
For context, my default integrated terminal is powershell. I have the code-runner.runInTerminal setting set (see, e.g., here), and whenever I run python using the extension, it creates a new "Code" powershell integrated terminal (unless one already exists), activates my selected python environment, and runs the code. However, when it tries to activate the environment, I get this popup:
So, I'd like to change the shell that Code Runner uses to cmd, BUT I want to keep using powershell as my default integrated terminal for the workspace.
Changing my workspace's default shell to cmd isn't an option, and neither is changing to a third shell like WSL. This issue indicates to me that the environment activation isn't getting fixed in powershell anytime soon. For now, I've done what the issue suggests and have just stopped Code Runner from activating a new environment in the terminals it creates (set python.terminal.activateEnvironment to false), but I'm worried now I won't be able to have Code Runner use any other python environments than the default one. What can I do?

PATH variable in Visual Studio Code different from Terminal on Mac

The PATH variable in the integrated Terminal in Visual Studio Code is different from the one in the Terminal app. How can I change it?
I'm using the Intel Distribution for Python from Intel oneAPI and I have both the setvars.sh script called and the conda initialize code in my .zshrc. In Mac Terminal I can properly activate conda environments. However, in the integrated Terminal in VSC, the /usr/bin folder is listed before the conda folders, so the system Python interpreted is called.
EDIT: I initially thought that the Intel Distribution for Python had something to do with it, but the same issue occurs with a regular Anaconda distribution.
It looks like that the problem is that Visual Studio Code inherits the PATH from Terminal but somehow it rearranges the order of the folder, and this confuses the Anaconda activation script. Since I couldn't find a solution, I implemented a workaround by adding the following lines to my .zshrc, right after the Anaconda initialization script:
# Workaround for Visual Studio Code integrated terminal
if [[ -v VSCODE_GIT_IPC_HANDLE ]]; then
conda deactivate
conda activate
fi
I am not 100% sure the right behaviour as I don't use a Mac but you might try launching VS Code from the terminal which has the right environment.
Make sure that code is in your PATH with https://code.visualstudio.com/docs/setup/mac#_launching-from-the-command-line
Then ensure that all the VScode sessions are closed on your machine and launch it via code from the terminal.

Wrong Python interpreter being used by VS Code

I am on Ubuntu 20.04 and have both Python2 and Python3 installed natively. I have also installed Python through miniforge, a variant of miniconda. In VSCode I have both the MS Python extension and Pylance installed.
I use the miniforge python for my coding. This works perfectly fine in PyCharm.
However in VSCode, when I try to execute the same file I get errors. After investigating it seems that VSCode is picking native Python2 - even though I have the miniforge Python selected. In this picture it can be seen that the status bar at the bottom states Python interpreter selected is Python3. But the output window shows that the python interpreter is Python2.
A more confusing thing is when I use VSCode for Jupyter notebook files then it picks up the interpreter correctly and I have no issues.
I have checked both User and Workspace settings, and they all point to Python3. How can I fix this for standard .py files?
I prefer VSCode to PyCharm, but will need to use PyCharm till this is resolved.
It seems that your system console cannot see python3. You need to put the python3 in the PATH variable, before python2. Like:
PATH=path/to/python3:path/to/python2:$PATH
Also, make sure that the environment containing python3 is activated before command prompt appears. It can be done in bash_profile by adding a line like
conda activate my_env_with_python3
Try changing the settings "Python:Python path", "Python:default interpreter path" and "Python:conda path" also.
I have just bumped into something similar. The Run code option resulted in the file being run with the default interpreter instead of the venv-based one with necessary packages installed.
The fix was simply to use "Run python file" instead:
The run-code behavior must be customizable, something is mentioned e.g. here: Run Code vs Run Python File in Terminal for VSCODE but I didn't bother.

Selecting Python interpreter from WSL

I am using Windows 10 and want to set the the default VSCode interpreter for Python to be the same one used in my WSL 2 (Ubuntu), so that I'm always using "one Python".
When I click "Select Python interpreter" a prompt appears to find the interpreter path, but I'm not sure what the path would be.
I think that, in order to use the WSL python, you need to be running VSCode in a WSL remote window. You need to install the Remote WSL extension first. Then, on the lower left there is a green button that will let you start a WSL window, or it will tell you that you are already in one.
If you are in a WSL window, you should be able to select your python interpreter pretty easily. It will either automatically detect it, or you can run which python3 in an Ubuntu terminal to get the path. If you want to navigate to a file on your Windows file system from within this window, look in, e.g., /mnt/c/Users/<USER_NAME>.
Well probably late to the party but you can find Python you are using on your WSL2 with simple command:
which python3
It will show you where is the python placed.
While above answers work, there are some related things to keep in mind here (or are at least worth mentioning):
I would recommend to create a virtual environment for your project. If you do so, its very easy to find the python interpreter in the bin folder of the venv. (If you want to "find" your WSL folder in windows. Just enter explorer.exe . in your terminal - it will open a windows explorer in the current location.
If you open the python interpreter selector form within VS Code on Windows, be aware that it opens a windows that let's you select "Executables" (meaning WINDOWS wxecutables):
You will not be able to open your python interpreter located in your wsl like that, as the folder will be shown as empty.

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.