89

I'm trying to install PyODBC on Heroku, but I get fatal error: sql.h: No such file or directory in the logs when pip runs. How do I fix this error?

davidism
  • 121,510
  • 29
  • 395
  • 339
shashi kanth
  • 1,031
  • 1
  • 8
  • 7

7 Answers7

177

To follow up on the answer below...

Example for Ubuntu:

sudo apt-get install unixodbc unixodbc-dev

Example for CentOS:

sudo yum install unixODBC-devel

Example for Fedora:

sudo dnf install unixODBC-devel

On Windows:

conn = pyodbc.connect('DRIVER={SQL Server};SERVER=yourserver.yourcompany.com;DATABASE=yourdb;UID=user;PWD=password')

On Linux:

conn = pyodbc.connect('DRIVER={FreeTDS};SERVER=yourserver.yourcompany.com;PORT=1433;DATABASE=yourdb;UID=user;PWD=password;TDS_VERSION=7.2')
Sazid
  • 2,747
  • 1
  • 22
  • 34
FlipperPA
  • 13,607
  • 4
  • 39
  • 71
  • Hi Flipper!thanks for the commands. I'm using Git Bash for windows. Can you please suggest how do i do this. One more thing is i'm able to install pyodbc package locally in my system. But when heroku server is trying to install the same package on server side, i'm receiving this error. – shashi kanth Jul 12 '15 at 03:47
  • No problem! So, you're using Windows for development and Linux in production on Heroku? This can get tricky (the holy grail is to have your development and production environments be as identical as possible). First I would suggest definitely using a virtualenv, then using pyodbc with the SQL Server driver on Windows / dev, and FreeTDS/unixODBC on Linux / prod Heroku. I'll amend my example above. – FlipperPA Jul 12 '15 at 16:34
  • Thanks a lot!I'm gonna try this. – shashi kanth Jul 13 '15 at 00:31
  • Let me know if it works. I would also highly recommend trying to get your development and production environments at least on the same operating system. Good luck. – FlipperPA Jul 13 '15 at 20:17
  • 1
    Did anyone get this to work on Heroku? None of the buildpacks seem to be working now that supported django-pyodbc – cph Apr 24 '16 at 08:06
  • Can you suggest, how do I solve this error when I use MongoDB as a database? The Python code connects to the Mongodb database in the cloud and I get the same error while trying to push into Heroku.Thanks – mari Feb 01 '18 at 12:50
  • If I'm deploying to Heroku would I include the sudo apt-get install unixodbc unixodbc-dev in the Aptfile? – user2200270 Jul 13 '20 at 06:53
  • 1
    for osx `brew install unixodbc` – northtree Mar 28 '22 at 06:20
14

You can add Heroku build pack to preinstall required apt packages first

heroku buildpacks:add --index 1 https://github.com/heroku/heroku-buildpack-apt

Add Aptfile in the your directory root and to the repository as well

unixodbc
unixodbc-dev
python-pyodbc
libsqliteodbc

It will install everything you need to work with pyodbc or aioodbc packages from python on Heroku

Most Wanted
  • 6,254
  • 5
  • 53
  • 70
8

You need the unixODBC devel package. I don't know what distro you are using but you can google it and build from source.

pbhowmick
  • 1,093
  • 11
  • 26
3

You don't have the required ODBC header files on your machine. You need to run below command to get g++ installed

yum install unixODBC-devel
StackOverFlow
  • 4,486
  • 12
  • 52
  • 87
1

The other answers are more or less correct; you're missing the unixodbc-dev[el] package for your operating system; that's what pip needs in order to build pyodbc from source.

However, a much easier option is to install pyodbc via the system package manager. On Debian/Ubuntu, for example, that would be apt-get install python-pyodbc. Since pyodbc has a lot of compiled components and interfaces heavily with the UnixODBC OS-level packages, it is probably a better fit for a system package rather than a Python/pip-installed one.

You can still list it as a dependency in your requirements.txt files if you're making code for distribution, but it'll usually be easier to install it via the system PM.

Zac B
  • 3,796
  • 3
  • 35
  • 52
  • 1
    The only problem with installing pyodbc from the OS repositories is that they tend to be *really old* versions of pyodbc. IIRC the Ubuntu repositories install v3.0.7 while the current stable version as I write this is v4.0.21, and 4.x is much better than 3.x at handling Unicode. – Gord Thompson Nov 29 '17 at 21:23
1

I recently saw this error in Heroku. To fix this problem I took the following steps:

  1. Add Apt File to the root folder, with the following: unixodbc unixodbc-dev python-pyodbc libsqliteodbc

  2. Commit that

  3. Run heroku buildpacks:clear

  4. Run heroku buildpacks:add --index 1 heroku-community/apt

  5. Push to Heroku

For me the problem was that I previously installed the buildpack for python, which was not needed. By running heroku buildpacks:clearI removed all un-needed buildpacka, then add back the one I needed. So if you do follow these steps be sure to make note of the build packs you need. To view the buildpacks you have run heroku buildpacks before following these steps.

GitBUB
  • 11
  • 1
0

RedHat/CentOS: dnf install -y unixODBC-devel along with unixODBC installation

Abhijeet Padwal
  • 143
  • 1
  • 6