0

I installed matplotlib in my terminal on windows but whenever i run the code below, it says "ModuleNotFoundError: No module named 'matplotlib.pyplot'; 'matplotlib' is not a package" but I already installed matplotlib. this is the code i want to run:

import matplotlib.pyplot as plt
x = ['jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aug', 'sep', 'oct', 'nov', 'dec']
y =[100,92,53,54,12,547,4,88,11,15,689,7]
plt.plot(x, y)
plt.show()

this is the output it gives:

PS C:\Users\danie\OneDrive\Desktop\Junior year\second semester\programming 2> & C:/Users/danie/AppData/Local/Programs/Python/Python311/python.exe "c:/Users/danie/OneDrive/Desktop/Junior year/second semester/programming 2/matplotlib.py"
Traceback (most recent call last):
  File "c:\Users\danie\OneDrive\Desktop\Junior year\second semester\programming 2\matplotlib.py", line 1, in <module>
    import matplotlib.pyplot as plt
  File "c:\Users\danie\OneDrive\Desktop\Junior year\second semester\programming 2\matplotlib.py", line 1, in <module>
    import matplotlib.pyplot as plt
ModuleNotFoundError: No module named 'matplotlib.pyplot'; 'matplotlib' is not a package

someone help me please. Here is an image of my code and my error code.

i have two versions of python : 311 and 310, i uninstalled both and redownloaded 311 but it messed up my 'pip' so i went to the recycle bin to undo the delete now my 'pip' works but 'matplotlib' still doesn't work. i use both VScode and IDLE and they both do not work when running this code.

4
  • 2
    It seems like you have something called matplotlib.py in the same folder which Python is seeing first instead of the actual package. So, make sure there's no files or folders with that name in the same directory as your script. Commented Mar 22, 2023 at 22:19
  • 1
    To fix this problem, rename your Python script to something other than matplotlib.py, like plot someothername.py, and delete any matplotlib.pyc or __pycache__ folders that may have been created in the same directory as your script. Let me know if it did work Commented Mar 22, 2023 at 22:30
  • 1
    just renamed it and it works now, thanks alot! Commented Mar 23, 2023 at 23:06
  • @FatokeAnjolaoluwa When it works, please mark the answer as accepted, also see help. Commented Jul 1, 2024 at 19:39

1 Answer 1

4

I just looked at your screenshot and the issue is that your script is called matplotlib.py. You are trying to import the current file into itself. You should rename it to something else so that its name isn't the same as matplotlib.

Sign up to request clarification or add additional context in comments.

2 Comments

just renamed it and it works now, thank you!
Bro nothing was working, only this answer saved my life.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.