0

I am trying to connect to a MySQL database through Python using PyCharm. I am using the following code.

from errno import errorcode

import pandas as pd
import pypyodbc
import mysql.connector
from mysql.connector import Error
import os

os.environ['LD_LIBRARY_PATH'] = os.getcwd()

team_df = pd.read_excel('sampledata.xlsx', sheet_name='Team')

cnx = mysql.connector.connect(host='localhost',
                              database='SoccerDatabase',
                              user='root',
                              password='')
if cnx.is_connected():
    print("Connection Established")
cnx.close()

However, I am not able to connect and getting the following error:

import pandas as pd
Traceback (most recent call last):
  File "/Users/bansarivadgama/Desktop/integrationfactory/DataModellingIntegrationFactory/venv/lib/python3.11/site-packages/pypyodbc.py", line 428, in <module>
    ODBC_API = ctypes.cdll.LoadLibrary('libodbc.so')
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/ctypes/__init__.py", line 454, in LoadLibrary
    return self._dlltype(name)
           ^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/ctypes/__init__.py", line 376, in __init__
    self._handle = _dlopen(self._name, mode)
                   ^^^^^^^^^^^^^^^^^^^^^^^^^
OSError: dlopen(libodbc.so, 0x0006): tried: 'libodbc.so' (no such file), '/System/Volumes/Preboot/Cryptexes/OSlibodbc.so' (no such file), '/usr/lib/libodbc.so' (no such file, not in dyld cache), 'libodbc.so' (no such file), '/usr/lib/libodbc.so' (no such file, not in dyld cache)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/bansarivadgama/Desktop/integrationfactory/DataModellingIntegrationFactory/dataimport.py", line 5, in <module>
    import pypyodbc
  File "/Users/bansarivadgama/Desktop/integrationfactory/DataModellingIntegrationFactory/venv/lib/python3.11/site-packages/pypyodbc.py", line 440, in <module>
    raise OdbcNoLibrary('ODBC Library is not found. Is LD_LIBRARY_PATH set?')
pypyodbc.OdbcNoLibrary: 'ODBC Library is not found. Is LD_LIBRARY_PATH set?'

Process finished with exit code 1

Could anyone help me out figuring the solution ?

5
  • it seems like the issue is related to the ODBC library not being found. You can resolve this by installing the unixODBC library Commented Feb 16, 2024 at 15:15
  • pyodbc is already installed @JohnAtughara Commented Feb 16, 2024 at 15:18
  • Make sure you have the correct driver path specified in your code. In your case, the driver path should be driver='/usr/local/lib/libmsodbcsql.13.dylib' Commented Feb 16, 2024 at 15:21
  • Check if the driver is listed correctly in your odbcinst.ini file. It should look like this: Copy[ODBC Driver 13 for SQL Server] Description=Microsoft ODBC Driver 13 for SQL Server Driver=/usr/local/lib/libmsodbcsql.13.dylib UsageCount=1 Commented Feb 16, 2024 at 15:22
  • This helped to resolve the error. Commented Feb 17, 2024 at 18:14

1 Answer 1

1

The error is resolved by installing the following library:

brew install unixodbc
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.