Home > OS >  No module named "numpy" (and tensorflow) even though installed
No module named "numpy" (and tensorflow) even though installed

Time:11-12

I have been struggling on this for a while without prevail. I am trying to run a test script with

import numpy as np
array1 = np.array([1,2,3])

However, I get the error "No module named 'numpy'". The same goes for Tensorflow. But trying "pip install numpy" on terminal gives "Requirement already satisfied: numpy in /usr/local/lib/python3.9/site-packages (1.23.4)".

And when I move the script to another directory, in which I already have ran Numpy in, it runs without error. Why does the Numpy module (also Tensorflow) work in that directory, but not in the new one I created?

Tried to uninstall and install Numpy in Terminal

CodePudding user response:

Make sure you pip install and run scripts in the right python environment (can be python-venv or conda env), you can troubleshoot these with which python and which pip or pip --version

You can create venv with

python3 -m venv env

and make sure you are in the correct python environment by activate it:

source env/bin/activate

only then you will install numpy and other library inside this virtualenv

pip3 install numpy

Later on, when you run scripts from this project, make sure you cd into its directory, then source env/bin/activate before running scripts or pip install <libraries>.

CodePudding user response:

Maybe you have two instances of pyhton, like anaconda and python installed at the same time, something similar happened to me.

  • Related