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

Viewed 94985

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?

10 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

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

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.

I ran into the same error a few days ago! Thankfully, I found the answer.

You see, the problem is that pyodbc comes in a .whl (wheel) file/package. So, as a result, you have to pip install it.

Pip installing is a very tricky process, so please be careful. The steps are:-

Step1. Go to C:/Python (whatever version you are using)/Scripts. Scroll down. If you see a file named pip.exe, then that means that you are in the right folder. Copy the path.

Step2. In your computer, search for Environment Variables. You should see an option labeled 'Edit the System Environment Variables'. Click on it.

Step3. There, you should see a dialogue box appear. Click 'Environment Variables'. Click on 'Path'. Then, click 'New'. Paste the path that you copied earlier.

Step4. Click 'Ok'.

Step5. Open the Windows File Explorer. Shift + Right Click wherever your pyodbc.whl file is installed. Select 'Open Command Window Here' from the dropdown menu. Type in 'pip install py', then click tab and the full file name should fill in. Then, press Enter, and you're ready to go! Now you shouldn't get the error again!!!

I had absolutely the same error when tried to run my script on production server. pip successfully installed the pyodbc. its files were in location ...\Lib\site-packages of that python.exe which I run (in cmd). But python still couldn't find it. I've tried manually reinstall it, but nothing was changed.

Then I've noticed that python version on prod is newer 3.8.1 than it was on dev. So I installed an older version 3.6.5 of python (in other dir), install pyodbc to it (as usual).

And what do you think? - Now everything works fine with it!

The problem is the environnement. You have to execute

.venv\Scripts\activate

A little late perhaps but I had the same issue (i'm using conda for python envs) and did the following to solve this:

cd C:\Users\{user}\miniconda3\envs\{env_name}
pip list

Verify in the output if pyodbc is installed. If not run (from the environment folder) the following:

pip install -r C:\dev\repos\{repo-name}\requirements.txt

Check pip list again and now pyodbc should be there.

In case of this error you can just follow the installation documentation on this link: https://github.com/mkleehammer/pyodbc/wiki/Install. In my case, I had the same issue on MacOSX and I just installed unixodbc with brew: brew install unixodbc and after that the import was working without any problem

Related