找不到名为游标的模块
Posted
技术标签:
【中文标题】找不到名为游标的模块【英文标题】:No module found named cursors 【发布时间】:2018-12-09 05:21:14 【问题描述】:我刚刚在我的 VPS 上安装了 Pymysql,安装似乎没有任何问题。
为确保一切正常,我尝试执行我在their documentation. 中找到的以下脚本
Python 2.7.5
和 Python 3.6.5
Server version: 10.1.34-MariaDB MariaDB Server
import pymysql.cursors
# Connect to datebase
con = pymysql.connect(host='localhost',
user='dbuser',
password='dbpass',
db='dbname',
charset='utf8mb4',
cursorclass=pymysql.cursor.DictCursor)
try:
with con.cursor() as cursor:
# Create new record
sql = "INSERT INTO `test` (`idx`, `title`, `description`, `category`) VALUES (%s, %s, %s, %s)"
cursor.execute(sql, (1, 'Fake Title', 'Fake description about a fake post.', 'WELLNESS'))
with con.cursor() as cursor:
# Read a single record
sql = "SELECT `idx`, `title` FROM `test` WHERE `idx`=%s"
cursor.execute(sql, ('1'))
result = cursor.fetchone()
finally:
with open('dbText.txt', 'w') as f:
f.write('%s'.format(result))
con.close()
但是,我收到了一个ImportError
,上面写着No module found named cursors
。
[root@host public_html]# python pymysql.py
Traceback (most recent call last):
File "pymysql.py", line 1, in <module>
import pymysql.cursors
File "/home/bldsprt/public_html/pymysql.py", line 1, in <module>
import pymysql.cursors
ImportError: No module named cursors
[root@host public_html]# python3.6 pymysql.py
Traceback (most recent call last):
File "pymysql.py", line 1, in <module>
import pymysql.cursors
File "/home/bldsprt/public_html/pymysql.py", line 1, in <module>
import pymysql.cursors
ModuleNotFoundError: No module named 'pymysql.cursors'; 'pymysql' is not a package
当我尝试重新安装时,表明pymysql
已经安装。
[root@host public_html]# pip install pymysql
Requirement already satisfied: pymysql in /usr/local/lib/python3.6/site-packages (0.9.0)
Requirement already satisfied: cryptography in /usr/local/lib/python3.6/site-packages (from pymysql) (2.2.2)
Requirement already satisfied: idna>=2.1 in /usr/local/lib/python3.6/site-packages (from cryptography->pymysql) (2.7)
Requirement already satisfied: six>=1.4.1 in /usr/local/lib/python3.6/site-packages (from cryptography->pymysql) (1.11.0)
Requirement already satisfied: cffi>=1.7; platform_python_implementation != "PyPy" in /usr/local/lib/python3.6/site-packages (from cryptography->pymysql) (1.11.5)
Requirement already satisfied: asn1crypto>=0.21.0 in /usr/local/lib/python3.6/site-packages (from cryptography->pymysql) (0.24.0)
Requirement already satisfied: pycparser in /usr/local/lib/python3.6/site-packages (from cffi>=1.7; platform_python_implementation != "PyPy"->cryptography->pymysql) (2.18)
需要单独安装cursors
模块吗?
任何帮助都会很大!
【问题讨论】:
你是怎么安装的? @l'L'l pip install pymysql 奇怪,因为it should install that module along with it;你的好像不见了。 @l'L'l 用更多信息更新了我的问题 尽管您的 pip 显示您正在为 Python 3.6 安装包,但您已将此问题标记为 Python 2.7。您确定您使用的 Python 版本是您认为的那个版本吗?试试Python -V
并报告版本。
【参考方案1】:
堆栈跟踪的这一部分揭示了问题:
File "/home/bldsprt/public_html/pymysql.py", line 1, in <module>
import pymysql.cursors
ImportError: No module named cursors
您已将用于测试的文件命名为“pymysql.py”——因此,Python 尝试从同一个文件中获取游标模块。如果您将文件重命名为其他名称,它应该能够加载正确的库文件,只要通过 pip2/pip2.7 安装 pymysql(似乎您系统上的 pip 默认为 Python 3 安装)。
另外,从包和文档本身来看,我认为当你初始化 MySQL 连接对象(con
)时,它应该使用pymysql.cursors.DictCursor
(游标,而不是游标)作为游标类。
【讨论】:
omg,我在祈祷,希望这是问题所在。这么愚蠢的错误。有趣的是,我刚刚在这里用一段代码帮助了某人,该代码有一个非常相似的问题,哈哈 往往是小事搞砸了!我们谁都无法免疫 :)以上是关于找不到名为游标的模块的主要内容,如果未能解决你的问题,请参考以下文章
cx_Freeze:Python 错误主脚本。找不到模块错误:没有名为 pygments.lexers.python 的模块