question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Python Code Cell in Jupyter Notebook Cannot Use Conda Virtual Environment

See original GitHub issue

Seemingly similar issue to https://github.com/microsoft/vscode-jupyter/issues/2616, filing a new issue on @DonJayamanne’s request.

Environment

  • VS Code: v1.51.1
  • Jupyter Extension: v2020.11.392013122
  • Python Extension: v2020.11.371526539
  • OS: Windows 10 Pro v1909
  • Anaconda: v2020.07 (anaconda), v1.7.2 (anaconda-client)

Description

I am using a Jupyter notebook with Python code cells in VS Code. After creating a new virtual environment using the standard Anaconda steps, I select the appropriate Python interpreter via the VS Code GUI.

I have registered the virtual environment with Jupyter using python -m ipykernel install --user --name=myenv and my virtual environment has the jupyter library installed. Note that the Jupyter, Anaconda, and Python extensions are all installed in VS Code as well.

I am able to use conda activate myenv and run code directly in the terminal perfectly fine. Unfortunately, when attempting to run the same code in the code cell inside the Jupyter notebook, I am unable to activate myenv; instead, I see that base is activated when I run !conda info --envs and !where python.

Would appreciate any suggestions.

Steps to Reproduce

In VS Code terminal (using Command Prompt):

  1. conda create -n myenv python=3.8 pandas numpy matplotlib jupyter notebook

  2. conda activate myenv

  3. python -m ipykernel install --user --name=myenv Output: Installed kernelspec myenv in C:\Users\ayush\AppData\Roaming\jupyter\kernels\myenv

  4. conda info --envs

Output:

# conda environments:
#
base                     C:\Users\ayush\anaconda3
myenv                 *  C:\Users\ayush\anaconda3\envs\myenvp

In new Jupyter notebook test.ipynb:

  1. Select myenv as the Python interpreter via the VS Code GUI. drawing

  2. In Python code cell: !conda info --envs

Output:

# conda environments:
#
base                  *  C:\Users\ayush\anaconda3
myenv                    C:\Users\ayush\anaconda3\envs\myenv

If I install additional libraries in myenv (e.g. PyTorch), they are unable to be loaded within the Jupyter notebook. For example, see below:

drawing

Thank you all for your help.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

3reactions
DonJayamannecommented, Dec 2, 2020

If I attempt to then run a code cell, I get the following error pop up message: StdErr from ShellExec,

@IanMatthewHuff this is a known bug in core extension. Looks like some recent bug in core extension. Will find issue

0reactions
greazercommented, Dec 3, 2020

Dupe of #4011

Read more comments on GitHub >

github_iconTop Results From Across the Web

Get Your Conda Environment to Show in Jupyter Notebooks
1. First, install nb_conda_kernels in your base environment. Once this is installed any notebook running from the base environment will ...
Read more >
How to use Jupyter notebooks in a conda environment?
Jupyter runs the user's code in a separate process called kernel. The kernel can be a different Python installation (in a different conda...
Read more >
How to add your Conda environment to your jupyter notebook ...
In this article I am going to detail the steps, to add the Conda environment to your Jupyter notebook. Step 2: Activate the...
Read more >
You Must Know This Little Technique If You Use Jupyter ...
An easy way to change your virtual environment without leaving Jupyter Notebook. Note: This article needs some basic knowledge about Python virtual environments...
Read more >
Jupyter Notebook wrong directory. Unable to import seaborn
See Why Can't Python Find My Modules? – Real Python , which leads to Working With Python Virtual Environments. Even if some Anaconda/Windows...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found