Skip to main content

How to Solve "ModuleNotFoundError: No module named 'pyodbc'" in Python

The error ModuleNotFoundError: No module named 'pyodbc' in Python indicates that the pyodbc library, which is used for connecting to databases via ODBC (Open Database Connectivity), is not installed in your current Python environment, or can not be found.

This guide explains how to install pyodbc correctly, troubleshoot common installation problems, and configure your IDE/Jupyter Notebook.

Installing pyodbc

The standard way to install pyodbc is using pip:

pip install pyodbc
  • Multiple Python Versions: If needed, use pip3 or a specific version (e.g., pip3.9, pip3.10).

    pip3 install pyodbc
  • Permissions Errors:

    sudo pip3 install pyodbc  # Linux/macOS (use with extreme caution!)
    pip install pyodbc --user # Install for current user only
    note

    Using virtual environments (see next section) is the best way to avoid permission issues.

  • python -m pip: If pip isn't directly accessible, use:

    python -m pip install pyodbc # Or, python3 -m pip, or py -m pip
  • Anaconda: If you're using Anaconda, use the following command:

    conda install -c anaconda pyodbc

Verifying the Installation

After installation, check if pyodbc is correctly installed and accessible by running:

import pyodbc

# If no error, the installation was successful.
# You can add a simple test connection here if you have a database set up.
# For example:
# try:
# cnxn = pyodbc.connect("your_connection_string")
# print("Connection successful!")
# cnxn.close()
# except pyodbc.Error as ex:
# sqlstate = ex.args[0]
# print("Error connecting:", sqlstate)

Troubleshooting ModuleNotFoundError

Virtual Environments (Essential)

Always use virtual environments. They isolate project dependencies and prevent conflicts:

  1. Create: python3 -m venv venv (or python -m venv venv, or py -m venv venv)

  2. Activate:

    • Linux/macOS: source venv/bin/activate
    • Windows (cmd): venv\Scripts\activate.bat
    • Windows (PowerShell): venv\Scripts\Activate.ps1
      • If you get errors in powershell, run the following command and try again:
        Set-ExecutionPolicy RemoteSigned -Scope CurrentUser
  3. Install: pip install pyodbc (inside the activated environment).

Your terminal prompt should change (e.g., (venv) $).

Multiple Python Versions:

  • Check: python --version (or python3 --version)
  • Correct pip: Use the pip that corresponds to your intended Python version.

IDE Configuration (VS Code, PyCharm)

Configure your IDE to use the correct interpreter/environment.

Jupyter Notebook

Install within a notebook cell:

!pip install pyodbc

Ensure your Jupyter kernel uses the right environment.

Naming Conflicts

Never name your files pyodbc.py.

Reinstalling pyodbc

pip uninstall pyodbc
pip install pyodbc

Installation Instructions for Specific Environments

Windows:

  • Open Command Prompt or PowerShell.
  • Use pip install pyodbc (or py -m pip install pyodbc).

macOS / Linux:

  • Open your Terminal
  • Use pip3 install pyodbc. You might also try commands like: sudo apt-get install python3-pyodbc, sudo yum install python3-pyodbc, sudo zypper install python3-pyodbc

Anaconda:

  • Use conda install -c anaconda pyodbc

Jupyter Notebook (within a cell):

!pip install pyodbc

Handling "Import 'pyodbc' could not be resolved" (Pylance - VS Code)

  • Select correct interpreter.
  • Restart VS Code.
  • You can disable the warning by adding # type: ignore next to the import statement.

Conclusion

The ModuleNotFoundError: No module named 'pyodbc' error usually indicates a simple installation or environment problem. The key steps are:

  • Use a virtual environment.
  • Install pyodbc using pip within the activated environment.
  • Ensure your IDE/Jupyter is configured to use that environment.

By following these guidelines, you can connect to your databases using pyodbc without issues.