Setting
Suppose that I got a project called my_project
that uses a python interpreter from a specific conda environment called my_env
. When I set the interpreter of the project I will get the following settings.json file inside the .vscode
directory of my_project
:
{
"python.pythonPath": "<path to conda>/envs/my_env/bin/python"
}
Problem
With this configuration I would expect VSCode to automatically activate my_env
in the terminal everytime I open my_project
. But typically when I open my_project
only the base
environment of anaconda is activated in the terminal. To activate my_env
I got different options that are all manual.
conda activate my_env
strg+shift+p
and execute create new integrated terminal
Probably there are even more ways of doing it.
Question
How can I avoid this stuff such that VSCode simply activates my_env
automatically inside the integrated terminal when I open my_project
.
Further Informations
Another way out would be to create shortcut for activation.For eg in ~/.bashrc do the following
alias a="conda activate my_env"
alias d="conda deactivate"
So now you need to just type 'a'
to activate and 'd'
to deactivate.
The Python extension for VS Code will pick up and support conda environment as long as you have specified a Python version when you created your conda environment and you gave it a name (see the docs on environments ).
And one way to work around this if you can't create an environment as required above is to use conda run
which will handle the activation for you when running your code.
{ "folders": [ { "path": "." } ], "settings": { "python.condaPath": "/home/pcuser/anaconda3/condabin/conda", "python.pythonPath": "/home/pcuser/anaconda3/envs/my_env/bin/python", "python.terminal.activateEnvironment": true, "python.terminal.activateEnvInCurrentTerminal": true, } }
It will select automatically the conda enviroment (Should appear at the bottom left of vscode). Inside terminal 'my_env' will also be selected. If vscode doesn't select the 'Python Selected Interpreter' before you open the terminal the my_env inside terminal won't be selected. Open the terminal after the my_env is selected by vscode
I do have faced similar problem when I have multiple python project having virtual environments in system.
Since I was switching project based on workload, its difficult to activate right environment for specific project.
As VSCODE contain settings.py which store python path to use in particular project. But was not activating automatically.
I followed below step and its very fast compared to other method I searched.
Step 1. Delete current terminal. You will see bin icon as in below pic.
Step 2. Now start new terminal by shortcut.
CTRL + `
You will see VSCODE auto detect python path settings and activate the environment.
Simple
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.