I just went through this, the reason for its failure is that the port, each instance is listening on its port, so you need to specify which port is for this instance.
my code is:
DBserver='host.abc.nt' DBengine='devenv' DBport='####' DBname='mydb' DBDriver='ODBC Driver 13 for SQL Server' DBuser='user' DBpwd='pass' DBuserpass = DBuser if len(DBuser) > 0 else '' DBuserpass = DBuserpass + ':' + DBpwd if len(DBuser) > 0 else '' if len(DBengine) > 0: DBserver = DBserver + '\\' + DBengine engine = sa.create_engine(f'''mssql+pyodbc://{DBuserpass}@{DBserver}:{DBport}/{DBname}?driver={DBDriver}''') query=engine.execute('SELECT DB_NAME(), @@SERVERNAME') query.fetchall() [('mydb', 'host\\devenv')]
After you create the engine, you will not need other libraries except sqlalchemy
but if you want to know the instance port you can use: Github repository from gordthompson / sqlserverport
or on Windows, you connect to SQL Studio (or something else), select the database, and in the meantime, run netstat in CMD to get the port:
C:\Users\pla>netstat -qfa | findstr host TCP 10.0.0.1:
source share