报错的最便捷方法"/>
Python 3.7 Error loading MySQLdb module. 安装mysqlClient报错的最便捷方法
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_removedFile "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 DatabaseIntrospectionFile "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:
Collecting mysqlclientDownloading .1.1.tar.gz (88 kB)---------------------------------------- 88.1/88.1 kB 2.5 MB/s eta 0:00:00Installing build dependencies ... doneGetting requirements to build wheel ... donePreparing metadata (pyproject.toml) ... done
Building wheels for collected packages: mysqlclientBuilding wheel for mysqlclient (pyproject.toml) ... errorerror: subprocess-exited-with-error× Building wheel for mysqlclient (pyproject.toml) did not run successfully.│ exit code: 1╰─> [23 lines of output]running bdist_wheelrunning buildrunning build_pycreating buildcreating build\lib.win-amd64-cpython-37creating build\lib.win-amd64-cpython-37\MySQLdbcopying MySQLdb\__init__.py -> build\lib.win-amd64-cpython-37\MySQLdbcopying MySQLdb\_exceptions.py -> build\lib.win-amd64-cpython-37\MySQLdbcopying MySQLdb\connections.py -> build\lib.win-amd64-cpython-37\MySQLdbcopying MySQLdb\converters.py -> build\lib.win-amd64-cpython-37\MySQLdbcopying MySQLdb\cursors.py -> build\lib.win-amd64-cpython-37\MySQLdbcopying MySQLdb\release.py -> build\lib.win-amd64-cpython-37\MySQLdbcopying MySQLdb\times.py -> build\lib.win-amd64-cpython-37\MySQLdbcreating build\lib.win-amd64-cpython-37\MySQLdb\constantscopying MySQLdb\constants\__init__.py -> build\lib.win-amd64-cpython-37\MySQLdb\constantscopying MySQLdb\constants\CLIENT.py -> build\lib.win-amd64-cpython-37\MySQLdb\constantscopying MySQLdb\constants\CR.py -> build\lib.win-amd64-cpython-37\MySQLdb\constantscopying MySQLdb\constants\ER.py -> build\lib.win-amd64-cpython-37\MySQLdb\constantscopying MySQLdb\constants\FIELD_TYPE.py -> build\lib.win-amd64-cpython-37\MySQLdb\constantscopying MySQLdb\constants\FLAG.py -> build\lib.win-amd64-cpython-37\MySQLdb\constantsrunning build_extbuilding 'MySQLdb._mysql' extensionerror: Microsoft Visual C++ 14.0 or greater is required. Get it with "Microsoft C++ 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
网上查了一大堆,有几种解决方法
-
- 离线下载 mysqlclient‑1.4.6‑cp39‑cp39‑win32.whl 并进行离线安装
-
- pip install mysql-python 试过了 没有用
-
- 安装一些编译MySQL所需的依赖库
sudo apt-get install python3-dev default-libmysqlclient-dev build-essential sudo apt-get install mysql-client pip install mysqlclient
- 安装一些编译MySQL所需的依赖库
看着就麻烦,而且我是在使用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相关数据库操作
更多推荐
Python 3.7 Error loading MySQLdb module. 安装mysqlClient报错的最便捷方法
发布评论