Installing pyodbc fails on OSX 10.9 (Mavericks)

Question:

When running pip install pyodbc, I get

In file included from .../build/pyodbc/src/buffer.cpp:12:
    .../build/pyodbc/src/pyodbc.h:52:10: fatal error: 'sql.h' file not found
    #include <sql.h>
             ^
    1 error generated.
    error: command 'cc' failed with exit status 1

It seems that Mavericks has no sql.h under /usr/include

Did anyone manage to install pyodbc? Is there a known workaround?

Asked By: Moshe

||

Answers:

As you noticed OSX Mavericks dropped sql headers that are required for PyODBC compilation.
Following these steps allowed me to install PyODBC:

  1. Make sure you have iODBC library installed (http://www.iodbc.org/)
  2. Download and extract iODBC sources
  3. Run pip install --no-install pyodbc
  4. cd [VIRTUAL_ENV]/build/pyodbc
  5. Run python setup.py build_ext --include-dirs=[LIBIODBC_SOURCES]/include/
  6. Run pip install --no-download pyodbc:

    Installing collected packages: pyodbc
      Running setup.py install for pyodbc
    
        warning: no files found matching 'tests/*'
    Successfully installed pyodbc
    Cleaning up...
    

I could as well copy the files under [libiodbc_sources]/include/ to my /usr/include and just run pip install pyodbc, but I didn’t want to add files manually to system folders.

Answered By: m_vitaly

I had no joy with @Vitaly’s answer because there appears to be an issue building packages on Mavericks to do with the lack of support for hard-linking. I couldn’t get the package to build.

So I opted for @Vitaly’s second suggestion which was to copy the necessary files from the [libiodbc_sources]/include/ directory to /usr/include and the install worked find. Here is a list of the files you will need to copy:

  • sql.h
  • sqltypes.h
  • iodbcunix.h
  • sqlext.h
  • sqlucode.h
Answered By: MFB

You can use Homebrew to install unixodbc, then pyodbc via pip in the usual fashion.

brew install unixodbc && pip install pyodbc

This works for me on Mavericks.

Answered By: Ryan Balfanz

If you see errors like

clang: error: unknown argument: '-mno-fused-madd' [-Wunused-command-line-argument-hard-error-in-future]

The problem is that with Mavericks Apple has removed gcc from the command line developer tools; it is now clang just symlinked to gcc. The --mno-fused-madd flag is not supported by clang (same goes for lots of other flags).

One solution might be to install gcc using homebrew or another method and symlink /usr/bin/gcc to a proper gcc.

A simpler workaround that worked for me is suppressing these error by turning them into warnings:

export CFLAGS=-Qunused-arguments

After settings that I was able to pip install pyodbc without errors.

PS! In future versions of clang this might not be possible. At least it works on:

$> gcc -v
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 5.1 (clang-503.0.38) (based on LLVM 3.4svn)
Target: x86_64-apple-darwin13.1.0
Thread model: posix

Refs:
https://bitbucket.org/cffi/cffi/issue/46/unused-mno-fused-madd-clang-warnings-on-os
https://coderwall.com/p/lqpp8w
clang: error: unsupported option '-static-libgcc' on Mac OSX Mavericks

Answered By: asbjornenge

See my installation instructions which I’ve written after some futile attempts of the other answers provided:

First, install the following libraries:

$ brew install unixodbc
$ brew install freetds --with-unixodbc

FreeTDS should already work now, without configuration:

$ tsql -S [IP or hostname] -U [username] -P [password]
locale is "en_US.UTF-8"
locale charset is "UTF-8"
using default charset "UTF-8"
1> ^D

Onto unixODBC, we need to link to the driver, edit /usr/local/etc/odbcinst.ini:

[FreeTDS]
Description = TD Driver (MSSQL)
Driver = /usr/local/lib/libtdsodbc.so
Setup = /usr/local/lib/libtdsodbc.so
FileUsage = 1

The test command we’re using requires configuring a DSN, so edit /usr/local/etc/odbc.ini:

[MYDSN]
Driver = FreeTDS
Server = [IP address]
Port = 1433

The configuration for your DNS might vary, you might need the TDS_Version or Servername directives. The above worked for me for SQL Server 2008 R2. Now, run the test command:

$ isql MYDSN [username] [password] -v
+---------------------------------------+
| Connected!                            |
|                                       |
| sql-statement                         |
| help [tablename]                      |
| quit                                  |
|                                       |
+---------------------------------------+
SQL> ^D

If the test succeeded, you can continue onto installing the Python library pyodbc. The current version of writing (3.0.7) doesn’t link with unixODBC on OS X, so a change has to be made to setup.py. Download the source package and extract it somewhere. Find the following lines (146-147):

    elif sys.platform == 'darwin':
        # OS/X now ships with iODBC.

And change this line:

        settings['libraries'].append('iodbc')

into:

        settings['libraries'].append('odbc')

Then run the following command to install:

> python install .

Now pyodbc should work:

import pyodbc
pyodbc.connect('DSN=MYDSN;UID=[username];PWD=[password]')

You don’t need to have your DSN configured in odbc.ini, so clear that file. You probably want to select a database on connect, so change your connect line to read:

pyodbc.connect('DRIVER=FreeTDS;SERVER=[IP address];PORT=1433;DATABASE=[database];UID=[username];PWD=[password]')

Note that you could also link to the library file of FreeTDS instead of using odbcinst.ini, like this:

pyodbc.connect('DRIVER=/usr/local/lib/libtdsodbc.so;SERVER=[IP address];PORT=1433;DATABASE=[database];UID=[username];PWD=[password]')
Answered By: Bouke

After many dead ends, this worked for me:

$ brew unlink unixodbc
$ brew install unixodbc  --universal
$ sudo pip install --upgrade --global-option=build_ext --global-option="-I/usr/local/include" --global-option="-L/usr/local/lib" --allow-external pyodbc --allow-unverified pyodbc pyodbc
Answered By: dennisobrien

I just went through the whole process on Mac OS X; connecting to pyodbc to MS SQL Server 2014. The whole process is as follows:

Connection pipeline:

pyodbc ----> iodbc ----> freetds ----> MS SQL Server 2014
  1. Build freetds (the SQL Server driver/connector):

    ./configure --prefix=/usr/local --with-tdsver=8.0
    
    make
    
    sudo make install
    
    // you should see /usr/local/lib/libtdsodbc.so was generated
    
    //test method 1:
    
    TDSVER=8.0 tsql -H hostname -p 1433 -U username -P XXX -D databasename
    
    //test method 2:
    
    //config /usr/local/etc/freetds.conf
    [mssqlserver]
        host = XXX
        port = 1433
        tds version = 8.0
    
    //run
    
    tsql -S mssqlserver -U username -P XXX -D databasename  
    
    //if you can run sql, good to go!
    
  2. Build iodbc (ODBC manager):

    //download from github, go to the folder
    
    cd mac
    
    ./configure 
    
    ./make
    
    sudo ./make install
    
    //config /usr/local/etc/odbc.ini
    [mssqlserver]
    Driver=/usr/local/lib/libtdsodbc.so
    TDS_Version=8.0
    Server=xxxx
    Port = 1433
    Trace = Yes
    Description=XXX
    
    //test
    
    which iodbctest
    
    iodbctest 
    
    DSN=masqlserver;UID=xxx;PWD=xxx
    
    //if you can run sql, good to go!
    
  3. Connect pyodbc (Python ODBC wrapper) to iodbc:

    pip install pyodbc 
    
    //in python,
    
    conn = pyodbc.connect("DSN=mssqlserver;UID=xxx;PWD=xxxx") 
    
Answered By: Hongli Deng

I was successful with
sudo port install py-pyodbc

Answered By: Brian

This worked for me after trying just about everything else suggested.

brew install unixodbc
sudo pip install --upgrade --global-option=build_ext --global-option="-I/usr/local/include" --global-option="-L/usr/local/lib" --allow-external pyodbc --allow-unverified pyodbc pyodbc

Running Mac OS 10.11.1,Homebrew 0.9.5, and pip 7.1.2

Answered By: Lothilius

I met the the same problem today on ubuntu 14.04. I found some guy in below link said should install unixodbc-dev.

https://code.google.com/p/pyodbc/issues/detail?id=55

I did it, and then the pip install success.

Hope this helpful.

Answered By: WanderInCode

I’ll add my $0.02 to this. Vitaly’s answer was the main inspiration.

OSX 10.9.5, MacPorts 2.3.4, pip 8.1.2 (which did not have an –no-install option), virtualenv 14.0.6

Also helped: https://stackoverflow.com/a/22942120/1394353

Anyway, install iODBC via MacPorts

sudo port install libiodbc

The missing sql.h is deposited by MacPorts @ /opt/local/include

Now, tell pip where it can find the includes (that’s where the linked answer came in handy):

pip install pyodbc --global-option=build_ext --global-option="-I/opt/local/include/"

Answered By: JL Peyret

OS Version: El Capitan 10.11.6
Python Version: 2.7.11
Pip Version: pip 9.0.1

1. Install iodbc for Mac (my installation is in [iODB_loc]=/usr/local/iODBC)
2. pip install --download [download_location] pyodbc==3.0.10
3. cd [download_location]
4. tar -xvzf pyodbc-3.0.10.tar.gz
5. cd pyodbc-3.0.10
6. vim setup.py: 
settings['libraries'].append('odbc')
->
settings['libraries'].append('iodbc')
7. python setup.py build_ext --include-dirs=[iODB_loc]/include/
8. pip install --upgrade .
Answered By: Lili

As pip doesn’t support --no-install option anymore and the --download option is deprecated. I had to follow the following steps.

pip download pyodbc
tar -zxvf pyodbc-4.0.17.tar.gz
python setup.py build_ext --include-dirs=[DIRECTORY CONTAINING THE HEADERS]
pip install pyodbc
Answered By: bfaskiplar
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.