一、问题描述
尝试使用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
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版本
4.然后直接安装
pip install C:\tmp\mysqlclient-1.4.6-cp37-cp37m-win32.whl
三、解决办法二【简单推荐!】
直接引入pymysql,虽然第一次引入,但是缺少初始化信息,需要整体引用
import pymysql
pymysql.install_as_MySQLdb()
添加初始化后,就不会报错缺少mysqldb module了。