1
votes

I am trying to connect my python code to sql server. But there is an error which shows: import pyodbc ModuleNotFoundError: No module named 'pyodbc'

import pyodbc
cnxn = pyodbc.connect("Driver={SQL Server};"
                  "Server=localhost;"
                  "Database=SCMS2;"
                  "uid=sa;pwd=tazbirul94")

cursor = cnxn.cursor()
cursor.execute('SELECT * FROM Suppliers')

for row in cursor:
    print('row = %r' % (row,))

Here is the pip freeze command:

This shows my odbc is present: Here is the error Here is my pycharm environment path

1

1 Answers

0
votes

The pycharm is using a python from a .../venv/... folder while what you type in the cmd does not. Make sure to use the same python version for both.

One way would be to go to:

File->Settings->Project->Project Interpreter and change to the python that you have installed the module for