Can't import matplotlib under Ipython notebook anaconda - python

I have downloaded and installed anaconda. But when I run jupyter notebook and import matplotlib , it doesn't work. Below are some commands
which -a python
/home/user/anaconda3/bin/python
/usr/bin/python
which -a jupyter
/home/user/anaconda3/bin/jupyter
I run the notebook as follows
jupyter notebook
I get the below error
ImportError Traceback (most recent call last)
<ipython-input-1-fe07c3ebfee7> in <module>()
2 # before proceeding further.
3 from __future__ import print_function
----> 4 import matplotlib.pyplot as plt
5 import numpy as np
6 import os
ImportError: No module named 'matplotlib'
Note when I run python through terminal I can import matplotlib , the problem only persist in the browser of ipython notebook after running jupyter.

It worked after changing /usr/local/share/jupyter/kernels/python3/kernel.json .
The path there was /usr/bin/python , changed it to anaconda one.

Related

"import tensorflow" results in error: No module named 'tensorflow.python.eager.polymorphic_function' (Python in Jupyter Lab)

Python 3.9.12.
Windows 10.
jupyterlab 3.3.2.
Import tensorflow
When I try to import Tensorflow, I get the following 'tensorflow.python.eager.polymorphic_function' error.
---------------------------------------------------------------------------
ModuleNotFoundError Traceback (most recent call last)
Cell In [44], line 1
----> 1 import tensorflow
File ~\OD13\TFODCourse\tfod13\lib\site-packages\tensorflow\__init__.py:45
42 from tensorflow.python import tf2 as _tf2
43 _tf2.enable()
---> 45 from ._api.v2 import __internal__
46 from ._api.v2 import __operators__
47 from ._api.v2 import audio
File ~\OD13\TFODCourse\tfod13\lib\site-packages\tensorflow\_api\v2\__internal__\__init__.py:14
12 from . import eager_context
13 from . import feature_column
---> 14 from . import function
15 from . import graph_util
16 from . import mixed_precision
File ~\OD13\TFODCourse\tfod13\lib\site-packages\tensorflow\_api\v2\__internal__\function\__init__.py:8
3 """Public API for tf.__internal__.function namespace.
4 """
6 import sys as _sys
----> 8 from tensorflow.python.eager.polymorphic_function.polymorphic_function import Function
9 from tensorflow.python.eager.polymorphic_function.quarantine import defun_with_attributes
ModuleNotFoundError: No module named 'tensorflow.python.eager.polymorphic_function'
My workflow is based on this tutorial: https://www.youtube.com/watch?v=yqkISICHH-U
I found the following answer, but I'm not understanding how to implement the TFLite Authoring Tool to solve this problem:
https://stackoverflow.com/questions/74177865/tensorflow-python-eager-polymorphic-function-no-module-error-on-imports
To answer my own question:
I created a conda environment and installed an older version of Python (3.7) in it and that seems to have fixed the problem.
I found these links to be helpful:
How to downgrade the Python Version from 3.8 to 3.7 on windows?
conda install downgrade python version
Jupyter Notebook - Cannot Connect to Kernel
How to find the version of jupyter notebook from within the notebook
pip command to downgrade jupyter notebook
How to check python anaconda version installed on Windows 10 PC?
https://towardsdatascience.com/get-your-conda-environment-to-show-in-jupyter-notebooks-the-easy-way-17010b76e874

Facing ImportError: cannot import name 'ModelParameters' (Python)

I am using the module ydata_synthetic (github link) for generating synthetic timeseries datasets. After installing the package, I ran the following commands:
from os import path
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from ydata_synthetic.synthesizers import ModelParameters
from ydata_synthetic.preprocessing.timeseries import processed_stock
from ydata_synthetic.synthesizers.timeseries import TimeGAN
I am receiving the following error:
ImportError Traceback (most recent call last)
<ipython-input-11-9f2f25e511c0> in <module>
4 import matplotlib.pyplot as plt
5
----> 6 from ydata_synthetic.synthesizers import ModelParameters
7 from ydata_synthetic.preprocessing.timeseries import processed_stock
8 from ydata_synthetic.synthesizers.timeseries import TimeGAN
ImportError: cannot import name 'ModelParameters'
How can I solve this error?
Please make sure, that the virtual environment, where you installed the package is the same one where you try to execute your code. In case of venv or anaconda, you need to explicitly activate it in the console/terminal, then use pip to install your package and then also use that activated environment to execute your python script. If you are using an IDE, you need to set your IDE python interpreter to that specific new environment you created and installed the packe in.
Unexpected solution I found:
Use Python 3.7 (not 3.6, or 3.8+).
It seems like much of the code requires features that were new to 3.7 (eg namedtuple with default values), but the codebase has not yet been adjusted to run with python 3.8 or later.

How to install sklearn package in Jupyter Notebook in pycharm

I was receiving the following error. I have installed the package in the pycharm. Now the module is available in main.py file but the module is not available in jupyter notebook that I am using in pycharm.
When I run the cell I still get the following error in jupyter notebook but not in main.py file.
Please help me out!
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-1-7a030bc34e9f> in <module>
----> 1 from sklearn.cluster import KMeans
2 import numpy as np
3
4 from sklearn.cluster import KMeans
5 import numpy as np
ModuleNotFoundError: No module named 'sklearn'

