0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

mysqlclientのインストールで、fatal error: 'mysql.h' file not foundに詰まりまくった

Posted at

pythonでmysqlclientをインストールしようとした時の備忘録

fatal error: 'mysql.h' file not found

pip install mysqlclientをしようとしたら以下のエラーが出た

src/MySQLdb/_mysql.c:29:10: fatal error: 'mysql.h' file not found
      #include "mysql.h"
               ^~~~~~~~~
      1 error generated.
      error: command '/usr/bin/gcc' failed with exit code 1
      [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: ERROR: Failed to build installable wheels for some pyproject.toml based projects (mysqlclient)

やったこと

まず、xcodeがなかったのでインストールしました。
私はこちらからダウンロードしました。
https://developer.apple.com/jp/xcode/resources/

$ brew link mysql
Warning: Already linked: /usr/local/Cellar/mysql/9.0.1_7
To relink, run:
  brew unlink mysql && brew link mysql
$ brew install mysql pkg-config
==> Downloading https://formulae.brew.sh/api/formula.jws.json
############################################################################################################################################################################################################################ 100.0%
==> Downloading https://formulae.brew.sh/api/cask.jws.json
############################################################################################################################################################################################################################ 100.0%
Warning: mysql 9.0.1_7 is already installed and up-to-date.
...
$ export MYSQLCLIENT_LDFLAGS=$(pkg-config --libs mysqlclient)
$ export MYSQLCLIENT_CFLAGS=$(pkg-config --cflags mysqlclient)
$ pip install mysqlclient
Collecting mysqlclient
  Using cached mysqlclient-2.2.6.tar.gz (91 kB)
...
$ brew uninstall mysql-client
Uninstalling /usr/local/Cellar/mysql-client/9.0.1... (120 files, 124.6MB)
$ brew install mysql-client@8.0                                                
==> Downloading https://formulae.brew.sh/api/formula.jws.json

==> Downloading https://formulae.brew.sh/api/cask.jws.json

Warning: mysql-client@8.0 8.0.40 is already installed and up-to-date.
To reinstall 8.0.40, run:
  brew reinstall mysql-client@8.0
...

ここでdjangoを実行しようとすると、

raise ImproperlyConfigured(
django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module.
Did you install mysqlclient?

このエラーが出てきたが、mysqlclient=2.2.4はインストール済み。
mysqlclientのバージョンを古いのにダウングレードしました。

$ pip uninstall mysqlclient
Found existing installation: mysqlclient 2.2.4
Uninstalling mysqlclient-2.2.4:
...
$ pip install mysqlclient==2.0.1 --no-cache-dir
Collecting mysqlclient==2.0.1
  Downloading mysqlclient-2.0.1.tar.gz (87 kB)
  Preparing metadata (setup.py) ... done
...

これでインストールできてdjangoも実行できました。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?