Skip to content

Building pyodbc from source

Keith Erskine edited this page Oct 30, 2018 · 15 revisions

Overview

Obtaining Source

The source for released versions are provided in zip files on the front page of this repository.

If you are going to work on pyodbc (changes are welcome!), best fork the repository from github so I can easily pull changes from your fork.

If you want an unreleased version from github but don't have git installed, choose the branch and commit you want and use the download button. It will offer you a tar or zip file.

Compiling

pyodbc is built using distutils, which comes with Python. If you have the appropriate C++ compiler installed (see below), run the following in the pyodbc source directory:

python setup.py build

To install after a successful build:

python setup.py install

Version

pyodbc uses git tags in the format major.minor.patch for versioning. If the commit being built has a tag, such as "3.0.7", then that is used for the version. If the commit is not tagged, then it is assumed that the commit is a beta for an upcoming release. The patch number is incremented, such as "3.0.7", and the number of commits since the previous release is appended: 3.0.7-beta3.

If you are building from inside a git repository, the build script will automatically determine the correct version from the tags (using git describe).

If you downloaded the source as a zip from the front page, the version will be in the text file PGK-INFO. The build script will read the version from that text file.

If the version cannot be determined for some reason, you will see a warning about this and the script will build version 2.1.0. Do not distribute this version since you won't be able to track its actual version!

Operating Systems

Windows

First of all, upgrade your Python setuptools module to the latest with pip install --upgrade setuptools.

To compile pyodbc, you must use the appropriate Microsoft Visual C++ compiler for the version of Python you wish to compile. See the following wiki page for reference: https://wiki.python.org/moin/WindowsCompilers

  • To build Python 2.4 or 2.5 versions, you will need the Visual Studio 2003 .NET compiler. Unfortunately there is no free version of this.
  • For Python 2.6, 2.7, 3.0, 3.1 and 3.2, use the Visual C++ 2008 compiler. There is a free version of this, Visual C++ 2008 Express.
  • For Python 3.3 and 3.4, use the Visual C++ 2010 compiler. There is a free version of this, Visual C++ 2010 Express, although that version does not inherently allow 64-bit builds to be compiled. To compile 64-bit builds with Visual C++ 2010 Express, follow these instructions: http://blog.ionelmc.ro/2014/12/21/compiling-python-extensions-on-windows/
  • For Python 3.5 and 3.6, use Visual C++ 2014, VC 2017 works too.
  • For Python 3.7, use Visual C++ 2017.

These instructions assume Windows 10. If you don't already have Visual Studio 2017 installed, install Build Tools for Visual Studio 2017 by downloading from https://visualstudio.microsoft.com/downloads/#build-tools-for-visual-studio-2017 and installing it. If you need to check or modify this installation, do so by opening the "Visual Studio Installer" app from the Start menu. In that app under the "Products" tab, you should see "Visual Studio Build Tools 2017". Click "Modify" under "Visual Studio Build Tools 2017". In the new window, under the "Workloads" tab, check the small checkbox on "Visual C++ build tools". On the right you'll see the "Installation details", which should now include the optional elements: "Windows 10 SDK"; "Visual C++ tools for CMake"; and "Testing tools core features - Build Tools".

To build pyodbc, from the Start menu within the "Visual Studio 2017" folder, open up an "x64 Native Tools Command Prompt for VS 2017" command prompt (NOT an ordinary command prompt). Make sure you open it with administrative privileges by right-clicking on the icon, choosing "More" then "Run as administrator".

Within that command prompt, cd to the top-level pyodbc directory, the one that includes setup.py, then:

  1. Run python setup.py build, which creates and populates the build subdirectory.
  2. Run python setup.py bdist_wininst, which creates and populates the dist subdirectory.
  3. Run python setup.py install, which installs your new pyodbc build into your Python environment. This step requires admin privileges.

Check your new version of pyodbc by running python -c "import pyodbc; print(pyodbc.version)".

Other

To build on other operating systems, use the gcc compiler.

On Linux, pyodbc is typically built using the unixODBC headers, so you will need unixODBC and its headers installed. On a RedHat/CentOS/Fedora box, this means you would need to install unixODBC-devel:

yum install unixODBC-devel

On Fedora, you may see this error: gcc: error: /usr/lib/rpm/redhat/redhat-hardened-cc1: No such file or directory. You'll need to install the redhat-rpm-config package.

sudo dnf install redhat-rpm-config
Clone this wiki locally