Django - installing mysqlclient error: mysqlclient 1.3.13 or newer is required; you have 0.9.3

I guess your project uses pymysql instead of mysqlclient.

You can try to search the following code snippet in your project. If you find it, please try the following methods to fix this problem:

import pymysql
pymysql.install_as_MySQLdb()

Insert a line of code between these two to make it look like this:

import pymysql
pymysql.version_info = (1, 3, 13, "final", 0)
pymysql.install_as_MySQLdb()

Then try to start your project.

  • Why do I know you are using pymysql? Because 0.9.3 is just the latest version of pymysql.
  • Why use pymysql instead of mysqlclient for the project? Because it is easier to install. pymysql does not depend on system libraries, while mysqlclient relies on a series of system libraries such as libmysqlclient-dev.
  • Why is mysqlclient difficult to install and Django still uses it by default? Because mysqlclient is faster and performs better. So if your project has high performance requirements, I suggest you remove the compatible code above and install mysqlclient in your project. If you need help during the installation of mysqlclient, please refer to this link: How to install Python MySQLdb module using pip?, and ensure libssl-dev has been installed before pip install mysqlclient.

This is how I fixed it.

Go to your django/db/backends/mysql installation dir. Check your path in the error message.

I'm using pipenv so my path is:

/home/username/.local/share/virtualenvs/project-env/lib/python3.7/site-packages/django/db/backends/mysql

If you use traditional env your path would be:

<env_directory_name>/Lib/site-packages/django/db/base.py

Open file base.py and search for:

version = Database.version_info

Put a pass inside if and comment line:

raise ImproperlyConfigured('mysqlclient 1.3.13 or newer is required; you have %s.' % Database.version)

Like this.

if version < (1, 3, 13):
   pass
   '''
   raise ImproperlyConfigured(
       'mysqlclient 1.3.13 or newer is required; you have %s.'
       % Database.__version__
   )
   '''

Save, close this file and open operations.py.

Search for:

query = query.decode(errors='replace')

and change decode to encode

query = query.encode(errors='replace')

Now, try to run the server.

@edit

Until this answer, I found no other way to solve it. Today there are better ways to deal with this problem. This answer has a better approach.


I have had the same issue as lower version of mysqlclient was installed due to pymysql.

OS: Linux Mint 19.1

Python: 3.6.8

Django: 2.2.2

  1. Uninstall mysqlclient: pip3 uninstall mysqlclient
  2. Uninstall pymysql: pip3 uninstall pymysql
  3. Install mysqlclient: pip3 install mysqlclient