Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Using Ubuntu 10.04 LTS, I'm trying to connect to an MS SQL Server we have on another box from within a Python script. What are the steps I need to follow to do so? Right now, I'm trying to use the pyodbc module, but whenever I try to connect I get the following error:

Data source name not found, and no default driver specified (0)(SQLDriverConnectW)

So I looked at the odbvcinst.ini file which was empty, added an entry, and now when I use one of the items I've defined there I get the following error:

Driver's SQLAllocHandle on SQL_HANDLE_HENV failed (0)(SQLDriverConnectW)

I've installed: unixodb unixodbc-dev freetds-dev freetds-bin. I've put this on SF and not SO because I'm thinking this is a server configuration issue. Which driver should I have and how do I reference it in which file, etc? (I'm pretty new to Ubuntu)

share|improve this question
up vote 2 down vote accepted

Have you set up the data source in your /etc/freetds.conf file?

The entry should look something like:

# A typical Microsoft server
        host =
        port = 1433
        tds version = 7.0

(Though I tend to use tds version = 8.0 when I'm working with MS SQL Server, and it works fine)

Edit: (Going to put my responses here so I can code-format them)

Your /etc/odbcinst.ini should look like:

Description = FreeTDS
Driver = /usr/lib/ 
Setup = /usr/lib/
CPTimeout =
CPReuse =

(Make sure the driver paths are right. Sometimes it's /usr/lib/odbc/)

Then the python string should look like:

import pyodbc
connection = pyodbc.connect("DRIVER={MSSQL};;UID=username;PWD=password;DATABASE=databasename")
share|improve this answer
Ok I've done this, and I'm getting the same error. Do I need to point to this somewhere in the odbcinst.ini? Right now in that file I have driver=/usr/lib/, I take it this is wrong? – Nick Jul 1 '10 at 15:51
Works - thanks for the help! – Nick Jul 1 '10 at 16:08
Update: As described in this post on pyodbc and mssql, on Ubuntu 12.04 the drivers have been moved to /usr/lib/x86_64-linux-gnu/odbc/. Gonna leave this here for any other late-comers like myself :) – george Oct 22 '12 at 19:59

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.