Jupyter Notebook ModuleError after Homebrew Upgrade - python

I have been using Jupyter for some time now and it has worked just fine. I have Jupyter and Python installed via Homebrew. I am running on MacOS.
Yesterday, I ran the command brew upgrade and now my Jupyter notebook is unable to find any of the installed python packages. I will use Numpy as the example.
When inside of a Jupyter notebook, I try to do
import numpy
I get the message:
ModuleNotFoundError: No module named 'numpy'
If, however, I launch python in a terminal window, then I can import Numpy without issue.
I first checked that the package was installed correctly by re-issuing the install command
brew install numpy
which outputs:
Warning: numpy 1.18.4 is already installed and up-to-date
To reinstall 1.18.4, run `brew reinstall numpy`
I also ran
pip install numpy
and got:
Requirement already satisfied: numpy in /usr/local/lib/python3.7/site-packages (1.18.4)
Now, this is where I got confused because I expected the path to point to something like /usr/local/Cellar/, so I checked the path inside of the Jupyter notebook:
import sys
sys.path
which outputs:
['/Users/kseuro/Dropbox/Dev/',
'/usr/local/Cellar/jupyterlab/2.1.2/libexec/lib/python38.zip',
'/usr/local/Cellar/jupyterlab/2.1.2/libexec/lib/python3.8',
'/usr/local/Cellar/jupyterlab/2.1.2/libexec/lib/python3.8/lib-dynload',
'/usr/local/opt/python#3.8/Frameworks/Python.framework/Versions/3.8/lib/python3.8',
'',
'/usr/local/Cellar/jupyterlab/2.1.2/libexec/lib/python3.8/site-packages',
'/usr/local/Cellar/jupyterlab/2.1.2/libexec/lib/python3.8/site-packages/IPython/extensions',
'/Users/kseuro/.ipython']
Ok, so Homebrew wants Jupyter to use Python3.8? So I tried brew switch python 3.8 and got:
Error: python does not have a version "3.8" in the Cellar.
python's installed versions: 3.7.7
I feel like I'm out of my depth now and need help figuring out what to do next. I don't want to start by just changing paths around.
Suggestions? Thanks so much.

I figured out what to do — posting the solution for my future self and others who may stumble upon this.
Since Jupyerlab is in its own Cellar, the Python packages need to end up in the
/usr/local/Cellar/jupyterlab/x.y.z/libexec/lib/python3.x/site-packages
directory, where x, y, z are integers, so that the Jupyter kernel can find them.
You can do this by calling:
import sys
!{sys.executable} -m pip install 'package-name'
inside of the Jupyer notebook.
All is well, again.

Related

Pip installed a module I want to use, but still getting "module not found" error

I am trying to use the Python module Gdal. I have run pip install gdal and I recieve the message
Requirement already satisfied: gdal in c:\users\willy\appdata\local\packages\pythonsoftwarefoundation.python.3.9_qbz5n2kfra8p0\localcache\local-packages\python39\site-packages (3.3.1)
and yet when I run import gdal in my Python IDE (I use Spyder), I still get the error no module named gdal
You need to tell Spyder which Python interpreter to use. Point it to the one you're installing the package for, as it seems that it's using a different one.
https://docs.spyder-ide.org/current/faq.html#using-existing-environment
From the documentation it seems you have to call:
from osgeo import gdal
See here
Answer is here in Spyder docs: https://docs.spyder-ide.org/5/faq.html#using-packages-installer
Don't try to use pip install and expect it to work in Spyder. Follow these instructions

Jupyter notebook does not reflect library version change in virtualenvironment

I am new to jupyter notebooks and virtualenvironment. I think that I am incurring in a really trivial problem.
I am trying to import matplotlib in a Jupyter notebook and the import works fine. However, it uses the wrong version of the library (installed also systemwide) and I don't know how to force it to use the one in the virtualenvironment.
In my virtualenvironment I want to use a most recent version of matplotlib and therefore I did
pip install --upgrade matplotlib in my virtualenvironment. The upgrade worked fine.
Now if I do pip show matplotlib in my virtualenvironment I get:
Name: matplotlib
Version: 2.2.5
...
Instead, if I do the same command in my home, I get:
Name: matplotlib
Version: 1.5.1
However, if in my jupyter notebook I do
import matplotlib
print ("matplotlib version:",matplotlib.__version__)
I get:
('matplotlib version:', '1.5.1')
Could you please help me understanding what I do wrong?
I found a solution to my problem. First of all, I read this long post that I suggest to everyone using conda or pip.
Then I understood that the shell environment is determined when the Jupyter notebook is launched, while the Python executable is determined by the kernel, and the two do not necessarily match.
I understood this by putting at the beginning of my jupyter notebook the following:
paths = !type -a python
for path in set(paths):
path = path.split()[-1]
print(path)
!{path} -c "import sys; print(sys.path)"
print()
!type python
import sys
sys.executable
!pip show matplotlib
It showed me that I was still using an older version of the matplotlib library.
Therefore I added
!{sys.executable} -m pip install --upgrade matplotlib
Restarted my jupyter kernel and this solved my problem.
I hope this solution will help someone else in the same situation.

