一、问题描述
尝试使用python链接mysql,把数据进行插入的时候,有报错:
No module named MySQLdb
二、问题修复
1.查询原因:可能缺少mysql客户端
然后:
pip install mysqlclient
报错:
Installing collected packages: mysqlclient
Running setup.py install for mysqlclient ... error
error: subprocess-exited-with-error
× Running setup.py install for mysqlclient did not run successfully.
│ exit code: 1
╰─> [23 lines of output]
running install
running build
running build_py
creating build
creating build\lib.win-amd64-3.7
creating build\lib.win-amd64-3.7\MySQLdb
copying MySQLdb\__init__.py -> build\lib.win-amd64-3.7\MySQLdb
copying MySQLdb\_exceptions.py -> build\lib.win-amd64-3.7\MySQLdb
copying MySQLdb\connections.py -> build\lib.win-amd64-3.7\MySQLdb
copying MySQLdb\converters.py -> build\lib.win-amd64-3.7\MySQLdb
copying MySQLdb\cursors.py -> build\lib.win-amd64-3.7\MySQLdb
note: This error originates from a subprocess, and is likely not a problem with pip.
error: legacy-install-failure
× Encountered error while trying to install package.
╰─> mysqlclient
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-eZL994tC-1668072647882)(tmp.assets/image-20221110170340943.png)]](https://i-blog.csdnimg.cn/blog_migrate/282c9915ba673e3b1109b28db1f4527c.png)
2.解决安装mysqlclient的时候出现Microsoft Visual C++ 14.0 is required报错
copying MySQLdb\constants\__init__.py -> build\lib.win-amd64-3.7\MySQLdb\constants
copying MySQLdb\constants\CLIENT.py -> build\lib.win-amd64-3.7\MySQLdb\constants
copying MySQLdb\constants\CR.py -> build\lib.win-amd64-3.7\MySQLdb\constants
copying MySQLdb\constants\ER.py -> build\lib.win-amd64-3.7\MySQLdb\constants
copying MySQLdb\constants\FIELD_TYPE.py -> build\lib.win-amd64-3.7\MySQLdb\constants
copying MySQLdb\constants\FLAG.py -> build\lib.win-amd64-3.7\MySQLdb\constants
running build_ext
building 'MySQLdb._mysql' extension
error: Microsoft Visual C++ 14.0 is required. Get it with "Microsoft Visual C++ Build Tools": https://visualstudio.microsoft.com/downloads/
[end of output]
note: This error originates from a subprocess, and is likely not a problem with pip.
error: legacy-install-failure
× Encountered error while trying to install package.
╰─> mysqlclient

3.mysqlclient安装报错----error: Microsoft Visual C++ 14.0 is required。解决办法
https://codeantenna.com/a/n3yCa0VDpB,下载对应python版本
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-lIa00YTG-1668072647884)(tmp.assets/image-20221110172850567.png)]](https://i-blog.csdnimg.cn/blog_migrate/47cf63eef2df106195c60b797d1f5c4a.png)
4.然后直接安装
pip install C:\tmp\mysqlclient-1.4.6-cp37-cp37m-win32.whl
三、解决办法二【简单推荐!】
直接引入pymysql,虽然第一次引入,但是缺少初始化信息,需要整体引用
import pymysql
pymysql.install_as_MySQLdb()
添加初始化后,就不会报错缺少mysqldb module了。
在尝试使用Python连接MySQL并插入数据时遇到`NomodulenamedMySQLdb`错误。问题源于缺少mysqlclient模块。安装mysqlclient过程中报错,提示需要MicrosoftVisualC++14.0。解决方案包括下载相应版本的VC++ Build Tools或使用pymysql作为替代,通过`pymysql.install_as_MySQLdb()`初始化避免缺少模块的问题。
866

被折叠的 条评论
为什么被折叠?



