24

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?

Jerry12345678
  • 251
  • 1
  • 2
  • 6
  • 4
    maybe try `pip install --upgrade pyodbc` (https://github.com/Microsoft/sql-server-samples/issues/295), or this technique from another similar question [Python pip unable to locate pyodbc](https://stackoverflow.com/a/21055195/1248974) – chickity china chinese chicken Aug 29 '18 at 22:24

13 Answers13

10

There is a useful step by step guide here: https://learn.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://learn.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
t_warsop
  • 1,170
  • 2
  • 24
  • 38
7

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

Shunya
  • 2,344
  • 4
  • 16
  • 28
4

For Mac users try this,

I faced the same issue on Mac M1 chip with pyodbc library, I resolved this and it worked for me, hope it will helps you as well.

Uninstall pyodbc library first. Run it with a --no-binary option which forces pip to compile the module from source instead of installing from pre compiled wheel.

#Package name pyodbc

pip uninstall pyodbc
pip install --no-binary :all: pyodbc
Anil Kumar
  • 385
  • 2
  • 17
Mahesh
  • 137
  • 2
  • 8
2

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

Dale K
  • 25,246
  • 15
  • 42
  • 71
A. Nadjar
  • 2,440
  • 2
  • 19
  • 20
1

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.

Community
  • 1
  • 1
0

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!

Gautam
  • 1,754
  • 1
  • 14
  • 22
alexey
  • 1
  • 2
0

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.

Fjurg
  • 487
  • 3
  • 10
0

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

Luigi Bungaro
  • 61
  • 1
  • 4
0

In my case i just replace:

#!/usr/bin/env python

with:

#!/usr/bin/python3
Daniel
  • 1
0

If you're using Visual Studio as the IDE.You can do the following:

  1. Expand Python Environments in your project
  2. Right click the environment e.g. Python 3.7 (64-bit) (global default)
  3. Choose Manage Python Packages...
  4. Type and install pyodbc

enter image description here

Denys Wessels
  • 16,829
  • 14
  • 80
  • 120
-1

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!!!

  • On step 5. In this directory (which is were my pyodbc file is): C:\Users\Jerry\AppData\Local\Programs\Python\Python37\Lib\site-packages I ran the "pip install pyodbc.cp37-win_amd64.pyd" command. It returned the following error: Could not find a version that satisfies the requirement pyodbc.cp37-win_amd64.pyd (from versions: ) No matching distribution found for pyodbc.cp37-win_amd64.pyd. Any idea why? – Jerry12345678 Aug 29 '18 at 21:24
  • 1
    I do not remember what to do when this arises :(sorry) –  Sep 10 '18 at 16:58
-1

I also faced the same issue. You can try the following method :

a) You can install the ODBC Driver for SQL Server. Visit:
https://learn.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

b) Uninstall Python and close all your IDLE and install Python again. This worked for me.

Pavel Smirnov
  • 4,611
  • 3
  • 18
  • 28
Manish Pal
  • 292
  • 2
  • 6
  • 20
-1

The problem is the environnement. You have to execute

.venv\Scripts\activate
Sven Eberth
  • 3,057
  • 12
  • 24
  • 29