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

14

26

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?

Redmund answered 29/8, 2018 at 19:56 Comment(1)
maybe try pip install --upgrade pyodbc (github.com/Microsoft/sql-server-samples/issues/295), or this technique from another similar question Python pip unable to locate pyodbcSpence
G
11

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

Graniteware answered 3/2, 2022 at 10:41 Comment(0)
B
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
Birchard answered 7/12, 2018 at 10:21 Comment(0)
E
5

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
Erastes answered 20/12, 2022 at 13:5 Comment(1)
it worked like a charm. thanks! Mac M1 chip has installation issues.Imago
O
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

Oshinski answered 25/4, 2019 at 4:24 Comment(0)
R
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.

Ragen answered 15/11, 2019 at 17:37 Comment(0)
I
1

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.

Invoice answered 18/10, 2021 at 17:11 Comment(0)
C
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!

Choric answered 7/2, 2020 at 16:20 Comment(0)
A
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

Allmon answered 22/6, 2022 at 16:50 Comment(0)
C
0

In my case i just replace:

#!/usr/bin/env python

with:

#!/usr/bin/python3
Climate answered 8/10, 2022 at 22:56 Comment(0)
H
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

Hach answered 11/3, 2023 at 12:5 Comment(0)
B
0

If the proposed solutions don't work, here's what I did:
I had to specify the path to my virtual environment to do a pip install. I don't know why it didn't work before when I had activated my environment.

CD Path/to/virtual/env
pip install pyodbc
Bract answered 22/4 at 9:0 Comment(0)
I
-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!!!

Intertype answered 29/8, 2018 at 20:57 Comment(2)
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?Redmund
I do not remember what to do when this arises :(sorry)Intertype
S
-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.

Sarnoff answered 11/4, 2019 at 10:40 Comment(0)
N
-1

The problem is the environnement. You have to execute

.venv\Scripts\activate
Nevillenevin answered 1/7, 2021 at 13:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.