Streamlit can not import matplotlib.pyplot

I can’t import matplotlib into my streamlit can someone help me find the solution?

Hi @Kuro,

Thanks for posting!

Can you share the contents of your requirements.txt file?

You can also try importing it directly as such:

pip install matplotlib

I don’t have requirements.txt, do i need to add one if so what do i write in it?

Yes, if you’re deploying the app to Streamlit Community Cloud. Are you able to share a link to your GitHub repo?

Yes @Kuro You do need a requirements.txt, it is mandatory for the app to work.
In the file you add the names of your porject"s dependencies. All the packages that you need for the project to work must be cited in the requirements.txt file.

See: App dependencies - Streamlit Docs

2 Likes

Here is my Github repo

You need to rename requirement.txt to requirements.txt

And change it to matplotlib not matplotlib.pyplot.

You can test that it’s working by doing pip install -r requirements.txt

1 Like

Also, make sure to add streamlit to the requirements.txt file.

Screenshot 2023-10-12 at 10.34.32 AM

@Kuro I pushed a PR Fixing project requirements and data path for streamlit cloud deployment by IndigoWizard · Pull Request #1 · Kurooo38/Lab2_ML · GitHub (the path was still set to your local drive path, which of course wont be possible to fetch by the streamlit app).
Check it, merge and you should be good to go (you can restart the app just in case).

1 Like

You must have requirements.txt but only write pandas, this already contains numpy, matplotlib among others
requirement

Hi, can you help me please.
I am having the same problem “ModuleNotFoundError: No module named ‘matplotlib’” while deploying the app.
There is a requirements.txt file in the main branch as well and matplotlib is mentioned as one of the requirement also. still it is showing an error. Here is the link for the git repo.

Thanks in advance

@Bharath-npl

Just use this:

requirements.txt

numpy
pandas
matplotlib
streamlit
1 Like

Hi @Franky1 Thanks for the quick reply.

I tried but the status is same …

ModuleNotFoundError: No module named ‘matplotlib’

Will it take much time to update the app after editing the Git files ? i refreshed and also re-launched the app… still same status…

1 Like

This should work, try a reboot of the app.

1 Like

Yeah now its working…

Thank you very much.

1 Like

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.