Welcome to the development build guide for the mssql-python project!
This guide will help you set up your environment, build the native bindings, and package the project as a Python wheel.
- Getting Started
- Prerequisites
- Platform-Specific Setup
- Building Native Bindings
- Building the Python Wheel (.whl)
- Running Tests
- Setting Up a Test Database (Optional)
- Directory Structure
- Troubleshooting
To contribute to this project, you'll need to fork and clone the repository. You can do this using either the command line or Visual Studio Code.
- Fork the repository on GitHub by clicking the "Fork" button on the mssql-python repository page.
- Clone your fork to your local machine:
git clone https://github.com/YOUR-USERNAME/mssql-python.git cd mssql-python - Set up the upstream remote to keep your fork in sync:
git remote add upstream https://github.com/microsoft/mssql-python.git
- Install the GitHub extension in VS Code:
- Open VS Code
- Go to the Extensions view (Ctrl+Shift+X)
- Search for "GitHub Pull Requests and Issues" and install it
- Fork and clone the repository:
- Navigate to the mssql-python repository page
- Click "Create new fork" to create a fork in your GitHub account
- Open VS Code
- Open the Command Palette (Ctrl+Shift+P)
- Type "Git: Clone" and select it
- Select "Clone from GitHub"
- Search for and select your forked repository
- Choose a local directory to clone to
- The upstream remote will be set up automatically when you fork through GitHub.
- Python: Minimum supported version is 3.10.
Ensurepythonandpipcommands refer to your Python 3.10+ installation. - pybind11: Used for C++/Python bindings.
- CMake: For Unix and macOS builds.
- Microsoft ODBC Driver: For packaging driver dependencies and header files such as
sql.h,sqlext.hetc. - setuptools, wheel, pytest: For building and testing (
pip install setuptools wheel pytest).
- Install Python (3.10+ from python.org).
- Install Visual Studio Build Tools
- Include the “Desktop development with C++” workload.
- CMake is included by default.
- Alternative for VS Code users: If you already have VS Code installed, you can configure it for C++ development by following this guide.
- Install Microsoft ODBC Driver for SQL Server:
Download here. - Install required Python packages:
# Will install pybind11, setuptools etc. pip install -r requirements.txt
- Install Python (3.10+ from python.org).
- Install CMake:
brew install cmake
- Install Microsoft ODBC Driver for SQL Server:
- Follow official instructions.
- Install Python requirements:
# Will install pybind11, setuptools etc. pip install -r requirements.txt
- Install Python and development tools:
Ensure
sudo apt-get update sudo apt-get install python3 python3-dev python3-pip build-essential cmake
pythonandpiprefer to Python 3.10+. - Install Microsoft ODBC Driver for SQL Server:
- Follow official instructions.
- Install Python packages:
# Will install pybind11, setuptools etc. pip install -r requirements.txt
The native bindings are in the mssql_python/pybind directory.
Open a Developer Command Prompt for VS and run:
cd mssql_python/pybind
build.batThis will:
- Clean previous builds
- Configure with CMake
- Build the extension
- Copy the generated
.pydfile to the correct location
cd mssql_python/pybind
./build.shThis will:
- Clean previous builds
- Configure with CMake
- Build the extension
- Copy the generated
.sofile to the correct location
Tests require a database connection string and must be run from the project root directory.
Set the DB_CONNECTION_STRING environment variable before running tests:
Tip
If you are using VS Code you may have to close all open instances of the IDE for the new environment variable to be visible. If you are still getting errors about invalid connection strings, you may need to reboot your system.
# If you're in mssql_python/pybind/, navigate back to the project root:
cd ../..
set DB_CONNECTION_STRING=your-connection-string-here
python -m pytest -v# If you're in mssql_python/pybind/, navigate back to the project root:
cd ../..
export DB_CONNECTION_STRING=your-connection-string-here
python -m pytest -vThe wheel includes the native bindings.
You must build the native bindings first (see above).
From the project root:
python setup.py bdist_wheelThe wheel file will be created in the dist/ directory.
From the project root:
# Build the bindings first!
cd mssql_python/pybind
./build.sh
cd ../..
# Then build the wheel:
python setup.py bdist_wheelThe wheel file will be created in the dist/ directory.
mssql_python/pybind/— Native C++/pybind11 bindings and platform build scriptsmssql_python/— Python package sourcetests/— Test suitedist/— Built wheel packages
If you don't have access to a SQL Server instance, you can quickly set up a containerized SQL Server using go-sqlcmd:
# Install Docker Desktop and sqlcmd
winget install Docker.DockerDesktopConfigure Docker, accept EULA, etc., then open a new terminal window:
winget install sqlcmdOpen a new window to get new path variables:
sqlcmd create mssql --name mssql-python --accept-eula tag 2025-latest --using https://github.com/Microsoft/sql-server-samples/releases/download/wide-world-importers-v1.0/WideWorldImporters-Full.bak
sqlcmd config connection-stringsCopy the ODBC connection string and remove the driver clause before storing it in your DB_CONNECTION_STRING environment variable.
# Install Docker Desktop and sqlcmd
brew install --cask docker
brew install sqlcmdStart Docker Desktop, then:
sqlcmd create mssql --name mssql-python --accept-eula tag 2025-latest --using https://github.com/Microsoft/sql-server-samples/releases/download/wide-world-importers-v1.0/WideWorldImporters-Full.bak
sqlcmd config connection-stringsCopy the ODBC connection string and remove the driver clause before storing it in your DB_CONNECTION_STRING environment variable.
# Install Docker and sqlcmd (Ubuntu/Debian)
sudo apt-get update
sudo apt-get install docker.io
sudo systemctl start docker
sudo systemctl enable docker
# Install sqlcmd
curl -fsSL https://packages.microsoft.com/keys/microsoft.asc | sudo gpg --dearmor -o /usr/share/keyrings/microsoft-prod.gpg
echo "deb [arch=amd64,arm64,armhf signed-by=/usr/share/keyrings/microsoft-prod.gpg] https://packages.microsoft.com/repos/microsoft-ubuntu-$(lsb_release -rs)-prod $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/mssql-release.list
sudo apt-get update
sudo apt-get install sqlcmdThen create the SQL Server container:
sudo sqlcmd create mssql --name mssql-python --accept-eula tag 2025-latest --using https://github.com/Microsoft/sql-server-samples/releases/download/wide-world-importers-v1.0/WideWorldImporters-Full.bak
sqlcmd config connection-stringsCopy the ODBC connection string and remove the driver clause before storing it in your DB_CONNECTION_STRING environment variable.
- Ensure all prerequisites are installed and on your PATH.
- If a build fails, clean up old artifacts and try again (
mssql_python/pybind/build.bat cleanor./build.sh clean). - For wheel issues, ensure the native binding (
.pydor.so) is present in the expected location before building the wheel. - For test failures, double-check your
DB_CONNECTION_STRING.
For more details on the native bindings, see mssql_python/pybind/README.md.
Happy coding!