Reputation: 1268
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.
Upvotes: 4
Views: 4495
Reputation: 29660
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 andcmd.exe
on earlier versions of Windows. These can be overridden manually by settingterminal.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..).
Upvotes: 1