Windows + Python 3.8 の環境下で pip install mysqlclient が失敗する原因と対策について、備忘録をまとめておきます。
結論
Windows 10 + Python 3.8 + pip 19.2.2 以前という環境で、mysqlclient 1.4.6 の pip インストールが失敗する。その際、「Microsoft Visual C++ 14.0 is required」あるいは「include ファイルを開けません。'mysql.h':No such file or directory」といったエラーが出る。
対策としては、pip のバージョンを 19.2.3 以降にアップグレードするだけ。
なお、Microsoft Visual C++ をインストールする必要はありません。
詳細
エラー(その1)
PC に Visual C++ がインストールされていない状況では、「error: Microsoft Visual C++ 14.0 is required」というエラーが出ます。しかし、Visual C++ をインストールしても後述するエラー(その2)が出てしまい、根本解決には至りません。
《 発生条件 》
- Windows 10
- Python 3.8.2
- pip 19.2.2
- Microsoft Visual C++ 14.0 以降が未インストール
《 エラー内容 》
(venv) C:\Users\akiyoko\work\mysqlclient-test>pip install --no-cache-dir mysqlclient Collecting mysqlclient Downloading https://files.pythonhosted.org/packages/d0/97/7326248ac8d5049968bf4ec708a5d3d4806e412a42e74160d7f266a3e03a/mysqlclient-1.4.6.tar.gz (85kB) |████████████████████████████████| 92kB 990kB/s Installing collected packages: mysqlclient Running setup.py install for mysqlclient ... error ERROR: Command errored out with exit status 1: command: 'c:\users\akiyoko\work\mysqlclient-test\venv\scripts\python.exe' -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'C:\\Users\\akiyoko\\AppData\\Local\\Temp\\pip-install-yluhih0a\\mysqlclient\\setup.py'"'"'; __file__='"'"'C:\\Users\\akiyoko\\AppData\\Local\\Temp\\pip-install-yluhih0a\\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\akiyoko\AppData\Local\Temp\pip-record-4l7b4jv9\install-record.txt' --single-version-externally-managed --compile --install-headers 'c:\users\akiyoko\work\mysqlclient-test\venv\include\site\python3.8\mysqlclient' cwd: C:\Users\akiyoko\AppData\Local\Temp\pip-install-yluhih0a\mysqlclient\ Complete output (24 lines): running install running build running build_py creating build creating build\lib.win-amd64-3.8 creating build\lib.win-amd64-3.8\MySQLdb copying MySQLdb\__init__.py -> build\lib.win-amd64-3.8\MySQLdb copying MySQLdb\_exceptions.py -> build\lib.win-amd64-3.8\MySQLdb copying MySQLdb\compat.py -> build\lib.win-amd64-3.8\MySQLdb copying MySQLdb\connections.py -> build\lib.win-amd64-3.8\MySQLdb copying MySQLdb\converters.py -> build\lib.win-amd64-3.8\MySQLdb copying MySQLdb\cursors.py -> build\lib.win-amd64-3.8\MySQLdb copying MySQLdb\release.py -> build\lib.win-amd64-3.8\MySQLdb copying MySQLdb\times.py -> build\lib.win-amd64-3.8\MySQLdb creating build\lib.win-amd64-3.8\MySQLdb\constants copying MySQLdb\constants\__init__.py -> build\lib.win-amd64-3.8\MySQLdb\constants copying MySQLdb\constants\CLIENT.py -> build\lib.win-amd64-3.8\MySQLdb\constants copying MySQLdb\constants\CR.py -> build\lib.win-amd64-3.8\MySQLdb\constants copying MySQLdb\constants\ER.py -> build\lib.win-amd64-3.8\MySQLdb\constants copying MySQLdb\constants\FIELD_TYPE.py -> build\lib.win-amd64-3.8\MySQLdb\constants copying MySQLdb\constants\FLAG.py -> build\lib.win-amd64-3.8\MySQLdb\constants running build_ext building 'MySQLdb._mysql' extension error: Microsoft Visual C++ 14.0 is required. Get it with "Microsoft Visual C++ Build Tools": https://visualstudio.microsoft.com/downloads/ ---------------------------------------- ERROR: Command errored out with exit status 1: ...(略)...
ちなみに、Visual Studio Community 2019 のインストール時に、Visual C++ を合わせてインストールすることができます(根本解決にはならないので悪しからず)。手順を以下に示します(が、やるだけ無駄ですのでご注意を)。
Visual Studio 2019 のダウンロードページ から、コミュニティ版のインストーラをダウンロードします。
ダウンロードしたインストーラをダブルクリックすると、Visual Studio Installer が起動します。「個別のコンポーネント」から「Visual C++」を検索して、「C++ x64/x86 ビルドツール」を選択してインストールします。
「Visual C++」がインストールできました。Windows の「アプリと機能」から確認可能です。
なお、「Microsoft Visual C++ Redistributable for Visual Studio 2015, 2017 and 2019」は、https://support.microsoft.com/en-gb/help/2977003/the-latest-supported-visual-c-downloads からも exe ファイルを直接ダウンロードしてインストールが可能です。PC の環境に合わせてご利用ください(私の環境は「x64」でした)。
エラー(その2)
PC にすでに Visual C++ がインストールされていても、pip のバージョンが 19.2.2 以前だと、「fatal error C1083: include ファイルを開けません。'mysql.h':No such file or directory」というエラーが出てしまいます。
《 発生条件 》
- Windows 10
- Python 3.8.2
- pip 19.2.2
- Microsoft Visual C++ 14.0 以降がインストール済み
《 エラー内容 》
(venv) C:\Users\akiyoko\work\mysqlclient-test>pip install --no-cache-dir mysqlclient Collecting mysqlclient Downloading https://files.pythonhosted.org/packages/d0/97/7326248ac8d5049968bf4ec708a5d3d4806e412a42e74160d7f266a3e03a/mysqlclient-1.4.6.tar.gz (85kB) |████████████████████████████████| 92kB 845kB/s Installing collected packages: mysqlclient Running setup.py install for mysqlclient ... error ERROR: Command errored out with exit status 1: command: 'c:\users\akiyoko\work\mysqlclient-test\venv\scripts\python.exe' -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'C:\\Users\\akiyoko\\AppData\\Local\\Temp\\pip-install-kp34ucod\\mysqlclient\\setup.py'"'"'; __file__='"'"'C:\\Users\\akiyoko\\AppData\\Local\\Temp\\pip-install-kp34ucod\\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\akiyoko\AppData\Local\Temp\pip-record-3csqetin\install-record.txt' --single-version-externally-managed --compile --install-headers 'c:\users\akiyoko\work\mysqlclient-test\venv\include\site\python3.8\mysqlclient' cwd: C:\Users\akiyoko\AppData\Local\Temp\pip-install-kp34ucod\mysqlclient\ Complete output (30 lines): running install running build running build_py creating build creating build\lib.win-amd64-3.8 creating build\lib.win-amd64-3.8\MySQLdb copying MySQLdb\__init__.py -> build\lib.win-amd64-3.8\MySQLdb copying MySQLdb\_exceptions.py -> build\lib.win-amd64-3.8\MySQLdb copying MySQLdb\compat.py -> build\lib.win-amd64-3.8\MySQLdb copying MySQLdb\connections.py -> build\lib.win-amd64-3.8\MySQLdb copying MySQLdb\converters.py -> build\lib.win-amd64-3.8\MySQLdb copying MySQLdb\cursors.py -> build\lib.win-amd64-3.8\MySQLdb copying MySQLdb\release.py -> build\lib.win-amd64-3.8\MySQLdb copying MySQLdb\times.py -> build\lib.win-amd64-3.8\MySQLdb creating build\lib.win-amd64-3.8\MySQLdb\constants copying MySQLdb\constants\__init__.py -> build\lib.win-amd64-3.8\MySQLdb\constants copying MySQLdb\constants\CLIENT.py -> build\lib.win-amd64-3.8\MySQLdb\constants copying MySQLdb\constants\CR.py -> build\lib.win-amd64-3.8\MySQLdb\constants copying MySQLdb\constants\ER.py -> build\lib.win-amd64-3.8\MySQLdb\constants copying MySQLdb\constants\FIELD_TYPE.py -> build\lib.win-amd64-3.8\MySQLdb\constants copying MySQLdb\constants\FLAG.py -> build\lib.win-amd64-3.8\MySQLdb\constants running build_ext building 'MySQLdb._mysql' extension creating build\temp.win-amd64-3.8 creating build\temp.win-amd64-3.8\Release creating build\temp.win-amd64-3.8\Release\MySQLdb C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.25.28610\bin\HostX86\x64\cl.exe /c /nologo /Ox /W3 /GL /DNDEBUG /MD -Dversion_info=(1,4,6,'final',0) -D__version__=1.4.6 "-IC:\Program Files (x86)\MySQL\MySQL Connector C 6.1\include\mariadb" -Ic:\users\akiyoko\work\mysqlclient-test\venv\include -IC:\Users\akiyoko\AppData\Local\Programs\Python\Python38\include -IC:\Users\akiyoko\AppData\Local\Programs\Python\Python38\include "-IC:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.25.28610\include" /TcMySQLdb/_mysql.c /Fobuild\temp.win-amd64-3.8\Release\MySQLdb/_mysql.obj /Zl /D_CRT_SECURE_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.25.28610\\bin\\HostX86\\x64\\cl.exe' failed with exit status 2 ---------------------------------------- ERROR: Command errored out with exit status 1: ...(略)...
原因
根本原因
同じ環境でも pip のバージョンによって、対応している Python のバージョンと ABIタグの組み合わせが微妙に異なっているということが分かりました。具体的には、バージョン 19.2.3 の前後で
- pip(バージョン 19.2.2 以前)では、Python バージョン「3.8」に対応する ABIタグは「cp38m」のみ
- pip(バージョン 19.2.3 以降)では、Python バージョン「3.8」に対応する ABIタグは「cp38」のみ
となっていたのですが、この差は、
から出てきたものと思われます。
なお、対応している Python バージョンと ABIタグの組み合わせは、
>>> from pip._internal.pep425tags import get_supported >>> get_supported()
の実行結果から確認可能です。
一方の mysqlclient は、PyPI でソースコード形式と(コンパイル済みの C拡張を含んだ)wheel形式が配布されているのですが、最新バージョンの「1.4.6」では、Pythonバージョン「3.8」と ABIタグ「cp38」の組み合わせに対応する 「mysqlclient‑1.4.6‑cp38‑cp38‑win_amd64.whl」は用意されているものの、Pythonバージョン「3.8」と ABIタグ「cp38m」の組み合わせに対応する「mysqlclient‑1.4.6‑cp38‑cp38m‑win_amd64.whl」は用意されておらず、ソースコードをローカルでビルドしようとしてエラーになっているものと推測されます。 *1
そもそも、wheel形式の配布物が利用できないのが根本原因で、これが利用できれば C++ によるビルドは必要ないはずです。
どうしてこのような状況になるのか?
Python をインストールしたときに合わせてインストールされた pip が古いままになっている(現在は最新の Python 3.8.2 をインストールするとバージョン「19.2.3」の pip が同梱されます)、あるいは、PyCharm 内部で利用されている pip のバージョンが古い(PyCharm 2019.3 では「19.0.3」だったが、最新版の PyCharm 2020.1 では「20.0.2」となっていてこの事象は解消済み)などの状況が考えられます。
*1:非公式ですが、Python Extension Packages for Windows - Christoph Gohlke から確認可能