Problem importing matplotlib in Python

In summary, pero_ found that if he changed the name of a file called numbers.py in his Python search path, that fixed the problem.
  • #1
PeroK
Science Advisor
Homework Helper
Insights Author
Gold Member
2023 Award
27,915
19,402
TL;DR Summary
Installed successfully but won't import
I'm running Python 3.8.3 on Windows 10 and was have installed matplotlib, but I can't get it to work. If I try import matplotlib then first it seems to spit out some output (always the same) from a couple of basic test programs I wrote (which is really weird), then just gets a bunch of errors:

c=300000000
Favorite number is 1729
Traceback (most recent call last):
File "C:\Users\pero_\OneDrive\Documents\python_work\mpl_squares.py", line 1, in <module>
import matplotlib
File "C:\Users\pero_\AppData\Roaming\Python\Python38\site-packages\matplotlib\__init__.py", line 139, in <module>
from . import cbook, rcsetup
File "C:\Users\pero_\AppData\Roaming\Python\Python38\site-packages\matplotlib\cbook\__init__.py", line 32, in <module>
import numpy as np
File "C:\Users\pero_\AppData\Roaming\Python\Python38\site-packages\numpy\__init__.py", line 145, in <module>
from . import core
File "C:\Users\pero_\AppData\Roaming\Python\Python38\site-packages\numpy\core\__init__.py", line 70, in <module>
from . import numerictypes as nt
File "C:\Users\pero_\AppData\Roaming\Python\Python38\site-packages\numpy\core\numerictypes.py", line 596, in <module>
_register_types()
File "C:\Users\pero_\AppData\Roaming\Python\Python38\site-packages\numpy\core\numerictypes.py", line 591, in _register_types
numbers.Integral.register(integer)
AttributeError: module 'numbers' has no attribute 'Integral'
[Finished in 0.9s]

I've been down a rabbit hole with this most of the day. I've tried what I found here, messing about with different WHL files:

https://ehmatthes.github.io/pcc/chapter_15/README.html#installing-matplotlib-on-windows

Any ideas?

Thanks
 
Technology news on Phys.org
  • #2
Are you using pip or anaconda?

It looks like you have a numbers.py file somewhere in your python search path aka PYTHONPATH that is being processed over the correct one needed by numerictypes.py
 
  • #3
pip
 
  • #6
How do I fix that?
 
  • #7
Look through your code to see if you have a file you named numbers.py
and change its name.
 
  • Informative
  • Like
Likes Borg and PeroK
  • #8
Aha, that was the test program that was printing the speed of light and the number 1729.

It's gone and that seems to have fixed it. Thanks a million!
 
  • #9
Great, I’m glad that was easy to find. We don’t use pip for our projects instead we use anaconda which comes loaded with a lot of modules all in one tar because works better in an airgapped environment with no internet access.
 

FAQ: Problem importing matplotlib in Python

What is matplotlib and why is it important?

Matplotlib is a popular data visualization library in Python that allows users to create interactive and high-quality graphs, charts, and plots. It is important for data scientists and researchers as it helps them to analyze and present their data in a more visually appealing and understandable manner.

Why am I having trouble importing matplotlib in Python?

There could be multiple reasons for this issue. Some common reasons include incorrect installation of matplotlib, using an outdated version of Python, or having conflicting dependencies. It is also possible that there may be an error in the code itself.

How can I fix the "Problem importing matplotlib in Python" error?

The first step is to check if matplotlib is properly installed. If not, try reinstalling it using a package manager such as pip or conda. If the issue persists, make sure that you are using the correct version of Python and that there are no conflicts with other dependencies. You can also try updating your code or seeking help from the matplotlib community.

Can I use matplotlib with other programming languages?

Yes, matplotlib is compatible with other programming languages such as R, Java, and Julia. However, the syntax and usage may differ slightly between languages.

Are there any alternatives to matplotlib for data visualization in Python?

Yes, there are other popular data visualization libraries in Python such as seaborn, plotly, and bokeh. Each of these libraries has its own advantages and may be more suitable for certain types of data or graphs. It is recommended to explore and experiment with different libraries to find the best fit for your specific needs.

Similar threads

Back
Top