Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.7k views
in Technique[技术] by (71.8m points)

python - 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:UsersJerryDocumentsPythonSQLembed.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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...