📚

【Python】「pip install mysqlclient」がエラーになたよ

2025/01/11に公開

Django はデフォルトの DB が sqlite3 となっています

MySQL に変更しようとして、必要なライブラリであるmysqlclientを インストールしようとしたらエラーになりました

とりあえず、エラー内容を見ていきます

エラー

(実行コマンド)

pip install mysqlclient

(エラー内容一部抜粋)

MySQLdb/_mysql.c(29): fatal error C1083: include ファイルを開けません。'mysql.h':No such file or directory

「mysql.h」がないっていってますね

何はなくとも検索してみる

https://libproc.com/python-install-mysqlclient-on-windows/

こちらの記事によると、「MySQL Connector C 6.1」をインストールすればエラーは解消。

MySQL Connector C 6.1 をインストールしてみた

公式 HPからインストーラーをダウンロードして、実行します

↓ が立ち上がります

されるがまま、インストールしていき完了したら OK

もう一回インストールしてみる

(実行コマンド)

pip install mysqlclient

(エラー内容一部抜粋)

MySQLdb/_mysql.c(29): fatal error C1083: include ファイルを開けません。'mysql.h':No such file or directory

変わらないんかい!!

別のライブラリを使いました

無理そうなので、PyMySQLというライブラリに変えました

(特にこだわりがあるわけではないので、、、)

PS C:\Users\******> pip install PyMySQL
Collecting PyMySQL
  Downloading PyMySQL-1.0.2-py3-none-any.whl (43 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 43.8/43.8 kB 2.2 MB/s eta 0:00:00
Installing collected packages: PyMySQL
Successfully installed PyMySQL-1.0.2
PS C:\Users\******>

普通にはいった(笑)

最初からこっちにしておけばよかった。。。

これでなんとか Python→MySQL へ接続できそう、よかった

(おまけ)インストール時の出力全文

PS C:\Users\******\Desktop\開発\Docker\React+Django\react_front> pip install mysqlclient
Collecting mysqlclient
  Using cached mysqlclient-2.1.1.tar.gz (88 kB)
  Preparing metadata (setup.py) ... done
Building wheels for collected packages: mysqlclient
  Building wheel for mysqlclient (setup.py) ... error
  error: subprocess-exited-with-error
  × python setup.py bdist_wheel did not run successfully.exit code: 1
  ╰─> [29 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
      creating build\temp.win-amd64-cpython-37
      creating build\temp.win-amd64-cpython-37\Release
      creating build\temp.win-amd64-cpython-37\Release\MySQLdb
      "C:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools\VC\Tools\MSVC\14.34.31933\bin\HostX86\x64\cl.exe" /c /nologo /O2 /W3 /GL /DNDEBUG /MD -Dversion_info=(2,1,1,'final',0) -D__version__=2.1.1 "-IC:\Program Files\MariaDB\MariaDB Connector C\include\mariadb" "-IC:\Program Files\MariaDB\MariaDB Connector C\include" -IC:\Users\******\anaconda3\envs\JIRA\include -IC:\Users\******\anaconda3\envs\JIRA\Include "-IC:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools\VC\Tools\MSVC\14.34.31933\include" "-IC:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools\VC\Auxiliary\VS\include" "-IC:\Program Files
(x86)\Windows Kits\10\include\10.0.22000.0\ucrt" "-IC:\Program Files (x86)\Windows Kits\10\\include\10.0.22000.0\\um" "-IC:\Program Files (x86)\Windows Kits\10\\include\10.0.22000.0\\shared" "-IC:\Program Files
(x86)\Windows Kits\10\\include\10.0.22000.0\\winrt" "-IC:\Program Files (x86)\Windows Kits\10\\include\10.0.22000.0\\cppwinrt" /TcMySQLdb/_mysql.c /Fobuild\temp.win-amd64-cpython-37\Release\MySQLdb/_mysql.obj
      _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\\2022\\BuildTools\\VC\\Tools\\MSVC\\14.34.31933\\bin\\HostX86\\x64\\cl.exe' failed with exit code 2
      [end of output]

  note: This error originates from a subprocess, and is likely not a problem with pip.
  ERROR: Failed building wheel for mysqlclient
  Running setup.py clean for mysqlclient
Failed to build mysqlclient
Installing collected packages: mysqlclient
  Running setup.py install for mysqlclient ... error
  error: subprocess-exited-with-error

  × Running setup.py install for mysqlclient did not run successfully.exit code: 1
  ╰─> [31 lines of output]
      running install
      C:\Users\******\anaconda3\envs\JIRA\lib\site-packages\setuptools\command\install.py:37: SetuptoolsDeprecationWarning: setup.py install is deprecated. Use build and pip and other standards-based tools.
        setuptools.SetuptoolsDeprecationWarning,
      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
      creating build\temp.win-amd64-cpython-37
      creating build\temp.win-amd64-cpython-37\Release
      creating build\temp.win-amd64-cpython-37\Release\MySQLdb
      "C:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools\VC\Tools\MSVC\14.34.31933\bin\HostX86\x64\cl.exe" /c /nologo /O2 /W3 /GL /DNDEBUG /MD -Dversion_info=(2,1,1,'final',0) -D__version__=2.1.1 "-IC:\Program Files\MariaDB\MariaDB Connector C\include\mariadb" "-IC:\Program Files\MariaDB\MariaDB Connector C\include" -IC:\Users\******\anaconda3\envs\JIRA\include -IC:\Users\******\anaconda3\envs\JIRA\Include "-IC:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools\VC\Tools\MSVC\14.34.31933\include" "-IC:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools\VC\Auxiliary\VS\include" "-IC:\Program Files
(x86)\Windows Kits\10\include\10.0.22000.0\ucrt" "-IC:\Program Files (x86)\Windows Kits\10\\include\10.0.22000.0\\um" "-IC:\Program Files (x86)\Windows Kits\10\\include\10.0.22000.0\\shared" "-IC:\Program Files
(x86)\Windows Kits\10\\include\10.0.22000.0\\winrt" "-IC:\Program Files (x86)\Windows Kits\10\\include\10.0.22000.0\\cppwinrt" /TcMySQLdb/_mysql.c /Fobuild\temp.win-amd64-cpython-37\Release\MySQLdb/_mysql.obj
      _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\\2022\\BuildTools\\VC\\Tools\\MSVC\\14.34.31933\\bin\\HostX86\\x64\\cl.exe' failed with exit code 2
      [end of output]

  note: This error originates from a subprocess, and is likely not a problem with pip.
error: legacy-install-failure

× Encountered error while trying to install package.
╰─> mysqlclient

note: This is an issue with the package mentioned above, not pip.
hint: See above for output from the failure.
PS C:\Users\******\Desktop\開発\Docker\React+Django\react_front>

Discussion