Connecting to mysql on Macbook M1 (pyodbc)

Summary

I am trying to connect to my local database running on docker from streamlit.
I have no problems with connecting within Python programs, but as soon as I try to import pyodbc as described here (Connect Streamlit to Microsoft SQL Server - Streamlit Docs ) I get the following error:
ImportError: dlopen(/opt/homebrew/lib/python3.11/site-packages/pyodbc.cpython-311-darwin.so, 0x0002): symbol not found in flat namespace ‘_SQLAllocHandle’

Steps to reproduce

Code snippet:

  1. I inserted my access data in # .streamlit/secrets.toml
server = "localhost"
database = "mydb"
username = "SA"
password = "xxx"
  1. installed pyodbc (pip) and added it to requirements.txt
    pyodbc== 4.0.35

added following code to my app:

import pyodbc
    # Initialize connection.
    # Uses st.cache_resource to only run once.
    @st.cache_resource
    def init_connection():
        return pyodbc.connect(
            "DRIVER={ODBC Driver 18 for SQL Server};SERVER="
            + st.secrets["server"]
            + ";DATABASE="
            + st.secrets["database"]
            + ";UID="
            + st.secrets["username"]
            + ";PWD="
            + st.secrets["password"]
        )

    conn = init_connection()

If applicable, please provide the steps we should take to reproduce the error or specified behavior.

Expected behavior:
I can import pyodbc and create connection

Actual behavior:
import pyodbc
ImportError: dlopen(/opt/homebrew/lib/python3.11/site-packages/pyodbc.cpython-311-darwin.so, 0x0002): symbol not found in flat namespace ‘_SQLAllocHandle’

Debug info

  • Streamlit version: Streamlit, version 1.19.0
  • Python version: Python 3.10.9
  • Using Conda? conda 4.12.0
  • OS version: macOs 13.0.1 (m1 Chip)
  • Browser version: Google Chrome Version 109.0.5414.119 (Official Build) (arm64)

Requirements file

Using Conda? PipEnv? PyEnv? Pex? Share the contents of your requirements file here.

pymssql==2.2.7

streamlit==1.19.0

charset-normalizer==2.0.9

openai

pyyaml

python-dotenv

streamlit-chat

plotly

bokeh==2.4.3

altair

pm4py

seaborn

pyodbc== 4.0.35

(App dependencies - Streamlit Docs) and add a requirements file to your app.

Links

  • Link to your GitHub repo:
  • Link to your deployed app:

Additional information

If needed, add any other context about the problem here.

Just google this and you will find some hints, for example:

I found the solution for my first problem :slightly_smiling_face:
I uninstalled everything in my environment
and installed from scratch this time with no binary pyodbc
it seemed to work with some similar issues.
maybe it would help somebody:

in requirements.txt:

pyodbc --no-binary=pyodbc==4.0.35

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