Error while installing mysqlclient in django

Question:

I’m trying to install mysqlclient using

pip install mysqlclient
But facing the following error

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: Command errored out with exit status 1:
   command: /home/pixarsart/PycharmProjects/video_slicing/venv/bin/python -u -c 'import io, os, sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-uljhxj0e/mysqlclient_bcf14369053645289dd7e777e689db38/setup.py'"'"'; __file__='"'"'/tmp/pip-install-uljhxj0e/mysqlclient_bcf14369053645289dd7e777e689db38/setup.py'"'"';f = getattr(tokenize, '"'"'open'"'"', open)(__file__) if os.path.exists(__file__) else io.StringIO('"'"'from setuptools import setup; setup()'"'"');code = f.read().replace('"'"'rn'"'"', '"'"'n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' bdist_wheel -d /tmp/pip-wheel-r6zmjoeu
       cwd: /tmp/pip-install-uljhxj0e/mysqlclient_bcf14369053645289dd7e777e689db38/
  Complete output (43 lines):
  mysql_config --version
  ['8.0.31']
  mysql_config --libs
  ['-L/usr/lib/x86_64-linux-gnu', '-lmysqlclient', '-lpthread', '-ldl', '-lssl', '-lcrypto', '-lresolv', '-lm', '-lrt']
  mysql_config --cflags
  ['-I/usr/include/mysql']
  ext_options:
    library_dirs: ['/usr/lib/x86_64-linux-gnu']
    libraries: ['mysqlclient', 'pthread', 'dl', 'resolv', 'm', 'rt']
    extra_compile_args: ['-std=c99']
    extra_link_args: []
    include_dirs: ['/usr/include/mysql']
    extra_objects: []
    define_macros: [('version_info', "(2,1,1,'final',0)"), ('__version__', '2.1.1')]
  running bdist_wheel
  running build
  running build_py
  creating build
  creating build/lib.linux-x86_64-3.11
  creating build/lib.linux-x86_64-3.11/MySQLdb
  copying MySQLdb/__init__.py -> build/lib.linux-x86_64-3.11/MySQLdb
  copying MySQLdb/_exceptions.py -> build/lib.linux-x86_64-3.11/MySQLdb
  copying MySQLdb/connections.py -> build/lib.linux-x86_64-3.11/MySQLdb
  copying MySQLdb/converters.py -> build/lib.linux-x86_64-3.11/MySQLdb
  copying MySQLdb/cursors.py -> build/lib.linux-x86_64-3.11/MySQLdb
  copying MySQLdb/release.py -> build/lib.linux-x86_64-3.11/MySQLdb
  copying MySQLdb/times.py -> build/lib.linux-x86_64-3.11/MySQLdb
  creating build/lib.linux-x86_64-3.11/MySQLdb/constants
  copying MySQLdb/constants/__init__.py -> build/lib.linux-x86_64-3.11/MySQLdb/constants
  copying MySQLdb/constants/CLIENT.py -> build/lib.linux-x86_64-3.11/MySQLdb/constants
  copying MySQLdb/constants/CR.py -> build/lib.linux-x86_64-3.11/MySQLdb/constants
  copying MySQLdb/constants/ER.py -> build/lib.linux-x86_64-3.11/MySQLdb/constants
  copying MySQLdb/constants/FIELD_TYPE.py -> build/lib.linux-x86_64-3.11/MySQLdb/constants
  copying MySQLdb/constants/FLAG.py -> build/lib.linux-x86_64-3.11/MySQLdb/constants
  running build_ext
  creating build/temp.linux-x86_64-3.11
  creating build/temp.linux-x86_64-3.11/MySQLdb
  x86_64-linux-gnu-gcc -pthread -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/pixarsart/PycharmProjects/video_slicing/venv/include -I/usr/include/python3.11 -c MySQLdb/_mysql.c -o build/temp.linux-x86_64-3.11/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
  ----------------------------------------
  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: Command errored out with exit status 1:
     command: /home/pixarsart/PycharmProjects/video_slicing/venv/bin/python -u -c 'import io, os, sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-uljhxj0e/mysqlclient_bcf14369053645289dd7e777e689db38/setup.py'"'"'; __file__='"'"'/tmp/pip-install-uljhxj0e/mysqlclient_bcf14369053645289dd7e777e689db38/setup.py'"'"';f = getattr(tokenize, '"'"'open'"'"', open)(__file__) if os.path.exists(__file__) else io.StringIO('"'"'from setuptools import setup; setup()'"'"');code = f.read().replace('"'"'rn'"'"', '"'"'n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record /tmp/pip-record-x5dprlcj/install-record.txt --single-version-externally-managed --compile --install-headers /home/pixarsart/PycharmProjects/video_slicing/venv/include/site/python3.11/mysqlclient
         cwd: /tmp/pip-install-uljhxj0e/mysqlclient_bcf14369053645289dd7e777e689db38/
    Complete output (45 lines):
    mysql_config --version
    ['8.0.31']
    mysql_config --libs
    ['-L/usr/lib/x86_64-linux-gnu', '-lmysqlclient', '-lpthread', '-ldl', '-lssl', '-lcrypto', '-lresolv', '-lm', '-lrt']
    mysql_config --cflags
    ['-I/usr/include/mysql']
    ext_options:
      library_dirs: ['/usr/lib/x86_64-linux-gnu']
      libraries: ['mysqlclient', 'pthread', 'dl', 'resolv', 'm', 'rt']
      extra_compile_args: ['-std=c99']
      extra_link_args: []
      include_dirs: ['/usr/include/mysql']
      extra_objects: []
      define_macros: [('version_info', "(2,1,1,'final',0)"), ('__version__', '2.1.1')]
    running install
    /home/pixarsart/PycharmProjects/video_slicing/venv/lib/python3.11/site-packages/setuptools/command/install.py:34: SetuptoolsDeprecationWarning: setup.py install is deprecated. Use build and pip and other standards-based tools.
      warnings.warn(
    running build
    running build_py
    creating build
    creating build/lib.linux-x86_64-3.11
    creating build/lib.linux-x86_64-3.11/MySQLdb
    copying MySQLdb/__init__.py -> build/lib.linux-x86_64-3.11/MySQLdb
    copying MySQLdb/_exceptions.py -> build/lib.linux-x86_64-3.11/MySQLdb
    copying MySQLdb/connections.py -> build/lib.linux-x86_64-3.11/MySQLdb
    copying MySQLdb/converters.py -> build/lib.linux-x86_64-3.11/MySQLdb
    copying MySQLdb/cursors.py -> build/lib.linux-x86_64-3.11/MySQLdb
    copying MySQLdb/release.py -> build/lib.linux-x86_64-3.11/MySQLdb
    copying MySQLdb/times.py -> build/lib.linux-x86_64-3.11/MySQLdb
    creating build/lib.linux-x86_64-3.11/MySQLdb/constants
    copying MySQLdb/constants/__init__.py -> build/lib.linux-x86_64-3.11/MySQLdb/constants
    copying MySQLdb/constants/CLIENT.py -> build/lib.linux-x86_64-3.11/MySQLdb/constants
    copying MySQLdb/constants/CR.py -> build/lib.linux-x86_64-3.11/MySQLdb/constants
    copying MySQLdb/constants/ER.py -> build/lib.linux-x86_64-3.11/MySQLdb/constants
    copying MySQLdb/constants/FIELD_TYPE.py -> build/lib.linux-x86_64-3.11/MySQLdb/constants
    copying MySQLdb/constants/FLAG.py -> build/lib.linux-x86_64-3.11/MySQLdb/constants
    running build_ext
    creating build/temp.linux-x86_64-3.11
    creating build/temp.linux-x86_64-3.11/MySQLdb
    x86_64-linux-gnu-gcc -pthread -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/pixarsart/PycharmProjects/video_slicing/venv/include -I/usr/include/python3.11 -c MySQLdb/_mysql.c -o build/temp.linux-x86_64-3.11/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
    ----------------------------------------
ERROR: Command errored out with exit status 1: /home/pixarsart/PycharmProjects/video_slicing/venv/bin/python -u -c 'import io, os, sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-uljhxj0e/mysqlclient_bcf14369053645289dd7e777e689db38/setup.py'"'"'; __file__='"'"'/tmp/pip-install-uljhxj0e/mysqlclient_bcf14369053645289dd7e777e689db38/setup.py'"'"';f = getattr(tokenize, '"'"'open'"'"', open)(__file__) if os.path.exists(__file__) else io.StringIO('"'"'from setuptools import setup; setup()'"'"');code = f.read().replace('"'"'rn'"'"', '"'"'n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record /tmp/pip-record-x5dprlcj/install-record.txt --single-version-externally-managed --compile --install-headers /home/pixarsart/PycharmProjects/video_slicing/venv/include/site/python3.11/mysqlclient Check the logs for full command output.

Any guess, how to solve this problem?

I tried to explore different solutions on the internet but unable to resolve it. I also tried this stackoverflow answer. But didn’t get any luck.

Asked By: CyBug

||

Answers:

Try this solution:

You can use pymysql instead of mysqlclient

Install pymysql
pip install pymysql

Modify your init.py file

import pymysql

pymysql.install_as_MySQLdb()

You can start your project

Answered By: Muhammad Afzaal