简体   繁体   中英

VS Code Jupyter Notebook not importing packages

I am new to Python and I am trying to start with some easy machine learning projects. I am trying to import the packages sys, scipy, numpy, matplotlib, pandas, and sklearn to a visual studio jupyter notebook. I am using this test code to see if they import properly:

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__))

When I do this with a jupyter notebook on the website launched from anaconda, it gives me no problems. However, I want to use VS code, but when I run it there, it gives me this:

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

What is going on and how can I fix this so these packages get imported into my VS Code jupyter notebook? I am very new to Python and many things with coding so this may be a really easy fix.

PS If anyone wants to help me learn a little more about using Python with machine learning(interesting in medical image segmentation) don't hesitate to pm me. Just a student trying to learn :)

To execute imported modules in Jupyter notebook in VSCode, we need to install them in the selected environment (upper right corner of Jupyter).

  1. Install the module in the VSCode terminal (use the shortcut key Ctrl+Shift+` to open a new terminal, it will automatically enter the currently selected environment):

    在此处输入图片说明

  2. Implementation:

    在此处输入图片说明

More reference: Jupyter in VSCode .

You have to activate the virtual environment that has packages installed. Initial environment in conda is named 'base'. So, if you are using windows powershell as your terminal run these commands to activate your conda environment.

conda init powershell

then

activate <YOUR_ENVIRONMENT_NAME>

In your case environment name should be 'base'.

If you are using bash in windows environment.

conda init bash

then activate the environment

source activate <YOUR_ENVIRONMENT_NAME>

That should solve your issue.

You can also select default python interpreter for your project at the bottom left in VS Code.

Check the virtual environment docs for more info on environments

It is recommended to create separate environment for each project to avoid version conflicts and keep packages for each project separate.

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