(Solved) Failed to install mysqlclient

I have tried several solutions posted here but no luck. However, I’m able to connect to mysql with the command ‘mysql -u root - p’ without any problem, after changing the path by following the solution provided in this forum.

(storefront-yT6GZW6h) C:\Users\Pua Hoover\Desktop\storefront>mysql -u root -p
Enter password: *********
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 49
Server version: 8.0.28 MySQL Community Server - GPL

Copyright (c) 2000, 2022, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

I’m using Window 10 Home edition and python 3.8.2 in the pipenv. The error msg I received as below:

(storefront-yT6GZW6h) C:\Users\Pua Hoover\Desktop\storefront>pipenv install mysqlclient
Installing mysqlclient...
Error:  An error occurred while installing mysqlclient!
Error text: Collecting mysqlclient
  Using cached mysqlclient-2.1.0.tar.gz (87 kB)
  Preparing metadata (setup.py): started
  Preparing metadata (setup.py): finished with status 'done'
Building wheels for collected packages: mysqlclient
  Building wheel for mysqlclient (setup.py): started
  Building wheel for mysqlclient (setup.py): finished with status 'error'
  Running setup.py clean for mysqlclient
Failed to build mysqlclient
Installing collected packages: mysqlclient
  Running setup.py install for mysqlclient: started
  Running setup.py install for mysqlclient: finished with status 'error'

  error: subprocess-exited-with-error

  × python setup.py bdist_wheel did not run successfully.
  │ exit code: 1
  ╰─> [31 lines of output]
      running bdist_wheel
      running build
      running build_py
      creating build
      creating build\lib.win32-3.8
      creating build\lib.win32-3.8\MySQLdb
      copying MySQLdb\__init__.py -> build\lib.win32-3.8\MySQLdb
      copying MySQLdb\_exceptions.py -> build\lib.win32-3.8\MySQLdb
      copying MySQLdb\connections.py -> build\lib.win32-3.8\MySQLdb
      copying MySQLdb\converters.py -> build\lib.win32-3.8\MySQLdb
      copying MySQLdb\cursors.py -> build\lib.win32-3.8\MySQLdb
      copying MySQLdb\release.py -> build\lib.win32-3.8\MySQLdb
      copying MySQLdb\times.py -> build\lib.win32-3.8\MySQLdb
      creating build\lib.win32-3.8\MySQLdb\constants
      copying MySQLdb\constants\__init__.py -> build\lib.win32-3.8\MySQLdb\constants
      copying MySQLdb\constants\CLIENT.py -> build\lib.win32-3.8\MySQLdb\constants
      copying MySQLdb\constants\CR.py -> build\lib.win32-3.8\MySQLdb\constants
      copying MySQLdb\constants\ER.py -> build\lib.win32-3.8\MySQLdb\constants
      copying MySQLdb\constants\FIELD_TYPE.py -> build\lib.win32-3.8\MySQLdb\constants
      copying MySQLdb\constants\FLAG.py -> build\lib.win32-3.8\MySQLdb\constants
      warning: build_py: byte-compiling is disabled, skipping.

      running build_ext
      building 'MySQLdb._mysql' extension
      creating build\temp.win32-3.8
      creating build\temp.win32-3.8\Release
      creating build\temp.win32-3.8\Release\MySQLdb
      "C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.14.26428\bin\HostX86\x86\cl.exe" /c /nologo /O2 /W3 /GL /DNDEBUG /MD -Dversion_info=(2,1,0,'final',0) -D__version__=2.1.0 "-IC:\Program Files (x86)\MariaDB\MariaDB Connector C\include\mariadb" "-IC:\Program Files (x86)\MariaDB\MariaDB Connector C\include" "-IC:\Users\Pua Hoover\.virtualenvs\storefront-yT6GZW6h\include" "-Ic:\users\pua hoover\appdata\local\programs\python\python38-32\include" "-Ic:\users\pua hoover\appdata\local\programs\python\python38-32\Include" "-IC:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.14.26428\include" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.16299.0\ucrt" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.16299.0\shared" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.16299.0\um" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.16299.0\winrt" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.16299.0\cppwinrt" /TcMySQLdb/_mysql.c /Fobuild\temp.win32-3.8\Release\MySQLdb/_mysql.obj
      _mysql.c
      MySQLdb/_mysql.c(29): fatal error C1083: Cannot open include file: 'mysql.h': No such file or directory
      error: command 'C:\\Program Files (x86)\\Microsoft Visual Studio\\2017\\Community\\VC\\Tools\\MSVC\\14.14.26428\\bin\\HostX86\\x86\\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
  error: subprocess-exited-with-error

  × Running setup.py install for mysqlclient did not run successfully.
  │ exit code: 1
  ╰─> [33 lines of output]
      running install
      C:\Users\Pua Hoover\.virtualenvs\storefront-yT6GZW6h\lib\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.win32-3.8
      creating build\lib.win32-3.8\MySQLdb
      copying MySQLdb\__init__.py -> build\lib.win32-3.8\MySQLdb
      copying MySQLdb\_exceptions.py -> build\lib.win32-3.8\MySQLdb
      copying MySQLdb\connections.py -> build\lib.win32-3.8\MySQLdb
      copying MySQLdb\converters.py -> build\lib.win32-3.8\MySQLdb
      copying MySQLdb\cursors.py -> build\lib.win32-3.8\MySQLdb
      copying MySQLdb\release.py -> build\lib.win32-3.8\MySQLdb
      copying MySQLdb\times.py -> build\lib.win32-3.8\MySQLdb
      creating build\lib.win32-3.8\MySQLdb\constants
      copying MySQLdb\constants\__init__.py -> build\lib.win32-3.8\MySQLdb\constants
      copying MySQLdb\constants\CLIENT.py -> build\lib.win32-3.8\MySQLdb\constants
      copying MySQLdb\constants\CR.py -> build\lib.win32-3.8\MySQLdb\constants
      copying MySQLdb\constants\ER.py -> build\lib.win32-3.8\MySQLdb\constants
      copying MySQLdb\constants\FIELD_TYPE.py -> build\lib.win32-3.8\MySQLdb\constants
      copying MySQLdb\constants\FLAG.py -> build\lib.win32-3.8\MySQLdb\constants
      warning: build_py: byte-compiling is disabled, skipping.

      running build_ext
      building 'MySQLdb._mysql' extension
      creating build\temp.win32-3.8
      creating build\temp.win32-3.8\Release
      creating build\temp.win32-3.8\Release\MySQLdb
      "C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.14.26428\bin\HostX86\x86\cl.exe" /c /nologo /O2 /W3 /GL /DNDEBUG /MD -Dversion_info=(2,1,0,'final',0) -D__version__=2.1.0 "-IC:\Program Files (x86)\MariaDB\MariaDB Connector C\include\mariadb" "-IC:\Program Files (x86)\MariaDB\MariaDB Connector C\include" "-IC:\Users\Pua Hoover\.virtualenvs\storefront-yT6GZW6h\include" "-Ic:\users\pua hoover\appdata\local\programs\python\python38-32\include" "-Ic:\users\pua hoover\appdata\local\programs\python\python38-32\Include" "-IC:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.14.26428\include" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.16299.0\ucrt" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.16299.0\shared" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.16299.0\um" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.16299.0\winrt" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.16299.0\cppwinrt" /TcMySQLdb/_mysql.c /Fobuild\temp.win32-3.8\Release\MySQLdb/_mysql.obj
      _mysql.c
      MySQLdb/_mysql.c(29): fatal error C1083: Cannot open include file: 'mysql.h': No such file or directory
      error: command 'C:\\Program Files (x86)\\Microsoft Visual Studio\\2017\\Community\\VC\\Tools\\MSVC\\14.14.26428\\bin\\HostX86\\x86\\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.

