ModuleNotFoundError: No module named 'matplotlib'

Hello all,
I hope this question isn’t repetitive - I tried finding the solution to this in other Posts and just Googling and still can’t figure it out.

I’m trying to do the Biodiversity in National Parks Capstone Python project (Analyze Data with Python) off Platform but my system can’t find matplotlib. I get the following error:
ModuleNotFoundError: No module named ‘matplotlib’

I think I installed python libraries through miniconda, and noticed matplotlib did not pop up after typing ‘conda list’, so I installed it through pip by typing: ‘pip install matplotlib’. matplotlib now shows up after typing pip list (i have matplotlib 3.6.3).

I read somewhere that sometimes there are compatibility issues between python and certain packages. I have python 3.8.3.

Oh, I’m using a PC and doing this all in bash.

Also, I do NOT see jupyter notebook listed after typing ‘pip list’, but I do see it after typing ‘conda list’. - is this potentially the issue bc my matplotlib is in pip and jupyter notebook is in conda?

I also tried restarting jupyter notebook numerous times in my browser and shutting the jupyter notebook browser tabs and restarting from bash.

I’d appreciate any help and apologies if this question was answered already!

You probably shouldn’t install using both conda and pip. Pick one of them. :slight_smile: I would suggest using pip.

Also, was it (python) installed globally or in a virtual environment?

Since you installed python3, then you need to use pip3 to install packages.

Install virtual env:
python3 -m pip install — user virtualenv

create a virtual env:
python3 -m -venv env

Then activate it:

.\env\Scripts\activate (or whatever dir you installed it in)

You can run the command, where python and then you’ll get the path where you want to install the packages. (If you need diff. versions of packages, or have multiple projects using different versions of python, you’ll have diff environments)

Then install packages there:

pipenv install matplotlib
pipenv install pandas etc, etc.

To install Jupyter in the env:
pipenv install jupyter

To run jupyter:
pipenv run jupyter notebook

See the Python installation documentation for help:

Installing packages using pip and virtual environments — Python Packaging User Guide.

This also might help:
https://pytutorial.com/solved-modulenotfounderror-no-module-named-matplotlib/

And, the pip docs:
https://pip.pypa.io/en/stable/installation/

===========
And, once again, xkcd has encapsulated this perfectly: :rofl:

Thank you so much for your response!

Regarding your question if I installed it globally or in a virtual environment - I’m really sorry - but I’m not sure… I followed the instructions here:
Installing Python 3 and Python Packages | Codecademy
which took me to:
Python Releases for Windows | Python.org

I remember having some problems installing Python. (And it still bewilders me that my computer has Python 3.8.3 but the latest releases are 3.11.1 - not sure how that happened)

I tried typing in your commands above to install the virtual environment, but I get the following error:
Python was not found; run without arguments to install from the Microsoft Store, or disable this shortcut from Settings > Manage App Execution Aliases.

I don’t understand why I’m getting this error bc I have Python 3.8.3 (it pops up when I type in ‘python --version’)

Thank you again for your help - I deeply appreciate it!!!

I am sorry for the confusion. I guess my question is: where did you install Python? (What is the directory path?)

  • You can choose any version of Python to download. So, you can have the latest version…if that’s what you need to use. (I just mentioned environments bc sometimes you might write programs that use diff. versions of python, so, you’d download them in diff. places.)

  • The main limitation with Windows is that (as it says on the Python downloads page), concerning versions of Python: ’ Note that Python 3.11.1 cannot be used on Windows 7 or earlier. https://www.python.org/downloads/windows/
    So, if you have Windows 10 you can download any version.

  • The command python --V only tells you what version you have, not where it is located, which is what you need to know.

  • When you do a where python3 command, that will give you the directory path to where you installed it/where Python lives on your machine. You need to install all your libraries in the same dir path or else, basically your computer won’t know where to ‘find’ them (if that makes sense)

  • When you downloaded Python 3.8.3 did you select the option on the downloads page: “Add Python 3.8 to PATH”? See here.

Think of installing things on your computer this way:

  • When you install something it has a unique directory path, or, it “lives” in a specific place in your computer’s file system. If you want to access it and use it, you need to go to that specific place, right? Think of a GUI (graphical user interface). On a mac, it’s Finder, on Windows it’s File Explorer (or, I think it is. I don’t have a windows-based laptop and my memory is fuzzy. lol). SO, when you install Python, matplotlib, pandas, scipy, whatever libraries, they all need to be in the same directory path (or, PATH). PATH is an environment variable, or I guess a shortcut that tells your system where to look for things. See:
    https://www.maketecheasier.com/what-is-the-windows-path/

This guy on YT might help you understand PATH & virtual environments a bit more:
https://youtu.be/YKSpANU8jPE