Skip to content

Building pyodbc from source

Keith Erskine edited this page Jul 23, 2023 · 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

If you have the appropriate C++ compiler installed (see below), run the following in the pyodbc top-level directory (i.e. where setup.py lives):

# generally best to use the latest pip and setuptools
python -m pip install --upgrade pip setuptools
# install pyodbc using the contents of the current directory (".")
python -m pip install --force-reinstall .

These days, the legacy install command python setup.py install should not typically be used. There is a good article on why not here.

If you wish to build the install files first before installing them in the site-packages directory, use the build module. There are many options when building so see here for the documentation:

python -m pip install --upgrade build wheel
python -m build .
python -m pip install --force-reinstall <path-to-the-wheel-in-the-dist-folder>

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

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 (version 9). There is a free version of this, Visual C++ 2008 Express.
  • For Python 3.3 and 3.4, use the Visual C++ 2010 compiler (version 10). 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+, use the Visual C++ 2019 compiler (version 16).

On Windows 10/11, you shouldn't typically need to install a compiler, you should already have Visual Studio 2019 installed on your PC. If not though, install Build Tools for Visual Studio 2019 by logging into https://my.visualstudio.com/ (create a Microsoft account if necessary), searching for "Build Tools for Visual Studio 2019", downloading and running the executable. This will initially install Visual Studio Installer if you don't already have it. Eventually you should see a window with a "Workloads" tab highlighted. (If you see "Visual Studio Build Tools 2019" as a box within an "Installed" tab, click the "Modify" button.) In the "Desktop development with C++" section, click the checkbox in the top right of that box. On the right, make sure "MSVC v142 - VS 2019 C++ x64/x86 build tools" is checked and at least one "Windows 10 SDK" is also checked, then click Install. To build pyodbc, you may also need to compile from a specific Windows VS 2019 prompt. In this case, go to the Start menu, open the "Visual Studio 2019" folder, then open up an "x64 Native Tools Command Prompt for VS 2019" command prompt (or "x86" for a 32-bit compilation, but NOT an ordinary command prompt). If you need to open the command prompt with admin privileges (see below), do this by right-clicking on the icon, choosing "More" then "Run as administrator".

Install pyodbc from a command prompt using the commands in the "Compiling" section above. Check the new version of pyodbc by running:

python -c "import pyodbc; print(pyodbc.version)".

macOS

You'll need the ODBC header files from unixODBC:

brew install unixodbc

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 Ubuntu and Debian based systems: sudo apt install python3-dev unixodbc-dev

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