python-3.xpippyodbc

Import "pyodbc" could not be resolved from sourcePylancereportMissingModuleSource


In our python file inside VSCode editor, the import pyodbc declaration gives the following error:

Import "pyodbc" could not be resolved from sourcePylancereportMissingModuleSource

However when I try to install pyobdc, I get the following message:

PS C:\PyCopilotProject\my-python-project> pip install pyodbc
Requirement already satisfied: pyodbc in c:\users\myusename\appdata\local\programs\python\python310\lib\site-packages (4.0.32)

And the following code verifies that the package pyodbc (4.0.32) is indeed installed.

PS C:\PyCopilotProject\my-python-project> pip list

Question: What could be a cause of the error and how we can resolve it?


Solution

  • It depends on what interpreter you're running import pyodbc.

    1. Are you using a virtual environment? If no, it is recommended to use virtual environments to manage your projects. So on Windows, create a virtual environment using:
    python -m venv path\to\venv
    

    Ensure your virtual environment is activated:

    path\to\venv\Scripts\activate
    

    and install pyodbc in the active virtual environment

    pip install pyodbc
    
    1. If working with global installations, it appears that pyodbc is currently installed in your python 3.10 interpreter. Are you also using Python 3.10 to run your code? Do you have multiple python versions?, you might be using a different version than the one that has the package installed.
    2. Ensure you are using the right interpreter on vscode especially if you have multiple python versions.