Python 3.7 Error loading MySQLdb module. 安装mysqlClient报错的最便捷方法

本文讲述了在Python3.7环境下使用Django时遇到的mysqlClient安装错误,介绍了通过安装Pymysql并替换原MySQLDB模块来解决问题的方法,适用于WindowsPyCharm环境。
摘要由CSDN通过智能技术生成

Python 3.7 安装mysqlClient报错的最便捷方法

1.问题背景

在构建新的基于Pyhton3.7的Django Server时,引入mysql后,服务频繁报出 Error loading MySQLdb module. Did you install mysqlcliet,导致服务请求失败。

2.问题描述

服务请求报错

 File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "D:\work_space_for_python\calculate_for_GridAgent\venv\lib\site-packages\django\db\backends\mysql\base.py", line 29, in <module>
    from .introspection import DatabaseIntrospection
  File "D:\work_space_for_python\calculate_for_GridAgent\venv\lib\site-packages\django\db\backends\mysql\introspection.py", line 4, in <module>
    from MySQLdb.constants import FIELD_TYPE
ModuleNotFoundError: No module named 'MySQLdb'

尝试安装mysqlclients

ERROR: Could not find a version that satisfies the requirement ridAgent (from versions: none)
ERROR: No matching distribution found for ridAgent

(venv) D:\work_space_for_python\calculate_for_GridAgent>pip install mysqlclient
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple
Collecting mysqlclient
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/50/5f/eac919b88b9df39bbe4a855f136d58f80d191cfea34a3dcf96bf5d8ace0a/mysqlclient-2.1.1.tar.gz (88 kB)
     ---------------------------------------- 88.1/88.1 kB 2.5 MB/s eta 0:00:00
  Installing build dependencies ... done
  Getting requirements to build wheel ... done
  Preparing metadata (pyproject.toml) ... done
Building wheels for collected packages: mysqlclient
  Building wheel for mysqlclient (pyproject.toml) ... error
  error: subprocess-exited-with-error

  × Building wheel for mysqlclient (pyproject.toml) did not run successfully.
  │ exit code: 1
  ╰─> [23 lines of output]
      running bdist_wheel
      running build
      running build_py
      creating build
      creating build\lib.win-amd64-cpython-37
      creating build\lib.win-amd64-cpython-37\MySQLdb
      copying MySQLdb\__init__.py -> build\lib.win-amd64-cpython-37\MySQLdb
      copying MySQLdb\_exceptions.py -> build\lib.win-amd64-cpython-37\MySQLdb
      copying MySQLdb\connections.py -> build\lib.win-amd64-cpython-37\MySQLdb
      copying MySQLdb\converters.py -> build\lib.win-amd64-cpython-37\MySQLdb
      copying MySQLdb\cursors.py -> build\lib.win-amd64-cpython-37\MySQLdb
      copying MySQLdb\release.py -> build\lib.win-amd64-cpython-37\MySQLdb
      copying MySQLdb\times.py -> build\lib.win-amd64-cpython-37\MySQLdb
      creating build\lib.win-amd64-cpython-37\MySQLdb\constants
      copying MySQLdb\constants\__init__.py -> build\lib.win-amd64-cpython-37\MySQLdb\constants
      copying MySQLdb\constants\CLIENT.py -> build\lib.win-amd64-cpython-37\MySQLdb\constants
      copying MySQLdb\constants\CR.py -> build\lib.win-amd64-cpython-37\MySQLdb\constants
      copying MySQLdb\constants\ER.py -> build\lib.win-amd64-cpython-37\MySQLdb\constants
      copying MySQLdb\constants\FIELD_TYPE.py -> build\lib.win-amd64-cpython-37\MySQLdb\constants
      copying MySQLdb\constants\FLAG.py -> build\lib.win-amd64-cpython-37\MySQLdb\constants
      running build_ext
      building 'MySQLdb._mysql' extension
      error: Microsoft Visual C++ 14.0 or greater is required. Get it with "Microsoft C++ Build Tools": https://visualstudio.microsoft.com/visual-cpp-build-tools/
      [end of output]

  note: This error originates from a subprocess, and is likely not a problem with pip.
  ERROR: Failed building wheel for mysqlclient
Failed to build mysqlclient
ERROR: Could not build wheels for mysqlclient, which is required to install pyproject.toml-based projects

网上查了一大堆,有几种解决方法

    1. 离线下载 mysqlclient‑1.4.6‑cp39‑cp39‑win32.whl 并进行离线安装
    1. pip install mysql-python 试过了 没有用
    1. 安装一些编译MySQL所需的依赖库
      sudo apt-get install python3-dev default-libmysqlclient-dev build-essential 
      sudo apt-get install mysql-client
      pip install mysqlclient
      

看着就麻烦,而且我是在使用windows pycharm在开发,所以就没有试
最后找到了一种,使用pymysql替代mysqlClient

3.解决方案

3.1 安装Pymysql

pip3 install pymysql

3.2 替换Django 原始使用中的MYSQLDB

在这里插入图片描述

3.3 使用pymysql完成数据库操作

获取连接

connection = pymysql.connect(
host='localhost',  # 数据库主机地址
user='root',  # 数据库用户名
password='password',  # 数据库密码
db='database'  # 数据库名称
)

创建游标

cursor = connection.cursor(pymysql.cursors.DictCursor)

使用游标执行mysql相关数据库操作

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值