Data source name not found and no default driver specified

TheExecutable 15 Reputation points
2023-03-30T14:19:55.6833333+00:00

Dear everyone

I am trying to connect to an mssql server via Python using the pyodbc package. I have the following code

1. import pyodbc
2. 
3. cnxn_str = ("Driver={SQL Server Native Client 11.0};"
4.             "Server=MyServer;"
5.             "Database=Test;"
6.             "Trusted_Connection=yes;")
7. 
8. cnxn = pyodbc.connect(cnxn_str)

I keep getting the error message "Data source name not found and no default driver specified" from line 8. I have tried using "SQL Server Native Client 11.0" as well as "odbc driver 18 for sql server" as a Driver if someone is curious.

The full Traceback is:

Traceback (most recent call last):
  File "d:\Database\test.py", line 8, in <module>
    cnxn = pyodbc.connect(cnxn_str)
pyodbc.InterfaceError: ('IM002', '[IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified (0) (SQLDriverConnect)')
PS D:\Database>

Can anyone guide me through how to connect succesfully and execute queries on a MSSQL with Python using the pyodbc package? I have read the documentation and can't really seem to see why I keep getting the above error. Do I need to download something external or configure my MSSQL in a different way? Thanks in advance.

Windows for business | Windows Client for IT Pros | User experience | Other
SQL Server | Other
{count} votes

2 answers

Sort by: Most helpful
  1. Bruce (SqlWork.com) 78,006 Reputation points Volunteer Moderator
    2023-03-30T15:24:35.83+00:00
    1 person found this answer helpful.
    0 comments No comments

  2. Seeya Xi-MSFT 16,586 Reputation points
    2023-03-31T06:22:41.18+00:00

    Hi @TheExecutable ,

    Please refer to this similar thread:

    https://stackoverflow.com/questions/46045834/pyodbc-data-source-name-not-found-and-no-default-driver-specified

    You can check the DSN 64-bit with ODBC Data Source Administrator if you are using a 64-bit version of Windows( If you are on 32-bit Windows, select 32-bit).

    Best regards,

    Seeya


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    1 person found this answer helpful.
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.