Jupyter not recognising numpy (using homebrew pyenv)

Viewed 437

I followed these instructions to get a python virtual environment (managed by pyenv) working in a Jupyter Notebook.

In summary, I did

brew install pyenv
pyenv install 3.8.5
pyenv virtualenv 3.8.5 myproject
pyenv shell myproject 
pip install jupyter notebook matplotlib pandas numpy scipy

Then I started up jupyter notebook from terminal (I tried both jupyter notebook and jupyter lab). When I typed import numpy as np, I get

---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
<ipython-input-1-0aa0b027fcb6> in <module>
----> 1 import numpy as np

ModuleNotFoundError: No module named 'numpy'

But in the very next cell, if I evaluate !pip install numpy, I get

Requirement already satisfied: numpy in /Users/<my_username>/.pyenv/versions/3.8.5/envs/myproject/lib/python3.8/site-packages (1.19.1)

And also, !which python and !which pip return

/Users/<my_username>/.pyenv/shims/pip
/Users/<my_username>/.pyenv/shims/python

How is it even possible that in the very same jupyter notebook, it can't find numpy but pip says numpy is already installed? I can never seem to get my Python environments to just behave!

1 Answers

I had the exact same issue. I think it's all due to your "varying pip list". You probably have installed bunch of libraries via pip install before you created the shims path (prior your pyenv installation). You may also have installed some others after the shims creation. So your jupyter-notebook and jupyter-lab may not have access to the priorly installed libraries.

Since you have installed pyenv via homebrew, without touching anything else just re-installing the jupyterlab via homebrew might take care of it.

I used this:

brew install jupyterlab

And it solved my problem.

source: https://formulae.brew.sh/formula/jupyterlab

Related