Numpy & Matplotlib suddenly cannot be imported in Atom although it works fine in the Terminal

I'm regularly importing Matplotlib Numpy PySide2 in Atom. They unexpectedly cannot be import for an unknown reason and then appear to be again re-importable, again unexplained.
When trying to import matplotlib with the following command
import matplotlib.pyplot as plt
This scripts runs without problems when I run in a Jupyter notebook or through the Terminal. But When trying to run on Hydrogen in Atom, I get the following error message:
File "<ipython-input-3-3dc8365ef973>", line 6
ModuleNotFoundError: No module named 'matplotlib.backends'; 'matplotlib' is not a package
^
SyntaxError: invalid syntax
I get the same error with Pyside2 when trying to run the line
from PySide2.QtWidgets import QApplication, QLabel
I get the error message
ModuleNotFoundError: No module named 'PySide2.QtWidgets'; 'PySide2' is not a package
Here is the path of python (after having typed which python in the Terminal): /anaconda3/bin/python
I checked that the matplotlib is installed by running the command conda list in the Terminal and got (among all the other packages) those lines:
matplotlib 2.1.2 py36h6d6146d_0
matplotlib 2.2.2 <pip>
PySide2 5.9.0a1.dev1525348214
I tried to run the following command in the Terminal but didn't solve the problem
sudo apt-get install python3-matplotlib
sudo pip3 install matplotlib
I had a similar issue with numpy when trying to import import numpy. Although import numpy was not a problem an hour ago, I get now the following error message (it seems it wants to connect to a previously used script in which numpy was used, to open numpy itself):
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-5-592c151f282b> in <module>()
1 # from numpy import genfromtxt as gft
----> 2 import numpy
~/Downloads/numpy.py in <module>()
----> 1 from numpy import genfromtxt
2 my_data = genfromtxt('/Users/mymac/Documents/PyQt/image_viewer/csv_file_generator/eggs.csv',
3 delimiter=',',
4 dtype=None,
5 encoding=None)
ImportError: cannot import name 'genfromtxt'
I had the same issue when tryin to import opencv2: I got the following error message:
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
ImportError: numpy.core.multiarray failed to import---------------------------
------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-1-8f6675581547> in <module>()
5 from PySide2.QtCore import *
6 from PySide2.QtGui import *
----> 7 import cv2
8 import numpy
9 import csv
ImportError: numpy.core.multiarray failed to import
I visited the following site which didn't help me (or I didn't understand how to implement exactly the steps to fix the problem)
ImportError: No module named matplotlib.pyplot
Importing matplotlib.pyplot in atom editor
matplotlib Error: No module named matplotlib even though it is installed
I'm running on/with
python 3.6.5
macOS 10.13.4
Atom 1.27.0 x64
Hydrogen 2.4.1

import error OpenCV on Jupyter Notebook (but it works in Ipython on Terminal..)

I'm having an interesting error where I could import cv2 in Ipython on Terminal but cannnot import the library on Jupyter notebook. I checked the kernel but I'm using the same kernel (Anaconda python2.7)
Would someone has any idea how to fix this?
Error:
ImportErrorTraceback (most recent call last)
<ipython-input-2-52da0154cfe4> in <module>()
----> 1 import cv2
2 import numpy as np
ImportError: No module named cv2
$import os
$os.sys.path
['',
'/Users/kn/anaconda2/envs/python2/lib/python27.zip',
'/Users/kn/anaconda2/envs/python2/lib/python2.7',
'/Users/kn/anaconda2/envs/python2/lib/python2.7/plat-darwin',
'/Users/kn/anaconda2/envs/python2/lib/python2.7/plat-mac',
'/Users/kn/anaconda2/envs/python2/lib/python2.7/plat-mac/lib-scriptpackages',
'/Users/kn/anaconda2/envs/python2/lib/python2.7/lib-tk',
'/Users/kn/anaconda2/envs/python2/lib/python2.7/lib-old',
'/Users/kn/anaconda2/envs/python2/lib/python2.7/lib-dynload',
'/Users/kn/anaconda2/envs/python2/lib/python2.7/site-packages',
'/Users/kn/anaconda2/envs/python2/lib/python2.7/site-packages/Sphinx-1.4.6-py2.7.egg',
'/Users/kn/anaconda2/envs/python2/lib/python2.7/site-packages/aeosa',
'/Users/kn/anaconda2/envs/python2/lib/python2.7/site-packages/setuptools-27.2.0-py2.7.egg',
'/Users/kn/anaconda2/envs/python2/lib/python2.7/site-packages/IPython/extensions',
'/Users/kn/.ipython']
$which python
/Users/kn/anaconda2/envs/python2/bin/python
Create a one-line opencv.pth file under /Users/kn/anaconda2/lib/python2.7/site-packages.
The content of that file is like:
/usr/local/opt/opencv3/lib/python2.7/site-packages
This will append the opencv3 path under all conda environments permanently. I think it's better than just make cv2 available for python2 environment.

Categories

Resources