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

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

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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相关数据库操作

要解决Django中`ImproperlyConfigured: Error loading MySQLdb module`的错误,并改用`mysql-connector-python`(也称为`pymysql`),你需要执行以下步骤: 1. **移除MySQLdb依赖**: 首先,确认你的项目settings.py文件中已不再引用MySQLdb。如果存在,请移除对它的导入和配置。 2. **安装mysql-connector-python**: 使用pip安装`mysql-connector-python`,因为这是兼容Python 3的新版本MySQL客户端库。在命令行中运行: ```shell pip install mysql-connector-python ``` 3. **更新设置**: 在settings.py中,修改DATABASES配置以指向`mysql-connector-python`。示例配置可能如下所示: ```python DATABASES = { &#39;default&#39;: { &#39;ENGINE&#39;: &#39;django.db.backends.mysql&#39;, &#39;NAME&#39;: &#39;your_database_name&#39;, &#39;USER&#39;: &#39;your_username&#39;, &#39;PASSWORD&#39;: &#39;your_password&#39;, &#39;HOST&#39;: &#39;your_host&#39;, &#39;PORT&#39;: &#39;your_port&#39;, } } ``` 如果你正在使用Django的`sqlite3`数据库做迁移,记得保持`TEST`数据库引擎不变,直到完成迁移后再切换。 4. **重启Django服务器**: 重新启动你的开发服务器 (`python manage.py runserver`),让Django应用新的数据库配置。 如果你已经按照上述步骤操作但仍然遇到错误,可能是因为其他地方还在使用MySQLdb,如中间件或第三方应用。确保在整个项目的依赖链上都完成了替换。另外,检查环境变量或虚拟环境中的包冲突也可能导致此问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值