Importing matplotlib returns "ModuleNotFoundError" after installing matplotlib with pip

I'm trying to display data using a pie chart in python, so i tried to install matplotlib using pip. After doing that, I tried importing matplotlib but I get an error: ModuleNotFoundError: No module named 'matplotlib'.
In the Command Prompt I typed 'pip install matplotlib' and everything appeared to be successful. This is the last line:
Successfully installed backports.functools-lru-cache-1.5 cycler-0.10.0 kiwisolver-1.0.1 matplotlib-2.2.4 numpy-1.16.2 pyparsing-2.3.1 python-dateutil-2.8.0 pytz-2018.9 six-1.12.0
I've also tried 'python -m pip install -U matplotlib', but I still get a ModuleNotFoundError.
I've tried running the input statement in both python 3 and 2, neither worked.
I've looked at various different discussions on this, and none of them seemed to help me. How can I fix this?
There are various python installed in your OS. If you install matplotlib for somewhere the python that you are using does not search, you get importing errors. You should figure out which python you want to use and where the packages it searches.
check your python path. the matplotlib should be installed in the same path with your python environment.
I guess you use an IDE to run the python. some IDE have their own python environment.
if not solved. plz introduce some details about your system、how installed etc.

ModuleNotFoundError: No module named 'skmultilearn' Jupyter

Tried cloning git to Jupyter folder on Windows. No progress.
I have installed SKMultilearn in the same folder as Jupyter.
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-22-b987f02e7825> in <module>()
5 return_indicator = 'sparse', allow_unlabeled = False)
6
----> 7 from skmultilearn.adapt import MLkNN
8
9 classifier = MLkNN(k=20)
ModuleNotFoundError: No module named 'skmultilearn'
I faced the same issue and couldn't find a solution. Remember, you must have installed skmultilearn first from cmd, pip install scikit-multilearn. If you have done this and still Jupyter Notebook is saying ModuleNotFoundError: No module named 'skmultilearn', then my solution is for you.
I tried and managed to find an easy solution. I hope that you might have found a solution as this question was asked 3 years ago. I am posting this because many new users will be facing this issue.
Open Jupyter Notebook terminal and go into python.
Type, help('modules'). You will get a list of all packages available to you. 99% chance is that you will not find skmultilearn in that list.
Now, go to command prompt aka cmd.
Type, pip3 install ipykernel --upgrade, then py -3 -m ipykernel install --user, you will get this, Installed kernelspec python3 in C:\Users\MC\AppData\Roaming\jupyter\kernels\python3.
Now type, pip install scikit-multilearn. It will say Requirement already satisfied: scikit-multilearn in c:\python396\lib\site-packages (0.2.0)
Good now type, pip3 install jupyter.
Then, again install pandas and seaborn as your need, pip install pandas and pip install seaborn.
Enjoy, issue resolved. You will not face this error :)
Are you sure that skmultilearn has been correctly installed? It does not depend on the subdirectory where you did the installation.
Open a terminal window in your Jupyter browser, go into python
ipython
and then check out the modules that has been installed:
[1] help('modules')
You will get a list of all packages available to you. skmultilearn should be in the list if it was correctly installed.
pip install scikit-multilearn
pip install scikit-multilearn

ModuleNotFoundError: No module named 'Ipython'

I'm trying to parse the following line of code in an iPython notebook.
from Ipython.display import display, Image
I get the following error,
ModuleNotFoundError: No module named 'Ipython'
When I run pip3 install Ipython
Here's what I get.
Requirement already satisfied: Ipython in ./.envs/dl/lib/python3.6/site-packages
I'm running the code in the same virtual env in which ipython is installed. What am I missing. My python version is 3.6.
Its from IPython.display import display, Image
'P' also caps in IPython
I had this problem too. You have to pay attention to which python version you are using, which pip version, and which environment too. I made a stupid mistake at first and installed TensorFlow on a virtualenv, then tried to call IPython which means my function and installation had different paths.
I recommend using "python -m pip install"

Categories