Activate conda environment stopped working in vscode - visual-studio-code

I have noticed that conda activate environment stopped working in vscode integrated terminal with no obvious reason (I certainly did not install any new extensions, nor updated conda or vscode in the last few days).
Expected behaviour
After running conda activate environment I want the environment to be activated and be capable of using the environment python interpreter. Essentially it should look like this:
(base) lpravda-ml:~ lpravda$ conda activate rdkit-env
(rdkit-env) lpravda-ml:~ lpravda$ which python
/Users/lpravda/anaconda3/envs/rdkit-env/bin/python
This is what I can achieve using non-vscode terminal and work with conda as usual.
Actual behaviour
This happens when I type in the same set of commands in vscode integrated terminal:
(base) lpravda-ml:~ lpravda$ conda activate rdkit-env
(rdkit-env) lpravda-ml:~ lpravda$ which python
/usr/local/bin/python
The environment seems to be switched on, however python still points to the system install python 2.7 and I have no idea why.
It used to be working like for years and now it broke down.
Note: When I run debugger in vscode the correct conda environment is selected and executed.
My setup:
OS: Mac OS X: 10.14.3
vscode: 1.31.1
conda: 4.6.7
Update
ongoing issue with the following update:
OS: Mac OS X: 10.14.3
vscode: 1.33.3
conda: 4.6.8
Any idea what could possibly broke down?

it is issue which you can read about it here, but I find this solution
which solve the issue in this version. Also, as community said the problem with python interpreter and env gonna fix in next major update.

Create an workspace folder
Inside the create file post the following 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,
}
}
Save configuration and close vscode
Open vscode.
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

Related

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.

Jupyter notebooks in Visual Studio Code does not use the active virtual environment

