Python Forum
ModulNotFoundError matplotlib
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
ModulNotFoundError matplotlib
#1
I'm new to Python and have installed Python 3.12.3 with idle on Windows 10. Having had fairly good success with other scripts, I decided to try to code a simple plot from example, I believe I have installed matplotlib, but I get an error message "ModuleNotFoundError: No module named 'matplotlib'"
Filename scatter_plot.py
Thanks,
Here's the script:

import matplotlib.pyplot as plt
import numpy as np

# Fixing random state for reproducibility
np.random.seed(19680801)


N = 50
x = np.random.rand(N)
y = np.random.rand(N)
colors = np.random.rand(N)
area = (30 * np.random.rand(N))**2  # 0 to 15 point radii

plt.scatter(x, y, s=area, c=colors, alpha=0.5)
plt.show()
Larz60+ write Apr-26-2024, 09:15 AM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Fixed for you this time. Please use BBCode tags on future posts.
Reply
#2
How did you install matplotlib? Import does not install a package.
Reply
#3
(Apr-26-2024, 01:14 AM)deanhystad Wrote: How did you install matplotlib? Import does not install a package.
I went to matplotlib's website and did a bunch of things that it seemed to be telling me to do to get matplotlib. I'm not sure how to check what you're asking, though I'll repost.
import matplotlib.pyplot as plt
import numpy as np

# Fixing random state for reproducibility
np.random.seed(19680801)


N = 50
x = np.random.rand(N)
y = np.random.rand(N)
colors = np.random.rand(N)
area = (30 * np.random.rand(N))**2  # 0 to 15 point radii

plt.scatter(x, y, s=area, c=colors, alpha=0.5)
plt.show()
and here's the error:

Error:
= RESTART: C:\Users\richb\AppData\Local\Programs\Python\Python312\Lib\idlelib\scatter_plot.py Traceback (most recent call last): File "C:\Users\richb\AppData\Local\Programs\Python\Python312\Lib\idlelib\scatter_plot.py", line 1, in <module> import matplotlib.pyplot as plt ModuleNotFoundError: No module named 'matplotlib'
Reply
#4
Quote:I went to matplotlib's website and did a bunch of things that it seemed to be telling me to do to get matplotlib
That is not an answer.

I don't think you installed matplotlib, at least not the python interface to the C++ matplotlib libraries. I installed matplotlib by typing this in a cmd shell:
Output:
pip install matplotlib
To know what to type I went to the python package index.

https://pypi.org/

In the little search window I entered matplotlib. That brought up a list containing matplotlib and a bunch of related packates. I clicked on "matplotlib" and it took me here:

https://pypi.org/project/matplotlib/
Reply
#5
(Apr-26-2024, 04:36 PM)deanhystad Wrote:
Quote:I went to matplotlib's website and did a bunch of things that it seemed to be telling me to do to get matplotlib
That is not an answer.

I don't think you installed matplotlib, at least not the python interface to the C++ matplotlib libraries. I installed matplotlib by typing this in a cmd shell:
Output:
pip install matplotlib
To know what to type I went to the python package index.

https://pypi.org/

In the little search window I entered matplotlib. That brought up a list containing matplotlib and a bunch of related packates. I clicked on "matplotlib" and it took me here:

https://pypi.org/project/matplotlib/

sorry, yes I did go to the command prompt and did exactly what you did, before my first post. I don't remember for sure what the response from pip/command prompt was, but I think that recall that there wasn't any definitive response to the pip action, at least that would leadme to believe it shouldn't work. Sounds like you're implying that I should start over, maybe reinstall python and matplotlib from scratch?
Reply
#6
You can do "pip list" to see a list of packages installed using pip. There's also no harm in reinstalling.

