ModuleNotFoundError: No module named 'matplotlib' even though the package is installed

Viewed 301

I am building a GIS application, using python 3.7, on MacOS Mojave 10.14.2 . I have set an environment, called 'MG' with miniconda, and installed the following packages

# packages in environment at /Users/mousetsof/miniconda3/envs/MG:
Name                    Version                   Build  Channel
anaconda                  custom                   py37_0  
attrs                     18.2.0                     py_0    conda-forge
blas                      1.0                         mkl  
boost-cpp                 1.68.0               h3a22d5f_0    conda-forge
bzip2                     1.0.6                         1    conda-forge
ca-certificates           2018.11.29           ha4d7672_0    conda-forge
cairo                     1.14.12              h276e583_5    conda-forge
certifi                   2018.11.29            py37_1000    conda-forge
click                     7.0                        py_0    conda-forge
click-plugins             1.0.4                      py_0    conda-forge
cligj                     0.5.0                      py_0    conda-forge
curl                      7.63.0               h74213dd_0    conda-forge
cycler                    0.10.0                     py_1    conda-forge
expat                     2.2.6                h0a44026_0    anaconda
fiona                     1.8.4            py37hfc77a4a_1    conda-forge
fontconfig                2.13.1               hce039c3_0    conda-forge
freetype                  2.9.1                h6debe1e_4    conda-forge
freexl                    1.0.5                h470a237_2    conda-forge
gdal                      2.3.2            py37hfc77a4a_2    conda-forge
geos                      3.7.0                hfc679d8_0    conda-forge
geotiff                   1.4.3                h9c44c65_0    conda-forge
gettext                   0.19.8.1             h1f1d5ed_1    conda-forge
giflib                    5.1.4                h470a237_1    conda-forge
glib                      2.56.2               h464dc38_1    conda-forge
hdf4                      4.2.13               h951d187_2    conda-forge
hdf5                      1.10.4          nompi_h5598ddc_1105    conda-forge
icu                       58.2                 hfc679d8_0    conda-forge
intel-openmp              2019.1                      144  
jpeg                      9c                   h470a237_1    conda-forge
json-c                    0.13.1               h470a237_1    conda-forge
kealib                    1.4.10               heffcb4b_2    conda-forge
kiwisolver                1.0.1            py37h2d50403_2    conda-forge
krb5                      1.16.3               hbb41f41_0    conda-forge
libcurl                   7.63.0               hbdb9355_0    conda-forge
libcxx                    7.0.0                h2d50403_2    conda-forge
libdap4                   3.19.1               h18059cb_1    conda-forge
libedit                   3.1.20170329         haf1bffa_1    conda-forge
libffi                    3.2.1                hfc679d8_5    conda-forge
libgdal                   2.3.2                h9c3506f_2    conda-forge
libgfortran               3.0.1                h93005f0_2  
libiconv                  1.15                 h470a237_4    conda-forge
libkml                    1.3.0                he469717_9    conda-forge
libnetcdf                 4.6.2                h2c3f975_1    conda-forge
libpng                    1.6.36               ha92aebf_0    conda-forge
libpq                     10.6                 hf16a0db_0    conda-forge
libspatialindex           1.8.5                hfc679d8_3    conda-forge
libspatialite             4.3.0a              h201a3a7_25    conda-forge
libssh2                   1.8.0                h5b517e9_3    conda-forge
libtiff                   4.0.10               he6b73bb_1    conda-forge
libxml2                   2.9.8                h422b904_5    conda-forge
llvm-meta                 7.0.0                         0    conda-forge
matplotlib                3.0.2                    py37_1    conda-forge
matplotlib-base           3.0.2            py37hb2d221d_1    conda-forge
mkl                       2019.1                      144  
mkl_fft                   1.0.6            py37h27c97d8_0  
mkl_random                1.0.2            py37h27c97d8_0  
munch                     2.3.2                      py_0    conda-forge
ncurses                   6.1                  hfc679d8_2    conda-forge
numpy                     1.15.4           py37hacdab7b_0  
numpy-base                1.15.4           py37h6575580_0  
openjpeg                  2.3.0                h316dc23_3    conda-forge
openssl                   1.0.2p               h470a237_2    conda-forge
pandas                    0.23.4           py37h6440ff4_0    anaconda
pcre                      8.42                 h378b8a2_0    anaconda
pip                       18.1                  py37_1000    conda-forge
pixman                    0.34.0               h470a237_3    conda-forge
poppler                   0.67.0               hdf8a1b3_2    conda-forge
poppler-data              0.4.9                         1    conda-forge
postgresql                10.6                 ha408888_0    conda-forge
proj4                     5.2.0                h470a237_1    conda-forge
psycopg2                  2.7.6.1          py37hdffb7b8_0    conda-forge
pyparsing                 2.3.0                      py_0    conda-forge
pyproj                    1.9.6            py37h429999c_0    conda-forge
pysal                     1.14.4.post2          py37_1001    conda-forge
pyshp                     2.0.1                      py_0    conda-forge
python                    3.7.1                h46c1a51_0    conda-forge
python-dateutil           2.7.5                    py37_0    anaconda
pytz                      2018.7                   py37_0    anaconda
readline                  7.0                  haf1bffa_1    conda-forge
rtree                     0.8.3                 py37_1000    conda-forge
scipy                     1.1.0            py37h1410ff5_2  
setuptools                40.6.3                   py37_0    conda-forge
shapely                   1.6.4            py37hcff0d60_1    conda-forge
six                       1.12.0                py37_1000    conda-forge
sqlalchemy                1.2.15           py37h470a237_0    conda-forge
sqlite                    3.26.0               hb1c47c0_0    conda-forge
time                      1.7                  h470a237_1    conda-forge
tk                        8.6.9                ha92aebf_0    conda-forge
tornado                   5.1.1            py37h470a237_0    conda-forge
tzcode                    2018g                h470a237_1    conda-forge
wheel                     0.32.3                   py37_0    conda-forge
xerces-c                  3.2.2                h5d6a6da_0    conda-forge
xz                        5.2.4                h470a237_1    conda-forge
zlib                      1.2.11               h470a237_4    conda-forge

I am trying to run a simple script, using matplotlib

import matplotlib.pyplot as plt
plt.figure(1)                # the first figure
plt.subplot(211)             # the first subplot in the first figure
plt.plot([1, 2, 3])
plt.subplot(212)             # the second subplot in the first figure
plt.plot([4, 5, 6])


plt.figure(2)                # a second figure
plt.plot([4, 5, 6])          # creates a subplot(111) by default

plt.figure(1)                # figure 1 current; subplot(212) still current
plt.subplot(211)             # make subplot(211) in figure1 current
plt.title('Easy as 1, 2, 3') # subplot 211 title

But i get the following error, even though the package shows in the list

 import matplotlib.pyplot as plt
ModuleNotFoundError: No module named 'matplotlib'

Actually, everything was working just fine up until i did a

conda update all

and I also installed QGIS3 (works only with python 3.6 and also requested for GDAL installation, if it matters) but i uninstalled it in case it caused the problem. Now i can't get it the new MG environment to work again. I was getting more errors, this is my third attempt to create a fresh new environment, but i still have problem with matplotlib.

1 Answers

For anyone stumbling upon this question via google as I did, I simply removed and recreated the conda environement:

conda env remove name_of_your_env
conda env create --name the_yml_defining_your_env

For information on how to create said yml please consult the conda documentation, as of now the relevant part is located here. The linked paragraph as well as the following are of interest depending on your usecase.

Related