No module named 'numpy': Visual Studio Code

Question:

I’m trying to setup Visual Studio Code for python development

to begin with, I’ve installed

  1. Anaconda Python
  2. Visual Studio Code

and in a new file I have the following code

import numpy as np
import pandas as pd
from pandas import Series, DataFrame

upon hitting Ctrl+Shift+B I get the following error

import numpy as np

ImportError: No module named ‘numpy’

Also, is there python interactive window in VS Code? How to open it.

Asked By: billboard

||

Answers:

You may not have numpy installed on the version of python you are running.

Try this:

import sys

print(sys.version)

Is the printed version Anaconda? If you installed Anaconda python, it should come with numpy already installed. If it turns out to be another version of python you are accessing inside Visual Studio Code that doesn’t have numpy installed, then that’s what you need to fix.

The version of python that is called depends on which version of python comes up in your PATH variable first. Type into a terminal: echo $PATH.
The output should look like this with Anaconda bin first:
/Users/jlzhang/anaconda/bin:/usr/local/bin:/usr/bin:/bin

If you do not have Anaconda bin first, you can add this to your ~/.bashrc file:
echo

# Use Anaconda python

export PATH="/Users/jlzhang/anaconda/bin:$PATH"

Restart a terminal and Visual Studio Code and see if you are now running Anaconda python.

Hope it helps/ Did it work?

Answered By: user3835290

Changing python environment in VS code helped me. Default the visual studio code takes original Python environment, it requires numpy to install. If you have anaconda python (numpy comes with it) installed, you could switch the original python environment to anaconda python environment in visuals studio code. This can be done from the command palette Ctrl+Shift+P in visual studio

Check this link for how to switch from original python to anaconda python environment, specifically:

Snippet from VSCode instructions
enter image description here

Answered By: Madhukar Reddy

We’ve found that simply changing the environment worked. Right-click on “Python Environments” and choose “Add/Remove Python Environments” and choose 3.5 instead of 3.6:

Add/Remove Python Environments

Answered By: Informitics

On my laptop, i found that there are some version of python installed (checkout picture that i inserted below)

Picture for some version of python installed

One of them is python 3.7.6 which installed together when i installed Anaconda (I installed it the day before). I assumed that it was the updated version of python. So i changed my Jupyter kernel to that version of Python.
It works for me.

Answered By: Yanwardo Sitanggang

select the appropriate version

click on python and select the appropriate version.
your issue will be solved

Answered By: user14000463

Choose Python in the dropdown(default is PowerShell) and execute pip install NumPy pandas etc.

Dropdown

Answered By: user14208025

I used to get the same error.

if you installed python and pip then run this code on the vs code terminal ‘pip install numpy’.

Answered By: Enes

if you are at VSC jupyter, you should check this:

Like this (You can see this at the top right)

Answered By: Mahdi Ebi

you can do something else
in cmd type :

cmd) where pip

and cmd return some address
after you should install NumPy for all addresses and make sure installed NumPy in all them ->

cmd) ADDR1 install numpy

cmd) ADDR2 install numpy

… and all addresses

Answered By: Mahdi Ebi

If you are using jupiter notebook in VS Code, you need to select proper evvironmemt for it:
enter image description here

Answered By: TSV

You have to make sure VSCode selects the python interpreter bundled with Anaconda. If using Anaconda, please do not pip install your science packages.

The solution is as follows for me with Anaconda:

  1. To select a specific environment, use the Python: Select Interpreter command from the Command Palette (Ctrl+Shift+P).

enter image description here

  1. Select python bundled by Anaconda
    Select the correct interpreter..

Numpy, scipy, and the like should now no longer have a warning!

Answered By: Jeremy Jao

If you have tried pip install numpy and it is not working,copy paste this to your cmd

pip install numpy  --trusted-host=pypi.python.org --trusted-host=pypi.org --trusted-host=files.pythonhosted.org
Answered By: waithira

I’m my case I was receiving a message in the integrated terminal of Visual Studio Code:

ModuleNotFoundError: No module named 'scipy'

But the terminal was showing that scipy was installed:

Requirement already satisfied: scipy in ./opt/anaconda3/lib/python3.9/site-packages (1.7.1)

I discover that is because the vscode integrated terminal was using the default python version installed before Anaconda. The steps provided in others answers, like change the interpreter, did not solved my problem. So, I just deactivated Anaconda:

conda deactivate

I Installed the desired module in the default python installation:

pip install scipy

And then I reactivated conda:

conda activate

After it, everything works now.

Answered By: Leonardo

1.Numpy have showing error when importing at VS CODE

When i Run the code it show No module named ‘numpy’ .Although i check by pip install numpy it show satisfy that its already intsall

Then i found solution
1.Press Ctrl+shift+P
2.type python interpreter and click on it.
3.Then choose The Global interpreter shown in image attached.
4.again run the code no error.
No Error,Done 100%

Answered By: shaf Khattak

Even after conda(Anaconda) is installed and the anaconda version of python is selected(using ctrl+shift+p and selecting Python: Select interpreter) on the VS code python extension interpreter the version of the kernel used to run the notebook can be different. Below is when the default installation of python is selected on the top right corner of VS code: Numpy module not found with default python kernel
But when the anaconda version of python is selected on the top right corner of VS code, the numpy module is imported and runs(look for the currently selected one on the top menu).
enter image description here

Note after installing the python extension on VS code and selecting the python interpreter, it will automatically load and show you which interpreters you have on your system to choose from.

Answered By: Michaeluuk