Home > Mobile >  No module named 'numpy' for Mac OS in Visual Studio code using pyenv
No module named 'numpy' for Mac OS in Visual Studio code using pyenv

Time:03-08

I have a problem with numpy.

I receive this when I try to run my main.py file in visual studio.

import numpy as np
ModuleNotFoundError: No module named 'numpy'
ERROR conda.cli.main_run:execute(33): Subprocess for 'conda run ['python', '/Users/Bruker/Documents/Prosjektoppgave/PPO/main.py']' command failed. 

(See above for error)

I have created a virtual environment and I am using 3.8.12 ('mlp': conda) which is also the environment I am in with the terminal.

I am using a Macbook and trying to use tensorflow from visual studio code, but the code stops at import numpy as np.

If I run pip install numpyI receive the following message:

Requirement already satisfied: numpy in /opt/homebrew/Caskroom/miniforge/base/envs/mlp/lib/python3.8/site-packages (1.22.2)

And if I run

import sys
print("version: ", sys.executable)

I receive

version:  /opt/homebrew/Caskroom/miniforge/base/bin/python

What should I do?..

CodePudding user response:

One of the issues depends on how you are running the code. In VS Code you have to ensure that it is using the correct environment Python you have set up with pyenv. Typically, it will run with the global Python interpreter unless configured to use another.

From the docs:

Global and virtual environments# By default, any Python interpreter that you've installed runs in its own global environment, which is not specific to any one project.

You can read here about how to correctly set it up in VS Code.

Another issue may be that your pip is not installing it for the environment but globally. In other words, you are running with the correct environment but incorrect pip. In that case you can run:

python -m pip install numpy

Which will use pip that is linked to the python environment you are currently using.

Hopefully that helps!

CodePudding user response:

Few points you must note here

  1. You said you using conda python
  2. /opt/homebrew/Caskroom/miniforge/base/bin/python shows that this is Python installed from Homebrew

This could be the root cause issue you are dealing with not missing numpy. In Mac, it is very important to stick to one python version. Please reconfigure your Python Paths.

For your conda environment, you can check https://docs.anaconda.com/anaconda/user-guide/tasks/integration/python-path/

Since you are working with TensorFlow, I would recommend you to stick to only Conda updates, than using any pip install for better package dependency checks and properly planned package updates.

CodePudding user response:

The interpreter you use is

/opt/homebrew/Caskroom/miniforge/base/bin/python

but you are under the assumption that you are using the mlp env and have also installed to that env:

Requirement already satisfied: numpy in /opt/homebrew/Caskroom/miniforge/base/envs/mlp/lib/python3.8/site-packages (1.22.2)

So your visual studio code is not set up correctly, as it should be using

/opt/homebrew/Caskroom/miniforge/base/envs/mlp/bin/python
  • Related