AuroraS
AuroraS

Reputation: 59

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

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?..

Upvotes: 1

Views: 3811

Answers (6)

VISHAL KUMAR YADAV
VISHAL KUMAR YADAV

Reputation: 1

python -m pip install numpy

this is working for me

Upvotes: 0

Steven-MSFT
Steven-MSFT

Reputation: 8431

Are you taking conda run to execute the python file, isn't it? like this:

conda run -n env01 --no-capture-output --live-stream python c:/Work/python3.10/hello/a.py

It's the new update of Python Extension:

Use conda run for conda environments for running python files and installing modules. (#18479)

changelog(28 February 2022).

But as you have found, it will not take advantage of the sub environments under Anaconda3. It's using the base environment. If you execute it directly like python pythonFileName.py in the terminal, you will found it works.

It looks like an issue of Python Extension after the update, I have submitted a bug on GitHub, you can refer to here for more details.

Update:

Workaround:

  1. Set "python.terminal.activateEnvironment" to false in the settings.json.
  2. Downgrade to the previous version of the extension which works fine(avoid conda run).
  3. Try out the following VSIX which has the potential fix: https://github.com/microsoft/vscode-python/suites/5578467772/artifacts/180581906, Use Extension: Install from VSIX command to install the VSIX.

Reason:

Conda has some problems:

conda run -n MY-ENV python FILE.py uses the base interpreter instead of environment interpreter.

Running using conda run inside already activated environment should be the same as running it outside

conda run does not remove base environment components from $PATH

Upvotes: 1

AuroraS
AuroraS

Reputation: 59

Thank you very much for the help!

I solved the problem for now using this guide: https://www.youtube.com/watch?v=_CO-ND1FTOU

Firstly, I uninstalled conda and homebrew on my computer and then installed miniforge.

It works for now at least!

Upvotes: 0

sam
sam

Reputation: 1896

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/

Suggestion: Download Anaconda Package and install it.

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.

Upvotes: 1

FlyingTeller
FlyingTeller

Reputation: 20599

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

Upvotes: 1

InfernoCheese
InfernoCheese

Reputation: 94

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!

Upvotes: 1

Related Questions