Mohammad Azhar Khan aka MAK

Software Engineer. Loves Python. Knows Python, Flask, Django, Node.js, serverless,AWS lambda and many more.

 How to connect to MS SQL Server using Python3 on MacOS

15 May 2017 » python, Python3, MsSql, MacOS

MS SQL Server

MS SQL Server

Simply follow the steps below

Install pyodbc in python3

pip3 install pyodbc


python3 -m pip install pyodbc

Now install Microsoft ODBC Driver (need to have Homebrew installed on your system)

brew tap microsoftmsodbcsql

Update homebrew

brew update

Final Step

brew install msodbcsql

Now take idea from following python3 code

def sqlServerConnect(driverType,pip, pdbname, pusername="", ppwd=""):
    from pyodbc import connect
        #trying to create a cnnection
        connection = connect(driver=driverType, host=pip, database=pdbname, user=pusername,password=ppwd)
        print("Connection successful : "+driverType+" "+pip+" "+" "+pdbname+" "+pusername)
        return connection
    except Exception as e:
        #connection creation failed
        return None

sqlServerConnect('{ODBC Driver 13 for SQL Server}','server_name/ip','databse_name','username','password')

Hope it helps.