Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ModuleNotFoundError: No module named 'pyodbc' when importing pyodbc into py script

I've written a short python script which tries to import the pyodbc extension package so I can access my SQL table.

import pyodbc as pyodbc
cnxn = pyodbc.connect('Driver={SQL Server};'
                      'Server=DESKTOP-UO8KJOP;'
                      'Database=ExamplePFData'
                      'Trusted_Connection=yes;')

I have definitely installed the extension by using: pip install pyodbc. And when I go to install it again, cmd says: Requirement already satisfied: pyodbc in ... and I've found the pyd file in my directories.

I have also tried installing pypyodbc, which didn't work.

The error I get is:

Traceback (most recent call last):
File "C:\Users\Jerry\Documents\Python\SQLembed.py", line 5, in <module>
import pyodbc as pyodbc
ModuleNotFoundError: No module named 'pyodbc'

(where line 5 is the 'import pyodbc' line)

I have tried copying the pyodbc.cp37-win_amd64.pyd file into my Python Scripts folder and into the folder where my pip.exe file is.

  • Currently python is my Python37 folder.
  • pyodbc.cp37-win_amd64.pyd is in Python > Lib > site-packages.

Can anyone help me fix this error please so that I can import pyodbc?

Do all of the python extensions/modules that I install via pip need to be in the same folder/directory as python.exe?

like image 577
Jerry12345678 Avatar asked Aug 29 '18 19:08

Jerry12345678


People also ask

How do I know if Pyodbc is installed?

You can use the subprocess module to execute pip list to get a list of the installed modules and check if it contains the pyodbc module.

What is Pyodbc module in Python?

pyodbc is an open source Python module that makes accessing ODBC databases simple. It implements the DB API 2.0 specification but is packed with even more Pythonic convenience. The easiest way to install is to use pip: pip install pyodbc.


4 Answers

There is a useful step by step guide here: https://docs.microsoft.com/en-us/sql/connect/python/pyodbc/step-1-configure-development-environment-for-pyodbc-python-development?view=sql-server-2017

For reference, the steps in this guide (windows) are (assuming you already have python installed):

  1. Install the Microsoft ODBC Driver for SQL Server on Windows, from https://docs.microsoft.com/en-us/sql/connect/odbc/windows/system-requirements-installation-and-driver-files?view=sql-server-2017#installing-microsoft-odbc-driver-for-sql-server
  2. Open cmd.exe as an administrator
  3. Navigate to your python scripts folder containing pip
  4. Type: pip install pyodbc
like image 128
t_warsop Avatar answered Sep 21 '22 07:09

t_warsop


It seems you have already installed the pyodbc module, but are trying to reference it from another environment.

Some Steps:

  1. In the Solution Explorer window right-click Python Environments
  2. select add/remove
  3. choose your desired python interpreter.

Refer: How to switch your project python environment to the one which includes pyodbc

like image 33
A. Nadjar Avatar answered Sep 19 '22 07:09

A. Nadjar


Just Uninstall and reinstall the pyodbc to solve your issue

it worked for me.

use pip uninstall pyodb and confirm with Y to uninstall and then reinstall using pip install pyodbc

like image 30
Mohd Ajaz Uddin Avatar answered Sep 19 '22 07:09

Mohd Ajaz Uddin


I had the same problem.

import sys

print(sys.path)

It turned out that the IDE I was using, PyCharm by JetBrains, had a different directory in which I had to install pyodbc. I used cmd line prompt to navigate to the PyCharm directory and reinstalled with pip there.

like image 34
TrishaAgrawal Avatar answered Sep 21 '22 07:09

TrishaAgrawal