Reputation: 71
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 :)
Upvotes: 6
Views: 24316
Reputation: 10354
To execute imported modules in Jupyter notebook in VSCode, we need to install them in the selected environment (upper right corner of Jupyter).
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):
Implementation:
More reference: Jupyter in VSCode.
Upvotes: 11
Reputation: 591
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.
Upvotes: 3