Update: I downloaded the wheel from Python Extension Packages for Windows - Christoph Gohlke and installed it. It seems like I successfully installed mysqlclient.

(storefront-yT6GZW6h) C:\Users\Pua Hoover\Desktop\storefront>pipenv install mysqlclient-1.4.6-cp38-cp38-win32.whl
Installing mysqlclient-1.4.6-cp38-cp38-win32.whl...
Adding mysqlclient to Pipfile's [packages]...
Installation Succeeded
Installing dependencies from Pipfile.lock (8c9880)...
  ================================ 1/1 - 00:00:00
(storefront-yT6GZW6h) C:\Users\Pua Hoover\Desktop\storefront>pip show mysqlclient
Name: mysqlclient
Version: 1.4.6
Summary: Python interface to MySQL
Home-page: https://github.com/PyMySQL/mysqlclient-python
Author: Inada Naoki
Author-email: songofacandy@gmail.com
License: GPL
Location: c:\users\pua hoover\.virtualenvs\storefront-yt6gzw6h\lib\site-packages
Requires:
Required-by:

However, when I change the database from sqlite to mysql in setting.py and runserver according to the tutorial, I got the errors below:

(storefront-yT6GZW6h) C:\Users\Pua Hoover\Desktop\storefront>python manage.py runserver
Watching for file changes with StatReloader
Exception in thread django-main-thread:
Traceback (most recent call last):
  File "C:\Users\Pua Hoover\.virtualenvs\storefront-yT6GZW6h\lib\site-packages\django\db\backends\mysql\base.py", line 15, in <module>
    import MySQLdb as Database