pip install matplotlib should print a lot of stuff
Output:
c:\projects\bergen>pip install matplotlib Collecting matplotlib Downloading matplotlib-3.8.4-cp311-cp311-win_amd64.whl.metadata (5.9 kB) Collecting contourpy>=1.0.1 (from matplotlib) Downloading contourpy-1.2.1-cp311-cp311-win_amd64.whl.metadata (5.8 kB) Collecting cycler>=0.10 (from matplotlib) Downloading cycler-0.12.1-py3-none-any.whl.metadata (3.8 kB) Collecting fonttools>=4.22.0 (from matplotlib) Downloading fonttools-4.51.0-cp311-cp311-win_amd64.whl.metadata (162 kB) .... and on and on and on... Installing collected packages: python-dateutil, pillow, kiwisolver, fonttools, cycler, contourpy, matplotlib Successfully installed contourpy-1.2.1 cycler-0.12.1 fonttools-4.51.0 kiwisolver-1.4.5 matplotlib-3.8.4 pillow-10.3.0 python-dateutil-2.9.0.post0
If already installed, the output is a lot shorter
Output:
c:\projects\bergen>pip install matplotlib Requirement already satisfied: matplotlib in c:\projects\bergen\.venv\lib\site-packages (3.8.4) Requirement already satisfied: contourpy>=1.0.1 in c:\projects\bergen\.venv\lib\site-packages (from matplotlib) (1.2.1) Requirement already satisfied: cycler>=0.10 in c:\projects\bergen\.venv\lib\site-packages (from matplotlib) (0.12.1) Requirement already satisfied: fonttools>=4.22.0 in c:\projects\bergen\.venv\lib\site-packages (from matplotlib) (4.51.0) Requirement already satisfied: kiwisolver>=1.3.1 in c:\projects\bergen\.venv\lib\site-packages (from matplotlib) (1.4.5) Requirement already satisfied: numpy>=1.21 in c:\projects\bergen\.venv\lib\site-packages (from matplotlib) (1.26.4) Requirement already satisfied: packaging>=20.0 in c:\projects\bergen\.venv\lib\site-packages (from matplotlib) (24.0) Requirement already satisfied: pillow>=8 in c:\projects\bergen\.venv\lib\site-packages (from matplotlib) (10.3.0) Requirement already satisfied: pyparsing>=2.3.1 in c:\projects\bergen\.venv\lib\site-packages (from matplotlib) (3.1.2) Requirement already satisfied: python-dateutil>=2.7 in c:\projects\bergen\.venv\lib\site-packages (from matplotlib) (2.9.0.post0) Requirement already satisfied: six>=1.5 in c:\projects\bergen\.venv\lib\site-packages (from python-dateutil>=2.7->matplotlib) (1.16.0)
Reply
#7
(Apr-26-2024, 12:05 AM)WicardBohnam Wrote: I'm new to Python and have installed Python 3.12.3 with idle on Windows 10
Here a addition to deanhystad answers.
Start to test that all work from command line.
Here me just doing this this in cmd:
C:\Windows>cd ..

# Test Python 
C:\>python --version
Python 3.12.2

# Test pip
C:\>pip --version
pip 24.0 from C:\Python312\Lib\site-packages\pip (python 3.12)

# Install matplotlib
C:\>pip install matplotlib --upgrade
Collecting matplotlib
  Downloading matplotlib-3.8.4-cp312-cp312-win_amd64.whl.metadata (5.9 kB)
.....
Installing collected packages: pyparsing, kiwisolver, fonttools, cycler, matplotlib
Successfully installed cycler-0.12.1 fonttools-4.51.0 kiwisolver-1.4.5 matplotlib-3.8.4 pyparsing-3.1.2

