แก้ไข

Install mssql-python

The mssql-python driver is Microsoft's official Python driver for SQL Server, Azure SQL Database, Azure SQL Managed Instance, and SQL database in Microsoft Fabric. Install the driver on Windows, Linux, or macOS by using pip or conda.

Prerequisites

  • The driver requires Python 3.10 or later and doesn't support earlier Python versions.
  • To install with pip, the pip package manager, which is included with Python.
  • To install with conda, the conda package manager.

Important

The mssql-python driver uses DDBC (Direct Database Connectivity), which bundles and manages the ODBC layer internally. You don't need to install the Microsoft ODBC Driver for SQL Server separately.

Install from PyPI

Install the driver using pip:

pip install mssql-python

To upgrade an existing installation:

pip install --upgrade mssql-python

To install a specific version:

pip install mssql-python==1.16.0

The pip install mssql-python command also installs two companion packages that the driver requires at runtime:

  • mssql-python-odbc ships the Microsoft ODBC Driver 18 binaries.
  • mssql-python-rs ships the Rust runtime that powers bulk copy and the opt-in mssql-odbc provider.

Starting with version 1.16.0, mssql-python pins exact versions of both packages. Earlier versions don't require mssql-python-rs. For the pins by version, see Dependency requirements.

Install from conda

The driver is also available on the microsoft channel on Anaconda.org:

conda install -c microsoft mssql-python

The conda package is a self-contained package. It includes the Python bindings, the ODBC driver binaries, and the Rust runtime, so conda installs one package instead of three. Running pip list in the environment still shows mssql-python-odbc and mssql-python-rs, because the conda package includes their package metadata. Conda selects the build that matches your platform and Python version, and manages upgrades:

conda update -c microsoft mssql-python

Install the driver with either conda or pip in a given environment, not both.

The conda package has these platform requirements:

Platform Python versions Requirement
Windows x64 3.10 through 3.15 None.
Windows ARM64 3.12 through 3.15 None.
Linux x64, Linux ARM64 3.10 through 3.15 glibc 2.34 and later versions. Use pip on Red Hat Enterprise Linux (RHEL) 8, Alpine Linux, and other distributions with an older glibc or with musl.
macOS Intel, macOS Apple Silicon 3.10 through 3.15 macOS 15 and later versions. Encrypted connections require OpenSSL installed on the system, as described in macOS. The driver doesn't load OpenSSL from the conda environment.

Verify the installation

After installation, verify the driver is working:

import mssql_python

print(f"mssql-python version: {mssql_python.__version__}")
print(f"DB-API level: {mssql_python.apilevel}")
print(f"Thread safety: {mssql_python.threadsafety}")
print(f"Parameter style: {mssql_python.paramstyle}")

Expected output:

mssql-python version: 1.16.0
DB-API level: 2.0
Thread safety: 1
Parameter style: pyformat

Platform-specific notes

Windows

The driver ships as a prebuilt wheel that includes all necessary components.

Linux

The driver requires certain system libraries for Kerberos authentication and dynamic loading. Install them using your distribution's package manager:

Ubuntu/Debian:

sudo apt-get update
sudo apt-get install libltdl7 libkrb5-3 libgssapi-krb5-2

Red Hat/CentOS/Fedora:

sudo dnf install libtool-ltdl krb5-libs

Note

On RHEL 8 and other glibc 2.28 compatible distributions (AlmaLinux 8, Rocky Linux 8), the driver installs from the standard manylinux_2_28 wheel published to PyPI.

Alpine Linux:

apk add libltdl krb5-libs libstdc++

macOS

The driver ships as a universal2 fat binary that runs natively on both Apple Silicon (arm64) and Intel (x86_64) Macs. You don't need a separate unixODBC install.

The driver requires OpenSSL. Install it by using Homebrew:

brew install openssl

If you encounter SSL-related errors, ensure OpenSSL is properly linked:

export LDFLAGS="-L/opt/homebrew/opt/openssl/lib"
export CPPFLAGS="-I/opt/homebrew/opt/openssl/include"

Virtual environments

To avoid conflicts, install Python packages in a virtual environment:

# Create a virtual environment
python -m venv .venv

# Activate it (Windows)
.venv\Scripts\activate

# Activate it (Linux/macOS)
source .venv/bin/activate

# Install the driver
pip install mssql-python

Development installation

To install the driver with development dependencies for contributing to the driver:

git clone https://github.com/microsoft/mssql-python.git
cd mssql-python
pip install -e ".[dev]"

Troubleshoot installation issues

pip install fails with "no matching distribution"

Ensure you're using Python 3.10 or later:

python --version

If you're using an older Python version, upgrade Python or use pyenv to manage multiple versions.

Import error after installation

If you see ModuleNotFoundError: No module named 'mssql_python', verify you're using the same Python environment where you installed the package.

pip show mssql-python

Permission errors on Linux/macOS

If you encounter permission errors, avoid using sudo pip. Instead, use a virtual environment or the --user flag.

pip install --user mssql-python