I followed a tutorial on connecting to a MySQL database with streamlit, but am getting the following error: ModuleNotFoundError: No module named ‘MySQLdb’. You need to install the ‘mysqlclient’ package to use this connection.
Steps to reproduce
Follow this tutorial:
Is there any guidance you can give on how to set up the app with the requirements.txt, secrets.toml and the main script? I am able to connect to my database without using the secrets file and by importing mysql.connector. I am having issues when trying to use the requirements, secrets and streamlit though. I am sure it’s user error since I am new to this and python. Thanks!
Here is my code. Its copied from the streamlit MySQL tutorial.
# streamlit_app.py
import streamlit as st
# Initialize connection.
conn = st.experimental_connection('mysql', type='sql')
# Perform query.
df = conn.query('SELECT * from mytable;', ttl=600)
# Print results.
for row in df.itertuples():
st.write(f"{row.name} has a :{row.pet}:")
I have this in my requirements file:
pandas
streamlit
mysqlclient==8.0.33
SQLAlchemy==2.0.21
I am getting this error now when running locally:
ModuleNotFoundError: No module named ‘mysqlclient’
Traceback:
File “/home/tsedlak/.local/lib/python3.8/site-packages/streamlit/runtime/scriptrunner/script_runner.py”, line 552, in _run_script
exec(code, module.dict)
File “/mnt/c/mywebapp/Home.py”, line 2, in
import mysqlclient
Thanks for stopping by! We use cookies to help us understand how you interact with our website.
By clicking “Accept all”, you consent to our use of cookies. For more information, please see our privacy policy.
Cookie settings
Strictly necessary cookies
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
Performance cookies
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
Functional cookies
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
Targeting cookies
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.