简体   繁体   中英

Visual Studio Code - Interactive window: changing the environment does not work even with jupyter installed

My issue is like this one: How do you change environment of Python Interactive on Vscode? , but the solution provided does not seem to work for me.

I'm on Ubuntu 20.10, using Visual Studio Code 1.53.2 installed from the snap store.

To debug this, I recreated my environment from scratch using conda:

name: mytrips

channels:
  - defaults
  - conda-forge

dependencies:
  - python=3.8.*
  - pip

  - numpy==1.19.*
  - pandas==1.2.*
  - cartopy==0.18.*
  - jupyterlab

(I've put jupyterlab there so that should have all the dependencies required for the interactive window).

On the lower left, the interpreter is correctly set to the environment "mytrips". I'm using a test script that contains only the following two lines:

import cartopy


print(cartopy.__version__)

When I right click and select "Run python file in terminal", this is what (correctly) happens:

source /home/rick/anaconda3/bin/activate
(base) rick@linux:~/Documents/src/My trips$ source /home/rick/anaconda3/bin/activate
(base) rick@linux:~/Documents/src/My trips$ conda activate mytrips
(mytrips) rick@linux:~/Documents/src/My trips$ /home/rick/anaconda3/envs/mytrips/bin/python "/home/rick/Documents/src/My trips/test.py"
0.18.0

So you can see here that cartopy is imported correctly.

When I hit shift+Enter, or right-click and select to run the current file in the interactive window, it returns ModuleNotFoundError: No module named 'cartopy' because it's using python from the base conda environment. Problem 1: I have jupyter in my environment but it behaves as if it's not detected.

Then, when I click on the top-right corner in the interactive window and select the mytrips environment, the Interactive Window prints this as if it has just changed environment:

Started 'Python 3' kernel
Python 3.8.6 (default, Sep 25 2020, 09:36:53) 
Type 'copyright', 'credits' or 'license' for more information
IPython 7.20.0 -- An enhanced Interactive Python. Type '?' for help.

But still can't import cartopy , and if I check in the Interactive Window what executable it is now using, the result is:

import sys
sys.executable
'usr/bin/python3'

So that's problem 2: the Interactive Window does not actually change my environment.

And just for confirmation, here's proof that the environment has jupyter and cartopy :

(mytrips) rick@linux:~/Documents/src/My trips$ conda list | grep jupyter
jupyter_client            6.1.7                      py_0  
jupyter_core              4.7.1            py38h06a4308_0  
jupyterlab                2.2.6                      py_0  
jupyterlab_pygments       0.1.2                      py_0  
jupyterlab_server         1.2.0                      py_0  
(mytrips) rick@linux:~/Documents/src/My trips$ conda list | grep ipython
ipython                   7.20.0           py38hb070fc8_1  
ipython_genutils          0.2.0              pyhd3eb1b0_1 
(mytrips) rick@linux:~/Documents/src/My trips$ conda list | grep cartopy
cartopy                   0.18.0           py38h0d9ca2b_1

Any hints on what's going on?

Seems like a recent problem with already a couple of Issues on VSCode-Jupyter github. The main thread is the link below. I also commented my workaround in this link.

https://github.com/microsoft/vscode-jupyter/issues/4831

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM