Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I've trawled the forums but cannot find an answer or even any documentation on this. Trying to re-create a site like www.testandtrack.io

On running the command:

python manage.py inspectdb

I get the error:

mysqlclient 1.3.13 or newer is required; you have 0.9.3

I have tried all the suggested fixes including: -upgrading pip -installing a different wheel (32 bit instead of 64), namely mysqlclient-1.4.2-cp37-cp37m-win32.whl with the command pip install mysqlclient-1.4.2-cp37-cp37m-win32.whl (this works fine without an error but doesn't do the job required!)

My objective is to simply connect a legacy mysql database (running inside of XAMPP and myphpadmin) to Django. I've followed the documentation which misses out the need to install mysqlclient, and have got stuck at this point.

like image 320
Compoot Avatar asked Apr 12 '19 18:04

Compoot


2 Answers

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.
like image 160
WisZhou Avatar answered Oct 24 '22 11:10

WisZhou


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.

like image 57
Diego Magalhães Avatar answered Oct 24 '22 10:10

Diego Magalhães