python链接mysql报错:No module named MySQLdb

一、问题描述

尝试使用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)]

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)]

4.然后直接安装

pip install C:\tmp\mysqlclient-1.4.6-cp37-cp37m-win32.whl

三、解决办法二【简单推荐!】

直接引入pymysql,虽然第一次引入,但是缺少初始化信息,需要整体引用

import pymysql
pymysql.install_as_MySQLdb()

添加初始化后,就不会报错缺少mysqldb module了。

四、参考

1.No module named MySQLdb

2.mysqlclient安装报错----error: Microsoft Visual C++ 14.0 is required

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值