VS Code Jupyter Notebook 不导入包

Mat*_*and 7 python anaconda visual-studio-code jupyter-notebook

我是 Python 新手,我正在尝试从一些简单的机器学习项目开始。我正在尝试将 sys、scipy、numpy、matplotlib、pandas 和 sklearn 包导入到 Visual Studio jupyter 笔记本中。我正在使用此测试代码来查看它们是否正确导入:

import sys
print('Python: {}'.format(sys.version))
# scipy
import scipy
print('scipy: {}'.format(scipy.__version__))
# numpy
import numpy
print('numpy: {}'.format(numpy.__version__))
# matplotlib
import matplotlib
print('matplotlib: {}'.format(matplotlib.__version__))
# pandas
import pandas
print('pandas: {}'.format(pandas.__version__))
# scikit-learn
import sklearn
print('sklearn: {}'.format(sklearn.__version__))
Run Code Online (Sandbox Code Playgroud)

当我在从 anaconda 启动的网站上使用 jupyter 笔记本执行此操作时,它没有给我带来任何问题。但是,我想使用 VS code,但是当我在那里运行它时,它给了我这个:

P5 C:\Users\matti> conda activate base
conda : The term 'conda' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.        
At line:1 char:1
+ conda activate base
Run Code Online (Sandbox Code Playgroud)

发生了什么事以及如何修复此问题以便将这些包导入到我的 VS Code jupyter 笔记本中?我对 Python 和很多编码方面的东西都很陌生,所以这可能是一个非常简单的修复。

PS如果有人想帮助我更多地了解如何使用Python进行机器学习(对医学图像分割感兴趣),请随时联系我。只是一个想学习的学生:)

Jil*_*eng 12

要在 VSCode 中执行 Jupyter Notebook 中导入的模块,我们需要将它们安装在选定的环境(Jupyter 右上角)中。

  1. 在VSCode终端中安装模块(使用快捷键Ctrl+Shift+`打开新终端,会自动进入当前选择的环境):

    在此输入图像描述

  2. 执行:

    在此输入图像描述

更多参考:VSCode 中的 Jupyter


小智 3

您必须激活已安装软件包的虚拟环境。conda 中的初始环境名为“base”。因此,如果您使用 Windows powershell 作为终端,请运行这些命令来激活 conda 环境。

conda init powershell
Run Code Online (Sandbox Code Playgroud)

然后

activate <YOUR_ENVIRONMENT_NAME>
Run Code Online (Sandbox Code Playgroud)

在您的情况下,环境名称应该是“base”。

如果你在windows环境下使用bash。

conda init bash
Run Code Online (Sandbox Code Playgroud)

然后激活环境

source activate <YOUR_ENVIRONMENT_NAME>
Run Code Online (Sandbox Code Playgroud)

那应该可以解决你的问题。

您还可以在 VS Code 左下角为您的项目选择默认的 python 解释器。

检查虚拟环境文档以获取有关环境的更多信息

建议为每个项目创建单独的环境,以避免版本冲突,并将每个项目的包分开。