Error trying to connect to MySQL db


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!

@Timothy_Sedlak ,

Could you please post a code snippet of the code that is failing without any sensitive data on it?



Here is my code. Its copied from the streamlit MySQL tutorial.

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"{} has a :{}:")

I have this in my requirements file:

I am getting this error now when running locally:

ModuleNotFoundError: No module named ‘mysqlclient’
File “/home/tsedlak/.local/lib/python3.8/site-packages/streamlit/runtime/scriptrunner/”, line 552, in _run_script
exec(code, module.dict)
File “/mnt/c/mywebapp/”, line 2, in
import mysqlclient

Try adding sqlalchemy and mysql on your imports and try again.