I'm Unable to load matplotlib in Jupyter Notebook but woking fine in python command line shell,
Is there anything I need to configure to make it working?
Following is the error I'm getting in Jupyter Notebook
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-1-99ba79ecbbfb> in <module>()
----> 1 from matplotlib import pyplot as plt
ImportError: No module named matplotlib
And in command line I can access it like the following:
Python 3.7.3 (default, Mar 27 2019, 23:47:09)
[Clang 10.0.0 (clang-1000.10.44.4)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from matplotlib import pyplot as plt
>>>
Seems like it's working now I ran the following command as specified here
python3 -m pip install ipykernel
python3 -m ipykernel install --user
Thanks.
One way is to check if you installed matplotlib using pip3 (if you used pip3 to install jupyter notebook, which looks like is your case).
Another way is to add the path of site-wide packages (where of course matplotlib is installed). In your Jupyter notebook console:
import sys
PATH = '/usr/lib64/python3.7/site-packages/'
sys.path.append(PATH)
Notebook is launched from another virtual python environment.
You can check paths to python interpreters, that run your notebook and interactive shell:
import sys
print(sys.executable)
I'm sure, they will be different.
Related
System:
System Version: macOS 10.15.7 (19H1030)
Kernel Version: Darwin 19.6.0
conda version: 4.10.1
VS Code version: 1.56.2
I open the VS Code terminal, create a conda environment and activate it:
conda create -n foo python=3.6
conda activate foo
I install pandas and check that the installation was successful:
conda install pandas
conda list pandas
# packages in environment at /opt/anaconda3/envs/foo:
#
# Name Version Build Channel
pandas 1.1.5 py36hb2f4e1b_0
Simple script test.py to test that everything is fine:
import pandas
print(pandas.__version__)
So far, so good. With the foo environment still active, I run
(foo) ...:~ <username>$ python test.py
Traceback (most recent call last):
File "test.py", line 1, in <module>
import pandas
ImportError: No module named pandas
...Not so good anymore. The issue is that,even though the conda environment is active and the corresponding Python interpreter is selected in VS Code,
the terminal insists on using the system Python intepreter, for which pandas is not installed:
(foo) ...:~ <username>$ python
WARNING: Python 2.7 is not recommended.
This version is included in macOS for compatibility with legacy software.
Future versions of macOS will not include Python 2.7.
Instead, it is recommended that you transition to using 'python3' from within Terminal.
Python 2.7.16 (default, Jun 5 2020, 22:59:21)
[GCC 4.2.1 Compatible Apple LLVM 11.0.3 (clang-1103.0.29.20) (-macos10.15-objc- on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import pandas
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named pandas
>>>
Neither conda init bash, conda init or source ~/.bash_profile solved the issue. How can I get VS Code to actually use my conda env when running stuff in the terminal? I'm pulling my hair out on this.
PS note that if I use a better Mac terminal emulator (e.g., iTerm2), instead of the VS Code terminal, everything works fine.
(foo) ...:~ <username>$ conda activate foo
(foo) ...:~ <username>$ python test.py
1.1.5
This question already has answers here:
Conda environments not showing up in Jupyter Notebook
(24 answers)
Closed 2 years ago.
I installed openCV in my environment using '''conda-forge'''.
It works in my terminal
❯ python
Python 3.8.2 (default, Mar 26 2020, 10:43:30)
[Clang 4.0.1 (tags/RELEASE_401/final)] :: Anaconda, Inc. on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
But it doesn't work on my Jupyter Notebook
import cv2
---------------------------------------------------------------------------
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-1-c8ec22b3e787> in <module>
----> 1 import cv2
ModuleNotFoundError: No module named 'cv2'
I am using Python 3.8.2. It shows on my conda list and the environment package list of anaconda. Please help.
Thank you #Trenton.
python -m ipykernel install --user --name Vision --display-name "Python (Vision)"
Then selecting the Python (vision) from Jupyter Notebook solved this!
I created a fresh conda environment for using scikit-learn and used
conda install <package> to install scikit-learn, jupyter, pandas, etc. for compatible dependencies..
I checked if sklearn was working after loading the environment:
$python
Python 3.7.4 (default, Aug 13 2019, 15:17:50)
[Clang 4.0.1 (tags/RELEASE_401/final)] :: Anaconda, Inc. on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import sklearn
>>>
Since import command didn't throw errors, sklearn is ready for use. However, I am getting a ModuleNotFoundError while trying to import it in a jupyter notebook, that I am running from the same environment.
import sklearn
---------------------------------------------------------------------------
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-1-b7c74cbf5af0> in <module>()
----> 1 import sklearn
ModuleNotFoundError: No module named 'sklearn'
I was able to import numpy and pandas in the same notebook without any errors.
Please help me understand the problem and how to troubleshoot it.
Best practice: Install everything via conda or pip3, as mentioned in this answer.
If that didn't work, check the system paths in jupyter notebook:
import sys
sys.path
and the system executable:
sys.executable
These must correspond to the python in your current loaded environment.
For me, the issue was with the jupyter Notebook's kernel. See the kernel specifications in kernel.json file in the path. You can find the directory for this file from jupyter kernelspec list. I manually changed the python path to the python in my environment (this is a bad idea, but it worked).
Make sure that your jupyter notebook is finding the same version of python as your terminal, otherwise installing modules with conda install in your terminal won't show up in your notebook. Do
import sys
print(sys.version)
in your notebook and in your terminal. If they do not match up, then add your terminal's python version to your notebook:
conda install nb_conda_kernels
conda install ipykernel
and then in the notebook switch to the kernel you just installed (kernel -> change kernel)
Check your path for Python and Jupyter:
import sys
sys.path
You may find different results from Python and Jupyter. This can be fixed temporarily by appending this line of code if you are using macos:
sys.path.append('/Users/**your_user_name**/anaconda3/lib/python3.7/site-packages')
If you want to solve this permanently, you can create an iPython profile and fix it there.
First activate the finds environment, second launch jupyter notebook, third import sklearn. Inside jupyter notebook.
I have also found the same issue
ModuleNotFoundError: No module named 'sklearn'
but after searching I found its best solution.
You should also try the following steps:
Step 1:
open "cmd"
Step 2:
write "pip install notebook"
Step 3:
After installation of notebook, write "jupyter notebook " in cmd.
Tell me if you got your solution
thank you!
I'm about to give up on Anaconda. I never had trouble managing my packages with pip and I just thought I'd try it since now there's one package I can't get with pip and I'd heard so many good things about it.
I can't import a package I just installed with Anaconda, similar to this but on MacOS instead of Windows.
I really don't want multiple environments unless I have to have them. I want to be able to run most/all of my packages from the same scripts. I have a virtual environment named py37 where I've been putting most things. Among other packages:
(py37) jennifers-mbp:~ jenniferlongdiaz$ conda list
#packages in environment at /anaconda3/envs/py37:
#
# Name Version Build Channel
matplotlib-venn 0.11.5 py_1 conda-forge
numpy 1.15.3 py37h6a91979_0
python 3.7.1 haf84260_3
Python goes to the right installation:
(py37) jennifers-mbp:~ jenniferlongdiaz$ which python
/anaconda3/envs/py37/bin/python
(py37) jennifers-mbp:~ jenniferlongdiaz$ python
Python 3.7.1 (default, Oct 23 2018, 14:07:42)
[Clang 4.0.1 (tags/RELEASE_401/final)] :: Anaconda, Inc. on darwin
Type "help", "copyright", "credits" or "license" for more information.
I can import numpy but not matplotlib-venn:
>>> import numpy as np
>>> import matplotlib_venn as venn
...bunch of stuff and then:
ImportError: cannot import name 'get_backend' from 'matplotlib' (/anaconda3/envs/py37/lib/python3.7/site-packages/matplotlib/__init__.py)
Similiarly:
>>> from matplotlib import get_backend
...bunch of stuff and then:
ImportError: cannot import name 'get_backend' from 'matplotlib' (/anaconda3/envs/py37/lib/python3.7/site-packages/matplotlib/__init__.py)
From within the IDE spyder ((py37) jennifers-mbp:~ jenniferlongdiaz$ spyder), I get:
In [1]: import matplotlib_venn as venn
Traceback (most recent call last):
File "<ipython-input-9-aafbc15b97e7>", line 1, in <module>
import matplotlib_venn as venn
ModuleNotFoundError: No module named 'matplotlib_venn'
Please help!
According to matplotlib-venn's PyPi page (https://pypi.org/project/matplotlib-venn/), the import should look like this:
import matplotlib_venn as venn
Note that the module is named with an underscore whereas the package is named with a dash; this is a tricky inconsistency
Update for updated question: the issues with spyder were due to spyder not being installed as part of anaconda, resulting in the system's spyder not being aware of the anaconda environment's packages. Being unable to import get_backend from matplotlib would suggest either a missing or borked matplotlib installation. Both just require installing (or reinstalling) the packages using conda
I am attempting to teach myself programming and keep running into problems downloading modules I need for basic tutorials.
My latest attempt has been to get the matplotlib module into my Python 3 environment. I have tried so many different install packages and so many advice I found on the internet that I cannot remember how I originally got the module. But it seemed that everything went well with the installation process.
I am using a Raspberry Pi2 throughout all of this with the Raspbian OS installed.
Python 3.2.3 (default, Mar 1 2013, 11:53:50)
[GCC 4.6.3] on linux2
Type "copyright", "credits" or "license()" for more information.
>>> import matplotlib
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
import matplotlib
ImportError: No module named matplotlib
It may be possible that you actually installed the library for python 2. I would use pip to install the modules that you need. If you don't already have it installed, run this first command in the shell:
sudo apt-get install python3-pip
This program can be called with "pip3" or maybe "pip-3.2" in the shell.
To install mathplotlib with it, try running:
pip3 install matplotlib
or
pip-3.2 install matplotlib
That should install the Python 3 module for you.