I write Python code in Visual Studio Code and run the program from a terminal in which I have activated a virtual environment, and it works fine.
However, if I create notebook cells using #%% and run those interactively, the virtual environment is not used. How can I fix this?
It's because there is an extra step needed - you need to explicitly install a Jupyter kernel that points to your new Python virtual environment. You can't simply activate Jupyter-lab or Notebook from the virtual environment. This has tripped me up before, too.
Follow the advice here: Using Jupyter notebooks with a virtual environment
And, in fact, there can be an issue where your kernel still doesn't point to the correct Python binary, in which case you need to change one suggestion in the above advice process:
From: ipython kernel install --user --name=projectname
To: python3 -m ipykernel install --user --name=projectname
(This correction comes from a comment to Jupyter Notebook is loading incorrect Python kernel #2563.)
*and don't forget to restart VSCode
All you need is to edit Vscode settings following these steps:
Open Open User settings using shortcut Ctrl + Shift + P
Type in search space "env"
Under Extentions -> Python , you will find Python: Venv Path
Type the absolute path to your enviroment "path/to/myenv/bin" in linux or "path/to/myenv/Script/"
Restart vsCode
Select the desired kernel using Notebook : Select Notebook kernel using shortcut Ctrl + Shift + P
Read more here: https://techinscribed.com/python-virtual-environment-in-vscode/
For VSCode, your Jupyter kernel is not necessarily using the same python interpreter you're using at the command line.
Use Ctrl + Shift + P to open the Command Palette, and select "Notebook: Select Notebook Kernel"
Then choose the interpreter you're using at the terminal
I find it easy to use pipenv install ipykernel to set up the virtual environment with the Jupyter kernel in one go (comment on rocksteady's answer).
Encounter the same behaviour. Python code works perfectly fine, but Jupyter refuses to pick up the local .venv.
The local venv is in Python: Select Interpreter but not in Jupyter's Select kernel list.
The problem is there're too many venv in the system!
If you encounter the same behaviour,
Press F1, then Jupyter: Filter kernels, uncheck everything, except the local env.
Then F1 -> Developer: Reload Window.
Jupyter will automatically use the default local venv.
for me solved by adding the path of my venv to the settings.json,
now the kernel is detected automatically
"python.pythonPath": "P:\Miniconda3_64bit\venv\Scripts\python.exe",
Make sure you have installed jupyter, notebook, ipykernel libraries in your virtual environment.
Then hit Ctrl + Shift +P , press >Python: Select Interpreter and choose your path of the venv.
After that, hit Ctrl + Shift +P again, run >Notebook: Select Notebook kernel.
If you have already opened the jupyter notebook window , reload it again and you can find your path of the venv in Jupyter's Select kernel list.
On macOS I have .venv/ in the same folder as my .ipynb
. .venv/bin/activate
pip install ipykernel
Then I restart VSCode in the project folder, open the notebook, and in the Select Kernel dropdown I see .venv/bin/python
Selecting that, now it works.
Here is how to do for venv with Jupyter Notebook on VSCode in Windows:
Create a venv and get the path to this venv in Windows. As an example, with Anaconda, I get: C:\Users\rascoussier\Anaconda3\envs\research310.
Now, we need to tell VSCode to use it. In VSCode, go to the Python Extension > Extension Settings. Search for Python: Venv Path. Add the path where the venvs are located. Here we added C:\Users\rascoussier\Anaconda3\envs\research310.
Restart VSCode.
Now launch command pallet(ctrl+shift+P) and run >Notebook: Select Notebook kernel. Normally the venv python should be available and it should then works.
Try a few things:
Make sure you can run the code from a Visual Studio Code terminal using the "ipython" prompt with the same Conda environment.
If it works then sometimes it is a caching issue, so close your file and open a new one.
Now let me show you a scenario. You select the interpreter in Visual Studio Code, and then you write codes below '# %%'. The moment you hit Ctrl + Enter, you are guessing that the IPython kernel that Visual Studio Code is using is not of the interpreters that you have selected. In this case you could write the following code to conform which interpreter is used for IPython kernel.
import sys
print(sys.executable)
This shows the executable path that the IPython kernel is using. If you see that it's not taking the correct interpreter then here's something that worked for me.
Just restart your computer. Then reopen Visual Studio Code and reselect the interpreter and again hit Ctrl + Enter. Now this time Visual Studio Code should take the correct interpreter and its IPython kernel.
See the final output image
If this happens while using WSL server, don't forget to install Python in the WSL as well, because it doesn't come automatically from the local installation to the server.
https://code.visualstudio.com/docs/remote/wsl-tutorial
Another alternative is to specify the folders where the environmental variables should be sought for.
Create your virtual environment using conda create --name ENV_NAME e.g conda create --name pwd
Then, conda activate pwd
It Should print out details like this:
Use that environment location
Edit Vscode settings following these steps:
Open Open User settings using shortcut Ctrl + Shift + P
Type in search space "env"
Under Extentions -> Python , you will find Python: Venv Folders
(See the image below)
Try installing the Anaconda Extension pack.
When I code in Visual Studio Code with this extension in the bottom left corner, I can select the virtual environment I want to execute my code in. Hence installing this package should make the trick.

How to use zsh shell when debugging in VS Code

I run different node versions to build different web apps on my machine. I use nvm to manage the environments.
I set the integrated terminal in VS Code for osx to zsh as so:
"terminal.integrated.shell.osx": "/bin/zsh",
In the integrated terminal It appears to be running zsh, my nvm config runs etc.
But when I run the VS Code Debugger it doesn't pick up my nvm version.
When I run echo $SHELL in the integrated terminal I get /bin/bash even though it appears to be running /bin/zsh. In iTerm2 i get /bin/zsh as expected.
I need the debugger to run in zsh so that the nvm config is picked up and the build step is run in the correct node version. Otherwise node-sass has a fit etc and my debugging environment is out of whack with dev and prod.
How can I get the VS Code dubugger to run in zsh?
BTW: I am using the chrome debugger extension which appears to disallow the out-the-box node version debugger config such as "runtimeVersion": "9.4.0".
Add next to your settings.json, respecting your OS.
This covers both normal and debug terminals
"terminal.integrated.profiles.osx": {
"zsh": {
"path": "zsh",
"args": []
}
},
"terminal.integrated.defaultProfile.osx": "zsh"

I was messing with Visual Studio Code and now all terminals open with (base) environment

As the title says I was messing with Visual Studio Code while I was checking the debugging functionality.
I obviously did something wrong because since then every new terminal I open, both on VSC and Ubuntu terminal opens with (base) environment
Any ideas how to fix it?
Somewhere you have run conda init, which adds an entry to your .bashrc file to automatically activate the base environment whenever you start a bash terminal.
If you want to undo this change completely, you can run:
conda init --reverse
However, this will mean that you can't run conda activate, for example.
If you just want to prevent conda from activating the base environment when you start your shell session, you can run:
conda config --set auto_activate_base false
This will create a .condarc file in your home directory, with an entry of auto_activate_base: false. This will override any conda defaults.

"Running cells requires Jupyter notebooks to be installed" error in VS Code

I'm trying to use the new Jupyter integration for the Python extension in VS Code, and I'm getting the above error even though I have Jupyter installed and it works fine from the command prompt.
Here's my environment:
Python extension version 2018.10.1, and I see Run Cell/Run All Cells tooltips above #%% comments.
I've used the Python: Select Interpreter command to select my Anaconda environment, which is at ~/AppData/Local/Continuum/anaconda3/python.exe.
I have Jupyter installed in that interpreter (jupyter.exe is in the Scripts sub-folder under that location), and it runs fine with the jupyter notebook command at the Anaconda prompt.
But whenever I click on Run Cell or press shift-enter, I get this error message:
"Running cells requires Jupyter notebooks to be installed." Source: Python (Extension)
Is there something else I need to do to configure this?
You may give one try by restarting VS Code in following mentioned way [ It worked for me. ]
Open Bash or any other cmd
Activate any conda environment [ See below command ]
source activate base [ means activate base environment ]
Run VS Code instance [ See below command ]
code .
Now when you'll click on Run Cell or press shift-enter, it should work.
The problem is an issue in the VS Code python extension itself. There are a number of issues related to this open in the repository: #3354, #3343, #3330, and the issues are being worked on, see #3374.
The reason, as far as I understand, is that in this case - and in some other cases - the anaconda environment is not activated before running the command. Situations where the environment is activated are e.g. opening a python terminal or running a file in the python terminal, but this also needs to happen for Jupyter, Tests, and so on.
While theoretically, adding the Scripts folder to your PATH, as David mentioned, could help, it did not help in my case. This may just not be enough to properly reflect what happens on activation.
My guess is that we will have to wait for this issue to be resolved in the repo, but if someone else finds a workaround, I'd be happy.
Simply running vscode from the activated environment did not work for me, here is what did:
In terminal (bash) I ran:
conda activate <environment-name>
conda install jupyter notebook
When the install finishes, open vscode from terminal (the same shell with activated environment) with the command:
code .
Notes:
Replace '.' with the path to the directory you want to open if it's not the current directory.
I've written 'conda install ...' but mamba also works.
If the terminal command for 'code ' does not work, it's likely you need to add it to environemnt variables; in such a case, this post might help.