# Test that it work
C:\>python
Python 3.12.2 (tags/v3.12.2:6abddd9, Feb  6 2024, 21:26:36) [MSC v.1937 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import matplotlib
>>> matplotlib.__version__
'3.8.4'
>>> exit()
C:\>
When all works from command and not eg from Editors/Ide,then have to tell it to use right Python interpreter.
All Editors/Ide have setup for this,eg for you this is the placement of your Python interpreter:
C:\Users\richb\AppData\Local\Programs\Python\python.exe
Reply
#8
(Apr-26-2024, 04:59 PM)deanhystad Wrote: You can do "pip list" to see a list of packages installed using pip. There's also no harm in reinstalling.

pip install matplotlib should print a lot of stuff
Output:
c:\projects\bergen>pip install matplotlib Collecting matplotlib Downloading matplotlib-3.8.4-cp311-cp311-win_amd64.whl.metadata (5.9 kB) Collecting contourpy>=1.0.1 (from matplotlib) Downloading contourpy-1.2.1-cp311-cp311-win_amd64.whl.metadata (5.8 kB) Collecting cycler>=0.10 (from matplotlib) Downloading cycler-0.12.1-py3-none-any.whl.metadata (3.8 kB) Collecting fonttools>=4.22.0 (from matplotlib) Downloading fonttools-4.51.0-cp311-cp311-win_amd64.whl.metadata (162 kB) .... and on and on and on... Installing collected packages: python-dateutil, pillow, kiwisolver, fonttools, cycler, contourpy, matplotlib Successfully installed contourpy-1.2.1 cycler-0.12.1 fonttools-4.51.0 kiwisolver-1.4.5 matplotlib-3.8.4 pillow-10.3.0 python-dateutil-2.9.0.post0
If already installed, the output is a lot shorter
Output:
c:\projects\bergen>pip install matplotlib Requirement already satisfied: matplotlib in c:\projects\bergen\.venv\lib\site-packages (3.8.4) Requirement already satisfied: contourpy>=1.0.1 in c:\projects\bergen\.venv\lib\site-packages (from matplotlib) (1.2.1) Requirement already satisfied: cycler>=0.10 in c:\projects\bergen\.venv\lib\site-packages (from matplotlib) (0.12.1) Requirement already satisfied: fonttools>=4.22.0 in c:\projects\bergen\.venv\lib\site-packages (from matplotlib) (4.51.0) Requirement already satisfied: kiwisolver>=1.3.1 in c:\projects\bergen\.venv\lib\site-packages (from matplotlib) (1.4.5) Requirement already satisfied: numpy>=1.21 in c:\projects\bergen\.venv\lib\site-packages (from matplotlib) (1.26.4) Requirement already satisfied: packaging>=20.0 in c:\projects\bergen\.venv\lib\site-packages (from matplotlib) (24.0) Requirement already satisfied: pillow>=8 in c:\projects\bergen\.venv\lib\site-packages (from matplotlib) (10.3.0) Requirement already satisfied: pyparsing>=2.3.1 in c:\projects\bergen\.venv\lib\site-packages (from matplotlib) (3.1.2) Requirement already satisfied: python-dateutil>=2.7 in c:\projects\bergen\.venv\lib\site-packages (from matplotlib) (2.9.0.post0) Requirement already satisfied: six>=1.5 in c:\projects\bergen\.venv\lib\site-packages (from python-dateutil>=2.7->matplotlib) (1.16.0)
Yes, thanks, maybe in those details there's an indication of what's wrong, wouldn't mind your opinion
Output:
C:\Users\richb>pip install matplotlib Defaulting to user installation because normal site-packages is not writeable Requirement already satisfied: matplotlib in c:\users\richb\appdata\local\packages\pythonsoftwarefoundation.python.3.12_qbz5n2kfra8p0\localcache\local-packages\python312\site-packages (3.8.4) Requirement already satisfied: contourpy>=1.0.1 in c:\users\richb\appdata\local\packages\pythonsoftwarefoundation.python.3.12_qbz5n2kfra8p0\localcache\local-packages\python312\site-packages (from matplotlib) (1.2.1) Requirement already satisfied: cycler>=0.10 in c:\users\richb\appdata\local\packages\pythonsoftwarefoundation.python.3.12_qbz5n2kfra8p0\localcache\local-packages\python312\site-packages (from matplotlib) (0.12.1) Requirement already satisfied: fonttools>=4.22.0 in c:\users\richb\appdata\local\packages\pythonsoftwarefoundation.python.3.12_qbz5n2kfra8p0\localcache\local-packages\python312\site-packages (from matplotlib) (4.51.0) Requirement already satisfied: kiwisolver>=1.3.1 in c:\users\richb\appdata\local\packages\pythonsoftwarefoundation.python.3.12_qbz5n2kfra8p0\localcache\local-packages\python312\site-packages (from matplotlib) (1.4.5) Requirement already satisfied: numpy>=1.21 in c:\users\richb\appdata\local\packages\pythonsoftwarefoundation.python.3.12_qbz5n2kfra8p0\localcache\local-packages\python312\site-packages (from matplotlib) (1.26.4) Requirement already satisfied: packaging>=20.0 in c:\users\richb\appdata\local\packages\pythonsoftwarefoundation.python.3.12_qbz5n2kfra8p0\localcache\local-packages\python312\site-packages (from matplotlib) (24.0) Requirement already satisfied: pillow>=8 in c:\users\richb\appdata\local\packages\pythonsoftwarefoundation.python.3.12_qbz5n2kfra8p0\localcache\local-packages\python312\site-packages (from matplotlib) (10.3.0) Requirement already satisfied: pyparsing>=2.3.1 in c:\users\richb\appdata\local\packages\pythonsoftwarefoundation.python.3.12_qbz5n2kfra8p0\localcache\local-packages\python312\site-packages (from matplotlib) (3.1.2) Requirement already satisfied: python-dateutil>=2.7 in c:\users\richb\appdata\local\packages\pythonsoftwarefoundation.python.3.12_qbz5n2kfra8p0\localcache\local-packages\python312\site-packages (from matplotlib) (2.9.0.post0) Requirement already satisfied: six>=1.5 in c:\users\richb\appdata\local\packages\pythonsoftwarefoundation.python.3.12_qbz5n2kfra8p0\localcache\local-packages\python312\site-packages (from python-dateutil>=2.7->matplotlib) (1.16.0)
Reply
#9
You have two Python versions on your Pc.
This on mean that you have installed a Microsoft python 3.12 app version :
Requirement already satisfied: matplotlib in c:\users\richb\appdata\local\packages\pythonsoftwarefoundation.python.3.12_qbz5n2kfra8p0\localcache\local-packages\python312\site-packages (3.8.4)

Your first error message mean that you also have version from Python.org Python 3.12.2
  File "C:\Users\richb\AppData\Local\Programs\Python\Python312\Lib\idlelib\scatter_plot.py", line 1, in <module>
    import matplotlib.pyplot as plt
ModuleNotFoundError: No module named 'matplotlib
No problem to have several version Python,i have lot more.
Just that have you to choose right Python interpreter from Editor/Ide you run code from.
If this confusing for you,i would go with version from Python.org and uninstall the Microsoft python 3.1 2 app version.
Reply
#10
(Apr-26-2024, 05:38 PM)snippsat Wrote: You have two Python versions on your Pc.
This on mean that you have installed a Microsoft python 3.12 app version :
Requirement already satisfied: matplotlib in c:\users\richb\appdata\local\packages\pythonsoftwarefoundation.python.3.12_qbz5n2kfra8p0\localcache\local-packages\python312\site-packages (3.8.4)

Your first error message mean that you also have version from Python.org Python 3.12.2
  File "C:\Users\richb\AppData\Local\Programs\Python\Python312\Lib\idlelib\scatter_plot.py", line 1, in <module>
    import matplotlib.pyplot as plt
ModuleNotFoundError: No module named 'matplotlib
No problem to have several version Python,i have lot more.
Just that have you to choose right Python interpreter from Editor/Ide you run code from.
If this confusing for you,i would go with version from Python.org and uninstall the Microsoft python 3.1 2 app version.

Yeah, that was it, thanks so much. Deleted both versions, reinstalled Python from the Python.org, reinstalled pip and matplotlib.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Matplotlib: How do I convert Dates from Excel to use in Matplotlib JaneTan 1 3,273 Mar-11-2021, 10:52 AM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020