ModuleNotFoundError: No module named 'MySQLdb'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "c:\users\pua hoover\appdata\local\programs\python\python38-32\lib\threading.py", line 932, in _bootstrap_inner
    self.run()
  File "c:\users\pua hoover\appdata\local\programs\python\python38-32\lib\threading.py", line 870, in run
    self._target(*self._args, **self._kwargs)
  File "C:\Users\Pua Hoover\.virtualenvs\storefront-yT6GZW6h\lib\site-packages\django\utils\autoreload.py", line 64, in wrapper
    fn(*args, **kwargs)
  File "C:\Users\Pua Hoover\.virtualenvs\storefront-yT6GZW6h\lib\site-packages\django\core\management\commands\runserver.py", line 125, in inner_run
    autoreload.raise_last_exception()
  File "C:\Users\Pua Hoover\.virtualenvs\storefront-yT6GZW6h\lib\site-packages\django\utils\autoreload.py", line 87, in raise_last_exception
    raise _exception[1]
  File "C:\Users\Pua Hoover\.virtualenvs\storefront-yT6GZW6h\lib\site-packages\django\core\management\__init__.py", line 398, in execute
    autoreload.check_errors(django.setup)()
  File "C:\Users\Pua Hoover\.virtualenvs\storefront-yT6GZW6h\lib\site-packages\django\utils\autoreload.py", line 64, in wrapper
    fn(*args, **kwargs)
  File "C:\Users\Pua Hoover\.virtualenvs\storefront-yT6GZW6h\lib\site-packages\django\__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "C:\Users\Pua Hoover\.virtualenvs\storefront-yT6GZW6h\lib\site-packages\django\apps\registry.py", line 116, in populate
    app_config.import_models()
  File "C:\Users\Pua Hoover\.virtualenvs\storefront-yT6GZW6h\lib\site-packages\django\apps\config.py", line 304, in import_models
    self.models_module = import_module(models_module_name)
  File "c:\users\pua hoover\appdata\local\programs\python\python38-32\lib\importlib\__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 783, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "C:\Users\Pua Hoover\.virtualenvs\storefront-yT6GZW6h\lib\site-packages\django\contrib\auth\models.py", line 3, in <module>
    from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
  File "C:\Users\Pua Hoover\.virtualenvs\storefront-yT6GZW6h\lib\site-packages\django\contrib\auth\base_user.py", line 49, in <module>
    class AbstractBaseUser(models.Model):
  File "C:\Users\Pua Hoover\.virtualenvs\storefront-yT6GZW6h\lib\site-packages\django\db\models\base.py", line 141, in __new__
    new_class.add_to_class("_meta", Options(meta, app_label))
  File "C:\Users\Pua Hoover\.virtualenvs\storefront-yT6GZW6h\lib\site-packages\django\db\models\base.py", line 369, in add_to_class
    value.contribute_to_class(cls, name)
  File "C:\Users\Pua Hoover\.virtualenvs\storefront-yT6GZW6h\lib\site-packages\django\db\models\options.py", line 235, in contribute_to_class
    self.db_table, connection.ops.max_name_length()
  File "C:\Users\Pua Hoover\.virtualenvs\storefront-yT6GZW6h\lib\site-packages\django\utils\connection.py", line 15, in __getattr__
    return getattr(self._connections[self._alias], item)
  File "C:\Users\Pua Hoover\.virtualenvs\storefront-yT6GZW6h\lib\site-packages\django\utils\connection.py", line 62, in __getitem__
    conn = self.create_connection(alias)
  File "C:\Users\Pua Hoover\.virtualenvs\storefront-yT6GZW6h\lib\site-packages\django\db\utils.py", line 208, in create_connection
    backend = load_backend(db["ENGINE"])
  File "C:\Users\Pua Hoover\.virtualenvs\storefront-yT6GZW6h\lib\site-packages\django\db\utils.py", line 113, in load_backend
    return import_module("%s.base" % backend_name)
  File "c:\users\pua hoover\appdata\local\programs\python\python38-32\lib\importlib\__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "C:\Users\Pua Hoover\.virtualenvs\storefront-yT6GZW6h\lib\site-packages\django\db\backends\mysql\base.py", line 17, in <module>
    raise ImproperlyConfigured(
django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module.
Did you install mysqlclient?

Managed to find the solution from stackoverflow:

Hope it helps. :slight_smile:

It helped me too. I believe all M1 chips have to go with this solution at this point. I have tested two m1 chip macbook FYI.

It helped me too, I used MacOS too. Thank you so much

Simple Solution:
If you are not able to connect to mysql with the command mysql -u root -p then you need to add mysql to path.
After adding mysql to path go ahead and install mysqlclient. if you are having troubles installing mysqlclient or you installed it but Django doesn’t recognise it, then install pymysql, add to init.py and settings.py of your project:

import pymysql
pymysql.install_as_MySQLdb()

and it should work( no need to install mysqlclient).

1 Like

Thanks so much, my friend! :hugs::palms_up_together:t6: