ModuleNotFoundError: No module named 'sqlalchemy'

Hi!

My app need to source data from postgresql, however when I launched the app, I got the following errors:

Traceback (most recent call last):
  File "/home/adminuser/venv/lib/python3.11/site-packages/streamlit/runtime/scriptrunner/script_runner.py", line 541, in _run_script
    exec(code, module.__dict__)
  File "/mount/src/aisreport/interface.py", line 4, in <module>
    conn = st.experimental_connection("postgresql", type="sql")
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/adminuser/venv/lib/python3.11/site-packages/streamlit/runtime/connection_factory.py", line 298, in connection_factory
    raise ModuleNotFoundError(f"{str(e)}. {extra_info}")
ModuleNotFoundError: No module named 'sqlalchemy'. You need to install the 'sqlalchemy' package to use this connection.

Although in my requirements.txt file, I already included the sqlalchemy. These are my links:

Hi @linhphan09, welcome to our forums! :raised_hands:

A couple of questions:

  • Is the app working locally with this setup?
  • Have you tried specifying a different version for sqlalchemy (e.g., sqlalchemy==2.0.22)?

Best wishes,
Charly

Hi @Charly_Wargnier ! Thank you for your response.
For the questions:

  • I’m quite new with streamlit. Could you elaborate more on how I can setup the app so it work locally?

  • I have tried another version for sqlalchemy, but the same error still persists.

Cheers!

Absolutely @linhphan09!

Here’s a step-by-step guide for using Streamlit on your local machine.

1. Setup a Virtual Environment:

python -m venv streamlit_env
source streamlit_env/bin/activate

2. Install Streamlit:

pip install streamlit

3. Write Your App (app.py):

import streamlit as st
st.title('My First Streamlit App')
name = st.text_input('Enter your name:')
st.write(f'Hello, {name}!')

4. Run the App:

streamlit run app.py

To view, open http://localhost:8501 in a browser.

5. Stop the App:

Press Ctrl+C in the terminal.

Let me know if the app is indeed working locally. If not, we can investigate further.

Best,
Charly

Hi @Charly_Wargnier
In my local app, it works well if it’s the exact same prompt (without connecting to postgresql). Once I use the previous code that I used in my repository, it encountered another error of not finding the secrets file path.

Thanks!

Did you add your secrets in the Streamlit Community Cloud settings pane?

If not, here’s how to do it:

Charly

By the way, as a best practice, you should keep your secret info out of your GitHub repository. Be sure to add your secrets.toml file to your .gitignore so you don’t accidentally commit them. :slight_smile:

Best,
Charly

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