ModuleNotFoundError in importing matplotlib or seaborn - python

I was trying to perform EDA in Iris Dataset on Jupyter Notebook and I was trying to import these libraries but I keep running into this error. Please help.
I have tried pip install --upgrade pip in my Anaconda command prompt and it responds to me with
Requirement already satisfied: pip in c:\users\aarushi\anaconda3\lib\site-packages (21.2.4)
I also tried pip install Pillow but it responded with
Requirement already satisfied: Pillow in c:\users\aarushi\anaconda3\lib\site-packages (8.3.1)
Also tried pip install --upgrade pip and the response-
Requirement already satisfied: pip in c:\users\aarushi\anaconda3\lib\site-packages (21.2.4)
I also installed IntelliJ, Visual Studio, Weka on my system.
Here is my code-
import os
import matplotlib
import seaborn as sns
---------------------------------------------------------------------------
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-1-efa48e6030a2> in <module>
3 import os
4 import matplotlib
----> 5 import seaborn as sns
~\anaconda3\lib\site-packages\seaborn\__init__.py in <module>
1 # Import seaborn objects
----> 2 from .rcmod import * # noqa: F401,F403
3 from .utils import * # noqa: F401,F403
4 from .palettes import * # noqa: F401,F403
5 from .relational import * # noqa: F401,F403
~\anaconda3\lib\site-packages\seaborn\rcmod.py in <module>
5 import matplotlib as mpl
6 from cycler import cycler
----> 7 from . import palettes
8
9
~\anaconda3\lib\site-packages\seaborn\palettes.py in <module>
7 from .external import husl
8
----> 9 from .utils import desaturate, get_color_cycle
10 from .colors import xkcd_rgb, crayons
11
~\anaconda3\lib\site-packages\seaborn\utils.py in <module>
12 import matplotlib as mpl
13 import matplotlib.colors as mplcol
---> 14 import matplotlib.pyplot as plt
15 from matplotlib.cbook import normalize_kwargs
16
~\anaconda3\lib\site-packages\matplotlib\pyplot.py in <module>
34 from cycler import cycler
35 import matplotlib
---> 36 import matplotlib.colorbar
37 import matplotlib.image
38 from matplotlib import rcsetup, style
~\anaconda3\lib\site-packages\matplotlib\colorbar.py in <module>
42 import matplotlib.collections as collections
43 import matplotlib.colors as colors
---> 44 import matplotlib.contour as contour
45 import matplotlib.cm as cm
46 import matplotlib.gridspec as gridspec
~\anaconda3\lib\site-packages\matplotlib\contour.py in <module>
15 import matplotlib.collections as mcoll
16 import matplotlib.font_manager as font_manager
---> 17 import matplotlib.text as text
18 import matplotlib.cbook as cbook
19 import matplotlib.mathtext as mathtext
~\anaconda3\lib\site-packages\matplotlib\text.py in <module>
14 from .font_manager import FontProperties
15 from .patches import FancyArrowPatch, FancyBboxPatch, Rectangle
---> 16 from .textpath import TextPath # Unused, but imported by others.
17 from .transforms import (
18 Affine2D, Bbox, BboxBase, BboxTransformTo, IdentityTransform, Transform)
~\anaconda3\lib\site-packages\matplotlib\textpath.py in <module>
9 from matplotlib.font_manager import FontProperties, get_font
10 from matplotlib.ft2font import LOAD_NO_HINTING, LOAD_TARGET_LIGHT
---> 11 from matplotlib.mathtext import MathTextParser
12 from matplotlib.path import Path
13 from matplotlib.transforms import Affine2D
~\anaconda3\lib\site-packages\matplotlib\mathtext.py in <module>
25
26 import numpy as np
---> 27 from PIL import Image
28 from pyparsing import (
29 Combine, Empty, FollowedBy, Forward, Group, Literal, oneOf, OneOrMore,
ModuleNotFoundError: No module named 'PIL'

I solved this by uninstalling Anaconda and installing it again. I faced this problem initially because I had downloaded multiple environments. I suggest to properly uninstall Anaconda by searching on the web.

Yes, I also got this error.
Did you install the seaborn after jupyter notebook opened?
You should restart the kernel from the top menu Kernel > Restart kernel.

Related

AttributeError: partially initialized module 'pyproj' has no attribute 'network' (most likely due to a circular import)

I am trying to use geopandas but am getting the following error:
AttributeError: partially initialized module 'pyproj' has no attribute 'network' (most likely due to a circular import)
I am using Jupyter and am installing following dependencies
%conda install numpy
%conda install pandas
%pip install geopandas
%conda install -c plotly plotly
%conda install -c conda-forge pyarrow
%conda install scikit-learn
%pip install ipdb
%pip install --upgrade certifi
%pip install geopandas
I then import the following dependencies
import argparse
import os
import geopandas as gpd
import plotly.express as px
import plotly.graph_objects as go
import numpy as np
import pandas as pd
from ipdb import set_trace
from math import sin, cos, sqrt, atan2, radians
from pyod.models.lscp import LSCP
from pyod.models.lof import LOF
from pyod.utils.utility import standardizer
from pyod.utils.data import generate_data
from pyod.utils.data import evaluate_print
from pyod.utils.example import visualize
from scipy.stats import zscore
from sklearn.neighbors import BallTree, KDTree
from sklearn.cluster import DBSCAN
from sklearn.ensemble import IsolationForest
from sklearn.neighbors import LocalOutlierFactor
When I take out %pip install --upgrade certifi I get the following error:
AttributeError: module 'certifi' has no attribute 'where'
I get the following traceback for the certifi error:
AttributeError Traceback (most recent call last)
<ipython-input-2-c307bc851b37> in <module>
1 import argparse
2 import os
----> 3 import geopandas as gpd
4 import plotly.express as px
5 import plotly.graph_objects as go
~/miniconda3/lib/python3.9/site-packages/geopandas/__init__.py in <module>
----> 1 from geopandas._config import options # noqa
2
3 from geopandas.geoseries import GeoSeries # noqa
4 from geopandas.geodataframe import GeoDataFrame # noqa
5 from geopandas.array import points_from_xy # noqa
~/miniconda3/lib/python3.9/site-packages/geopandas/_config.py in <module>
124 use_pygeos = Option(
125 key="use_pygeos",
--> 126 default_value=_default_use_pygeos(),
127 doc=(
128 "Whether to use PyGEOS to speed up spatial operations. The default is True "
~/miniconda3/lib/python3.9/site-packages/geopandas/_config.py in _default_use_pygeos()
110
111 def _default_use_pygeos():
--> 112 import geopandas._compat as compat
113
114 return compat.USE_PYGEOS
~/miniconda3/lib/python3.9/site-packages/geopandas/_compat.py in <module>
6
7 import pandas as pd
----> 8 import pyproj
9 import shapely
10 import shapely.geos
~/miniconda3/lib/python3.9/site-packages/pyproj/__init__.py in <module>
78 warnings.warn(str(err))
79
---> 80 pyproj.network.set_ca_bundle_path()
~/miniconda3/lib/python3.9/site-packages/pyproj/network.py in set_ca_bundle_path(ca_bundle_path)
49 env_var_name in os.environ for env_var_name in env_var_names
50 ):
---> 51 ca_bundle_path = certifi.where()
52 else:
53 # reset CA Bundle path to use system settings
AttributeError: module 'certifi' has no attribute 'where'
I get the following error when I install certifi:
------ AttributeError Traceback (most recent call last) <ipython-input-5-c307bc851b37> in <module>
1 import argparse
2 import os
----> 3 import geopandas as gpd
4 import plotly.express as px
5 import plotly.graph_objects as go
~/miniconda3/lib/python3.9/site-packages/geopandas/__init__.py in <module>
----> 1 from geopandas._config import options # noqa
2
3 from geopandas.geoseries import GeoSeries # noqa
4 from geopandas.geodataframe import GeoDataFrame # noqa
5 from geopandas.array import points_from_xy # noqa
~/miniconda3/lib/python3.9/site-packages/geopandas/_config.py in <module>
124 use_pygeos = Option(
125 key="use_pygeos",
--> 126 default_value=_default_use_pygeos(),
127 doc=(
128 "Whether to use PyGEOS to speed up spatial operations. The default is True "
~/miniconda3/lib/python3.9/site-packages/geopandas/_config.py in
_default_use_pygeos()
110
111 def _default_use_pygeos():
--> 112 import geopandas._compat as compat
113
114 return compat.USE_PYGEOS
~/miniconda3/lib/python3.9/site-packages/geopandas/_compat.py in <module>
6
7 import pandas as pd
----> 8 import pyproj
9 import shapely
10 import shapely.geos
~/miniconda3/lib/python3.9/site-packages/pyproj/__init__.py in <module>
78 warnings.warn(str(err))
79
---> 80 pyproj.network.set_ca_bundle_path()
AttributeError: partially initialized module 'pyproj' has no attribute 'network' (most likely due to a circular import)

TypeError: an integer is required (got type bytes) when importing sklearn

import pandas
from sklearn import tree
import pydotplus
from sklearn.tree import DecisionTreeClassifier
import matplotlib.pyplot as plt
import matplotlib.image as pltimg
dtree = DecisionTreeClassifier()
dtree = dtree.fit(X, y)
y_pred = dtree.predict(X)
y_pred
The error ::
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-33-745bbfa9769b> in <module>
1 import pandas
----> 2 from sklearn import tree
3 import pydotplus
4 from sklearn.tree import DecisionTreeClassifier
5 import matplotlib.pyplot as plt
~/opt/anaconda3/lib/python3.8/site-packages/sklearn/__init__.py in <module>
62 else:
63 from . import __check_build
---> 64 from .base import clone
65 from .utils._show_versions import show_versions
66
~/opt/anaconda3/lib/python3.8/site-packages/sklearn/base.py in <module>
12 from scipy import sparse
13 from .externals import six
---> 14 from .utils.fixes import signature
15 from .utils import _IS_32BIT
16 from . import __version__
~/opt/anaconda3/lib/python3.8/site-packages/sklearn/utils/__init__.py in <module>
12 from .murmurhash import murmurhash3_32
13 from .class_weight import compute_class_weight, compute_sample_weight
---> 14 from . import _joblib
15 from ..exceptions import DataConversionWarning
16 from .fixes import _Sequence as Sequence
~/opt/anaconda3/lib/python3.8/site-packages/sklearn/utils/_joblib.py in <module>
20 from joblib import parallel_backend, register_parallel_backend
21 else:
---> 22 from ..externals import joblib
23 from ..externals.joblib import logger
24 from ..externals.joblib import dump, load
~/opt/anaconda3/lib/python3.8/site-packages/sklearn/externals/joblib/__init__.py in <module>
117 from .numpy_pickle import load
118 from .compressor import register_compressor
--> 119 from .parallel import Parallel
120 from .parallel import delayed
121 from .parallel import cpu_count
~/opt/anaconda3/lib/python3.8/site-packages/sklearn/externals/joblib/parallel.py in <module>
26 from .my_exceptions import TransportableException
27 from .disk import memstr_to_bytes
---> 28 from ._parallel_backends import (FallbackToBackend, MultiprocessingBackend,
29 ThreadingBackend, SequentialBackend,
30 LokyBackend)
~/opt/anaconda3/lib/python3.8/site-packages/sklearn/externals/joblib/_parallel_backends.py in <module>
20 from .pool import MemmappingPool
21 from multiprocessing.pool import ThreadPool
---> 22 from .executor import get_memmapping_executor
23
24 # Compat between concurrent.futures and multiprocessing TimeoutError
~/opt/anaconda3/lib/python3.8/site-packages/sklearn/externals/joblib/executor.py in <module>
12 from .disk import delete_folder
13 from ._memmapping_reducer import get_memmapping_reducers
---> 14 from .externals.loky.reusable_executor import get_reusable_executor
15
16
~/opt/anaconda3/lib/python3.8/site-packages/sklearn/externals/joblib/externals/loky/__init__.py in <module>
10
11 from .backend.context import cpu_count
---> 12 from .backend.reduction import set_loky_pickler
13 from .reusable_executor import get_reusable_executor
14 from .cloudpickle_wrapper import wrap_non_picklable_objects
~/opt/anaconda3/lib/python3.8/site-packages/sklearn/externals/joblib/externals/loky/backend/reduction.py in <module>
123 # global variable to change the pickler behavior
124 try:
--> 125 from sklearn.externals.joblib.externals import cloudpickle # noqa: F401
126 DEFAULT_ENV = "cloudpickle"
127 except ImportError:
~/opt/anaconda3/lib/python3.8/site-packages/sklearn/externals/joblib/externals/cloudpickle/__init__.py in <module>
1 from __future__ import absolute_import
2
----> 3 from .cloudpickle import *
4
5 __version__ = '0.6.1'
~/opt/anaconda3/lib/python3.8/site-packages/sklearn/externals/joblib/externals/cloudpickle/cloudpickle.py in <module>
165
166
--> 167 _cell_set_template_code = _make_cell_set_template_code()
168
169
~/opt/anaconda3/lib/python3.8/site-packages/sklearn/externals/joblib/externals/cloudpickle/cloudpickle.py in _make_cell_set_template_code()
146 )
147 else:
--> 148 return types.CodeType(
149 co.co_argcount,
150 co.co_kwonlyargcount,
TypeError: an integer is required (got type bytes)
So I keep getting this type error and have no clue what to do pretty sure that sklearn is installed. Both X and y are previously defined after creating the training set to be used. I've tried reinstalling scikit-learn, downgrading python and installing through the terminal but none of that helped. Just cant figure out what the issue is here and will really appreciate some help on this
I was able to reproduce the problem in a conda environment by running conda install python==3.8 scikit-learn but then manually downgrading joblib by running pip install joblib<0.14 since joblib<0.14 is incompatible with Python 3.8.
I'm not exactly sure how you got into the this situation, but it should fix it to first uninstall any joblib package that might have been mis-installed:
$ pip uninstall joblib
Then force reinstall/upgrade it with conda:
$ conda update --force-reinstall joblib
Confirm the correct version was installed:
$ python -c 'import joblib; print(joblib.__version__)'
0.17.0
If all else fails, try creating a fresh Anaconda install. Make sure to install packages with conda install and not pip.
In my case, joblib in scikit needs to be reinstalled.
pip uninstall joblib
This happened to me because I pip installed sklearn instead of scikit-learn.
In my case, I did
pip uninstall sklearn --yes
followed by
pip install sklearn

ImportError: cannot import name 'is_string_like' from 'matplotlib.cbook

<ipython-input-8-8ef302d419f0> in <module>
1 from netCDF4 import Dataset
2 import numpy
----> 3 from SkewTplus.sounding import sounding
~\Anaconda3\lib\site-packages\SkewTplus\sounding.py in <module>
19
20 from SkewTplus.`enter code here`errorHandling import fatalError
---> 21 from SkewTplus.skewT import figure
22
23
~\Anaconda3\lib\site-packages\SkewTplus\skewT.py in <module>
23 from matplotlib.axes import Axes
24 from matplotlib.backends import pylab_setup
---> 25 from matplotlib.cbook import is_string_like
26 from matplotlib.figure import Figure
27 from matplotlib.projections import register_projection
ImportError: cannot import name 'is_string_like' from 'matplotlib.cbook'
(C:\Users\User\Anaconda3\lib\site-packages\matplotlib\cbook\__init__.py)
I got an error when I import SkewTplus. I don't know what to do. I have tried reinstalling matplotlib but the problem doesn't go away.
I met the same error and solved it by reinstalling it again.Make sure using conda and pip to reinstall the package and then reinstall it again.
pip uninstall matplotlib
conda uninstall matplotlib

can't import matplotlib.pyplot; DLL load failed

Having a problem importing matplotlib.pyplot into Jupyter notebook.
I uninstalled matplotlib (and seaborn) and reinstalled them again. But that didn't solve the problem.
The exact code however works in my second computer without problems.
I researched stackoverflow, and tried one thing that was suggested (please see bottom of post) - that still gave the exact same error.
Here's some relevant info
Platform : win-64, Windows 7 Enterprise
Conda version : 4.4.7
Conda-build version : 3.0.27
Python version : 3.6.4.final.0
JupyterLab : v0.27.0
Matplotlib version 2.1.2
Here's what I am trying to run:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline
I get the error shown below:
Pyplot_error
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-1-d2e7a3f2c9b4> in <module>()
1 import pandas as pd
2 import numpy as np
----> 3 import matplotlib.pyplot as plt
---------------------------------------------------------------------------
4 import seaborn as sns
5 get_ipython().run_line_magic('matplotlib', 'inline')
~\AppData\Local\Continuum\anaconda3\lib\site-packages\matplotlib\pyplot.py in <module>()
30 from cycler import cycler
31 import matplotlib
---> 32 import matplotlib.colorbar
---------------------------------------------------------------------------
33 from matplotlib import style
34 from matplotlib import _pylab_helpers, interactive
~\AppData\Local\Continuum\anaconda3\lib\site-packages\matplotlib\colorbar.py in <module>()
34 import matplotlib.collections as collections
35 import matplotlib.colors as colors
---> 36 import matplotlib.contour as contour
---------------------------------------------------------------------------
37 import matplotlib.cm as cm
38 import matplotlib.gridspec as gridspec
~\AppData\Local\Continuum\anaconda3\lib\site-packages\matplotlib\contour.py in <module>()
19 import matplotlib.colors as colors
20 import matplotlib.collections as mcoll
---> 21 import matplotlib.font_manager as font_manager
---------------------------------------------------------------------------
22 import matplotlib.text as text
23 import matplotlib.cbook as cbook
~\AppData\Local\Continuum\anaconda3\lib\site-packages\matplotlib\font_manager.py in <module>()
56
57 import matplotlib
---> 58 from matplotlib import afm, cbook, ft2font, rcParams, get_cachedir
---------------------------------------------------------------------------
59 from matplotlib.compat import subprocess
60 from matplotlib.fontconfig_pattern import (
ImportError: DLL load failed: The specified procedure could not be found.
---------------------------------------------------------------------------
I get the same error when I try
import matplotlib as mpl
mpl.use('tkagg')
import matplotlib.pyplot as plt

Matplotlib on Windows - dependency unresolved

I have had problems with using matplotlib after a Windows update. I'm running Windows 7 Service Pack 1 32 bit and I installed Python and matplotlib as part of Python(x,y)-2.7.6.1. The problem appears related to FreeType, as the import fails on ft2font as shown in the stack trace below:
In [1]: import matplotlib
In [2]: matplotlib.use('agg')
In [3]: import matplotlib.pyplot as plt
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-3-eff513f636fd> in <module>()
----> 1 import matplotlib.pyplot as plt
C:\Python27\lib\site-packages\matplotlib\pyplot.py in <module>()
22
23 import matplotlib
---> 24 import matplotlib.colorbar
25 from matplotlib import _pylab_helpers, interactive
26 from matplotlib.cbook import dedent, silent_list, is_string_like, is_numlike
C:\Python27\lib\site-packages\matplotlib\colorbar.py in <module>()
27 import matplotlib.artist as martist
28 import matplotlib.cbook as cbook
---> 29 import matplotlib.collections as collections
30 import matplotlib.colors as colors
31 import matplotlib.contour as contour
C:\Python27\lib\site-packages\matplotlib\collections.py in <module>()
21 import matplotlib.artist as artist
22 from matplotlib.artist import allow_rasterization
---> 23 import matplotlib.backend_bases as backend_bases
24 import matplotlib.path as mpath
25 from matplotlib import _path
C:\Python27\lib\site-packages\matplotlib\backend_bases.py in <module>()
48
49 import matplotlib.tight_bbox as tight_bbox
---> 50 import matplotlib.textpath as textpath
51 from matplotlib.path import Path
52 from matplotlib.cbook import mplDeprecation
C:\Python27\lib\site-packages\matplotlib\textpath.py in <module>()
9 from matplotlib.path import Path
10 from matplotlib import rcParams
---> 11 import matplotlib.font_manager as font_manager
12 from matplotlib.ft2font import FT2Font, KERNING_DEFAULT, LOAD_NO_HINTING
13 from matplotlib.ft2font import LOAD_TARGET_LIGHT
C:\Python27\lib\site-packages\matplotlib\font_manager.py in <module>()
51 import matplotlib
52 from matplotlib import afm
---> 53 from matplotlib import ft2font
54 from matplotlib import rcParams, get_cachedir
55 from matplotlib.cbook import is_string_like
ImportError: DLL load failed: The specified procedure could not be found.
I have tried reinstalling Python(x,y), but this did not resolve the problem.
From other answers on Stackoverflow I have learned that common failures here include missing msvcr90.dll and msvcp90.dll files. I downloaded Dependency Walker and opened c:\Python27\Lib\site-packages\matplotlib\FT2FONT.PYD. This showed issues with these files and with libbz2.dll. I downloaded and copied these files to c:\windows\system32.
I have also tried checking my PATH and PYTHONPATH environment variables, but they appear to reference my Python install correctly:
PATH: C:\Python27\Lib\site-packages\PyQt4;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files\Novell\GroupWise;C:\Program Files\MiKTeX 2.9\miktex\bin\;C:\Program Files\Microsoft SQL Server\80\Tools\Binn\;C:\Program Files\Microsoft SQL Server\110\Tools\Binn\;C:\Program Files\Common Files\AspenTech Shared\;C:\Python27;C:\Python27\DLLs;C:\Python27\Scripts;C:\Python27\gnuplot\binary;C:\Program Files\pythonxy\SciTE-3.3.2-3;C:\Program Files\pythonxy\console;C:\MinGW32-xy\bin;C:\Python27\Lib\site-packages\vtk
PYTHONPATH: c:\Python27\DLLs
The problem manifests even when only using the Agg backend as shown in the session above, so I don't think it has anything to do with Qt or tk.
It appears that the problem was caused by an application installing a different/incompatible version of BZ2 in C:\Windows\System32\libbz2.dll. This was being used instead of the dll of the same name in the Python27 directory installed by Python(x,y). This is how the situation showed up in Dependency Walker:
You can see that there is something wrong with libbz2.dll even though Dependency Walker doesn't list it as a dependency error as such. Renaming or deleting the version in C:\Windows\System32\ caused the dependency to be resolved by the version in c:\Python27\DLLs.

Categories

Resources