Error trying to connect to MySQL db

Summary

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?

Thanks,

–Carlos

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

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

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