16
votes

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?

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

7 Answers

6
votes

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
2
votes

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

0
votes

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

0
votes

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

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

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

0
votes

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.

0
votes

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!

0
votes

The problem is the environnement. You have to execute

.venv\Scripts\activate