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
网上查了一大堆,有几种解决方法
-
- 离线下载 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相关数据库操作