Visual Studio代码交互窗口:即使安装了jupyter,更改环境也不起作用

2024-09-30 14:34:02 发布

您现在位置:Python中文网/ 问答频道 /正文

我的问题是这样的:How do you change environment of Python Interactive on Vscode?,但提供的解决方案似乎不适合我

我在Ubuntu 20.10上,使用从snap store安装的Visual Studio代码1.53.2

为了调试这个,我使用conda从头开始重新创建了我的环境:

name: mytrips

channels:
  - defaults
  - conda-forge

dependencies:
  - python=3.8.*
  - pip

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

(我把jupyterlab放在那里,这样就应该有交互窗口所需的所有依赖项)

在左下角,解释器被正确设置为环境“mytrips”。 我使用的测试脚本只包含以下两行:

import cartopy


print(cartopy.__version__)

当我右键单击并选择“在终端中运行python文件”时,会(正确地)发生以下情况:

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

因此,您可以在这里看到cartopy被正确导入

当我点击shift+Enter,或者右键单击并选择在交互窗口中运行当前文件时,它返回ModuleNotFoundError: No module named 'cartopy',因为它使用的是来自基本conda环境的python。 问题1:我的环境中有jupyter,但它的行为好像没有被检测到一样。

然后,当我单击交互窗口的右上角并选择mytrips环境时,交互窗口将打印此内容,就像它刚刚更改了环境一样:

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.

但是仍然无法导入cartopy,如果我在交互窗口中检查它现在使用的可执行文件,结果是:

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

这就是问题2:交互式窗口实际上并没有改变我的环境。

为了确认,这里有证据证明环境有jupytercartopy

(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

有什么线索吗


Tags: pysrchomebin环境mylinuxjupyter