Last modified: Apr 07, 2025 By Alexander Williams

Install Pyodbc in Python Step by Step

Pyodbc is a Python library for connecting to databases using ODBC. It is widely used for database operations. This guide will help you install it quickly.

Prerequisites

Before installing Pyodbc, ensure you have Python installed. You can check this by running python --version in your terminal.


python --version

If Python is not installed, download it from the official website. Also, ensure pip is installed. Pip is Python's package manager.

Install Pyodbc Using Pip

The easiest way to install Pyodbc is using pip. Open your terminal or command prompt and run the following command.


pip install pyodbc

This will download and install Pyodbc along with its dependencies. Wait for the installation to complete.

Verify the Installation

After installation, verify Pyodbc is installed correctly. Open a Python shell and import Pyodbc.

 
import pyodbc
print(pyodbc.version)

This should print the installed Pyodbc version. If you encounter ModuleNotFoundError, check our guide on How To Solve ModuleNotFoundError.

Install Pyodbc on Linux

On Linux, you may need to install additional dependencies. Run the following commands before installing Pyodbc.


sudo apt-get update
sudo apt-get install unixodbc-dev

After installing dependencies, use pip to install Pyodbc as shown earlier.

Install Pyodbc on macOS

For macOS, use Homebrew to install unixODBC before installing Pyodbc.


brew install unixodbc
pip install pyodbc

This ensures all required libraries are available for Pyodbc to work correctly.

Basic Usage of Pyodbc

Once installed, you can use Pyodbc to connect to a database. Here is a simple example.

 
import pyodbc

# Replace with your connection details
conn = pyodbc.connect('DRIVER={SQL Server};SERVER=localhost;DATABASE=testdb;UID=user;PWD=password')

cursor = conn.cursor()
cursor.execute("SELECT * FROM employees")

for row in cursor:
    print(row)

This code connects to a SQL Server database and fetches data from the employees table.

Troubleshooting Common Issues

If you face issues, ensure the ODBC driver is installed. For SQL Server, download the driver from Microsoft's website.

Another common issue is incorrect connection strings. Double-check your connection details.

Conclusion

Installing Pyodbc in Python is straightforward. Follow the steps above to set it up on any OS. Pyodbc makes database operations easy and efficient.

For more Python tips, explore our other guides. Happy coding!