问题描述
我编写了一个简短的 python 脚本,尝试导入 pyodbc 扩展包,以便我可以访问我的 SQL 表.
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;')
我确实使用以下命令安装了扩展:pip install pyodbc.当我再次去安装它时,cmd 说: 要求已经满足:pyodbc in ... 我在我的目录中找到了 pyd 文件.
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.
我也试过安装pypyodbc,但没用.
I have also tried installing pypyodbc, which didn't work.
我得到的错误是:
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'
(其中第 5 行是 'import pyodbc' 行)
(where line 5 is the 'import pyodbc' line)
我尝试将 pyodbc.cp37-win_amd64.pyd 文件复制到我的 Python Scripts 文件夹和 pip.exe 文件所在的文件夹中.
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.
- 目前 python 是我的 Python37 文件夹.
- pyodbc.cp37-win_amd64.pyd 在 Python > Lib > 站点包中.
谁能帮我修复这个错误,以便我可以导入 pyodbc?
Can anyone help me fix this error please so that I can import pyodbc?
我通过 pip 安装的所有 python 扩展/模块都需要与 python.exe 位于同一文件夹/目录中吗?
Do all of the python extensions/modules that I install via pip need to be in the same folder/directory as python.exe?
推荐答案
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
作为参考,本指南(Windows)中的步骤是(假设您已经安装了python):
For reference, the steps in this guide (windows) are (assuming you already have python installed):
- 在 Windows 上安装适用于 SQL Server 的 Microsoft ODBC 驱动程序,来自 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
- 以管理员身份打开 cmd.exe
- 导航到包含 pip 的 python 脚本文件夹
- 类型:pip install pyodbc
这篇关于ModuleNotFoundError:将pyodbc导入py脚本时没有名为“pyodbc"的模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!