[Django]如何在安装mysqlclient时解决错误


环境

Windows 10主页
Python 3.8.1
Django 3.0.7

错误细节

当我安装mysqlclient(推荐用于Django 2.2及更高版本)时,出现以下冗长的错误:

1
(venv) C:\Development\django-project\django_project>pip install mysqlclient
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
Collecting mysqlclient
  Using cached mysqlclient-1.4.6.tar.gz (85 kB)
Using legacy setup.py install for mysqlclient, since package 'wheel' is not installed.
Installing collected packages: mysqlclient
    Running setup.py install for mysqlclient ... error
    ERROR: Command errored out with exit status 1:
     command: 'c:\development\django-project\venv\scripts\python.exe' -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'C:\\Users\\Ryozo\\AppData\\Local\\Temp\\pip-install-qnbecfcr\\mysqlclient\\setup.py
'"'"'; __file__='"'"'C:\\Users\\Ryozo\\AppData\\Local\\Temp\\pip-install-qnbecfcr\\mysqlclient\\setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"')
;f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record 'C:\Users\Ryozo\AppData\Local\Temp\pip-record-isgb1l1w\install-record.txt' --single-version-externally-managed --compile --install-headers
 'c:\development\django-project\venv\include\site\python3.8\mysqlclient'
         cwd: C:\Users\Ryozo\AppData\Local\Temp\pip-install-qnbecfcr\mysqlclient\
    Complete output (30 lines):
    running install
    running build
    running build_py
    creating build
    creating build\lib.win32-3.8
    creating build\lib.win32-3.8\MySQLdb
    copying MySQLdb\__init__.py -> build\lib.win32-3.8\MySQLdb
    copying MySQLdb\_exceptions.py -> build\lib.win32-3.8\MySQLdb
    copying MySQLdb\compat.py -> build\lib.win32-3.8\MySQLdb
    copying MySQLdb\connections.py -> build\lib.win32-3.8\MySQLdb
    copying MySQLdb\converters.py -> build\lib.win32-3.8\MySQLdb
    copying MySQLdb\cursors.py -> build\lib.win32-3.8\MySQLdb
    copying MySQLdb\release.py -> build\lib.win32-3.8\MySQLdb
    copying MySQLdb\times.py -> build\lib.win32-3.8\MySQLdb
    creating build\lib.win32-3.8\MySQLdb\constants
    copying MySQLdb\constants\__init__.py -> build\lib.win32-3.8\MySQLdb\constants
    copying MySQLdb\constants\CLIENT.py -> build\lib.win32-3.8\MySQLdb\constants
    copying MySQLdb\constants\CR.py -> build\lib.win32-3.8\MySQLdb\constants
    copying MySQLdb\constants\ER.py -> build\lib.win32-3.8\MySQLdb\constants
    copying MySQLdb\constants\FIELD_TYPE.py -> build\lib.win32-3.8\MySQLdb\constants
    copying MySQLdb\constants\FLAG.py -> build\lib.win32-3.8\MySQLdb\constants
    running build_ext
    building 'MySQLdb._mysql' extension
    creating build\temp.win32-3.8
    creating build\temp.win32-3.8\Release
    creating build\temp.win32-3.8\Release\MySQLdb
    C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.26.28801\bin\HostX86\x86\cl.exe /c /nologo /Ox /W3 /GL /DNDEBUG /MD -Dversion_info=(1,4,6,'final',0) -D__version__=1.4.6 "-IC:\Pro
gram Files (x86)\MySQL\MySQL Connector C 6.1\include\mariadb" -Ic:\development\django-project\venv\include -IC:\Users\Ryozo\AppData\Local\Programs\Python\Python38-32\include -IC:\Users\Ryozo\AppData\Local\Programs
\Python\Python38-32\include "-IC:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.26.28801\include" /TcMySQLdb/_mysql.c /Fobuild\temp.win32-3.8\Release\MySQLdb/_mysql.obj /Zl /D_CRT_SEC
URE_NO_WARNINGS
    _mysql.c
    MySQLdb/_mysql.c(29): fatal error C1083: include ファイルを開けません。'mysql.h':No such file or directory
    error: command 'C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\Community\\VC\\Tools\\MSVC\\14.26.28801\\bin\\HostX86\\x86\\cl.exe' failed with exit status 2
    ----------------------------------------
ERROR: Command errored out with exit status 1: 'c:\development\django-project\venv\scripts\python.exe' -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'C:\\Users\\Ryozo\\AppData\\Local\\Temp\\pip-instal
l-qnbecfcr\\mysqlclient\\setup.py'"'"'; __file__='"'"'C:\\Users\\Ryozo\\AppData\\Local\\Temp\\pip-install-qnbecfcr\\mysqlclient\\setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().repl
ace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record 'C:\Users\Ryozo\AppData\Local\Temp\pip-record-isgb1l1w\install-record.txt' --single-version-externally-ma
naged --compile --install-headers 'c:\development\django-project\venv\include\site\python3.8\mysqlclient' Check the logs for full command output.

解决方案

[下载]并安装适合您的环境的whl文件(https://www.lfd.uci.edu/~gohlke/pythonlibs/#mysqlclient)

在我的情况下是mysqlclient?1.4.6?Cp38?Cp38?Win32.whl。

移至下载目录并执行以下命令

1
2
3
4
5
6
7
(venv) C:\Development\django-project>pip install mysqlclient-1.4.6-cp38-cp38-win32.whl
Processing c:\development\django-project\mysqlclient-1.4.6-cp38-cp38-win32.whl
Installing collected packages: mysqlclient
Successfully installed mysqlclient-1.4.6

(venv) C:\Development\django-project>pip install mysqlclient
Requirement already satisfied: mysqlclient in c:\development\django-project\venv\lib\site-packages (1.4.6)

现在安装已完成,请使用pip install mysqlclient安装它,并说它已经存在。