Python ModuleNotFoundError:将pyodbc导入py脚本时没有名为“pyodbc”的模块
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/52085191/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ModuleNotFoundError: No module named 'pyodbc' when importing pyodbc into py script
提问by Jerry12345678
I've written a short python script which tries to import the pyodbc extension package so I can access my SQL table.
我编写了一个简短的 python 脚本,它尝试导入 pyodbc 扩展包,以便我可以访问我的 SQL 表。
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.
我确实使用以下方法安装了扩展:pip install pyodbc。当我再次安装它时,cmd 说: 要求已经满足:pyodbc in ... 我在我的目录中找到了 pyd 文件。
I have also tried installing pypyodbc, which didn't work.
我也试过安装pypyodbc,但没有用。
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)
(其中第 5 行是 'import pyodbc' 行)
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.
我尝试将 pyodbc.cp37-win_amd64.pyd 文件复制到我的 Python Scripts 文件夹和 pip.exe 文件所在的文件夹中。
- Currently python is my Python37 folder.
- pyodbc.cp37-win_amd64.pyd is in Python > Lib > site-packages.
- 目前 python 是我的 Python37 文件夹。
- pyodbc.cp37-win_amd64.pyd 在 Python > Lib > site-packages 中。
Can anyone help me fix this error please so that I can import pyodbc?
谁能帮我解决这个错误,以便我可以导入pyodbc?
Do all of the python extensions/modules that I install via pip need to be in the same folder/directory as python.exe?
我通过 pip 安装的所有 python 扩展/模块都需要与 python.exe 位于同一文件夹/目录中吗?
回答by t_warsop
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
这里有一个有用的分步指南:https: //docs.microsoft.com/en-us/sql/connect/python/pyodbc/step-1-configure-development-environment-for-pyodbc-python-development?视图=sql-server-2017
For reference, the steps in this guide (windows) are (assuming you already have python installed):
作为参考,本指南(windows)中的步骤是(假设您已经安装了python):
- 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
- Open cmd.exe as an administrator
- Navigate to your python scripts folder containing pip
- Type: pip install pyodbc
- 在 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
回答by A. Nadjar
It seems you have already installed the pyodbc module, but are trying to reference it from another environment.
您似乎已经安装了pyodbc 模块,但正在尝试从另一个环境中引用它。
Some Steps:
一些步骤:
- In the Solution Explorer window right-clickPython Environments
- selectadd/remove
- chooseyour desired python interpreter.
- 在解决方案资源管理器窗口中右键单击Python 环境
- 选择添加/删除
- 选择你想要的 python 解释器。
Refer: How to switch your project python environment to the one which includes pyodbc
回答by TrishaAgrawal
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.
事实证明,我使用的 IDE,JetBrains 的 PyCharm,有一个不同的目录,我必须在其中安装 pyodbc。我使用 cmd 行提示导航到 PyCharm 目录并在那里使用 pip 重新安装。
回答by alexey
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.
尝试在生产服务器上运行我的脚本时,我遇到了完全相同的错误。pip
成功安装了pyodbc
. 它的文件是在位置...\Lib\site-packages
的是python.exe
我(在CMD)上运行。但是python还是找不到。我试过手动重新安装它,但没有任何改变。
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).
然后我注意到 prod 上的 python 版本3.8.1
比 dev 上的新。所以我安装了旧版本3.6.5
的python(在其他目录中),将pyodbc安装到它(像往常一样)。
And what do you think? - Now everything works fine with it!
你觉得怎么样?- 现在一切正常!
回答by Manish Pal
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
a) 您可以为 SQL Server 安装 ODBC 驱动程序。访问:https:
//docs.microsoft.com/en-us/sql/connect/odbc/windows/system-requirements-installation-and-driver-files?view=sql-server-2017#installing-microsoft-odbc-驱动程序为 sql 服务器
b) Uninstall Python and close all your IDLE and install Python again. This worked for me.
b) 卸载 Python 并关闭所有 IDLE 并再次安装 Python。这对我有用。
回答by Manish Pal
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.
你看,问题是 pyodbc 来自一个 .whl(wheel)文件/包。因此,因此,您必须 pip 安装它。
Pip installing is a very tricky process, so please be careful. The steps are:-
Pip 安装是一个非常棘手的过程,所以请小心。步骤是:-
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.
第1步。转到 C:/Python(无论您使用什么版本)/Scripts。向下滚动。如果您看到名为 pip.exe 的文件,则表示您位于正确的文件夹中。复制路径。
Step2. In your computer, search for Environment Variables. You should see an option labeled 'Edit the System Environment Variables'. Click on it.
第2步。在您的计算机中,搜索环境变量。您应该会看到一个标记为“编辑系统环境变量”的选项。点击它。
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!!!
第五步。打开 Windows 文件资源管理器。Shift + 右键单击 pyodbc.whl 文件的安装位置。从下拉菜单中选择“在此处打开命令窗口”。输入“pip install py”,然后单击选项卡,填写完整的文件名。然后,按 Enter,您就可以开始了!现在你不应该再收到错误了!!!