mysqlpython-3.9libmysqlclient

Unable to install mysqlclient in Python3.9


I encountered the following error while trying to install mysqlclient using the command

pip3 install mysqlclient

System information: Python 3.9, GNU/Linux Ubuntu 20.04 running on WSL

      x86_64-linux-gnu-gcc -pthread -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O2 -Wall -g -fstack-protector-strong -Wformat -Werror=format-security -g -fwrapv -O2 -fPIC -Dversion_info=(2,1,1,'final',0) -D__version__=2.1.1 -I/usr/include/mysql -I/home/eweinsh/microservice/src/auth/_venv/include -I/usr/include/python3.9 -c MySQLdb/_mysql.c -o build/temp.linux-x86_64-cpython-39/MySQLdb/_mysql.o -std=c99
      MySQLdb/_mysql.c:46:10: fatal error: Python.h: No such file or directory
         46 | #include "Python.h"
            |          ^~~~~~~~~~
      compilation terminated.
      error: command '/usr/bin/x86_64-linux-gnu-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: legacy-install-failure

Initially, the error seemed to be that I hadn't install the python3 dev tools correctly, so I reinstalled python3. However, I know believe this is an issue with my installation of MySql.

I reinstalled python3 with suggested packages. I also ensured configuration files were added for mysql.


Solution

  • You are in a virtual environment using python 3.9 instead of python 3.8 shipped with Ubuntu 20.04 try :

    sudo apt install python3.9-dev
    

    This package installs all headers files needed to build python3.9 instead of 3.8