44

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.

Compoot
  • 2,227
  • 6
  • 31
  • 63

16 Answers16

85

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.
WisZhou
  • 1,369
  • 10
  • 8
  • 1
    If you are using centos7 yum install mysql-devel is required before pip install mysqlclient. – WisZhou Feb 10 '20 at 05:58
  • 8
    As of today, I found you need to update the version in this manner to `1.4.2`. ```pymysql.version_info = (1, 4, 2, "final", 0)``` – sfdurbano Oct 07 '20 at 13:54
60

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.

David
  • 3
  • 1
Diego Magalhães
  • 1,704
  • 9
  • 14
  • 6
    This worked but is there any other ways? I mean how to fix it on a real server in which we might not have access to these files.? – Krishnadas PC Jun 13 '19 at 12:50
  • 1
    @KrishnadasPC did you find any solutions? – nesalexy Jun 16 '19 at 11:10
  • 1
    No other solutions found for me. Due to this issue I have moved to nodejs and customisation for django seems very hard atleast for me. – Krishnadas PC Jun 16 '19 at 11:13
  • 1
    @KrishnadasPC A suggestion is, connect to your server through SSH and use VIM to change files. – Diego Magalhães Jun 26 '19 at 12:08
  • 3
    @KrishnadasPC real server that uses django will have access to these files. I am running django in a container and I bypassed this issue by replacing "Database.version_info" with "(2,0,0)" to skip if statement that causes raise. bash script looks like this: `sed -i "s/Database.version_info/(2,0,0)/g" /usr/local/lib/python3.6/site-packages/django/db/backends/mysql/base.py` –  Jul 17 '19 at 22:47
  • This didn't work for me. For some reason its calling a file that doesnt exist on my computer. I can't search it up but it is still being called and giving me this error. – Agent Lu Oct 05 '19 at 19:07
  • You should not fix by editing files, follow the below instructions https://stackoverflow.com/a/59591269/521586 which is far cleaner and resilient when Django is updated or transparent when someone is trying to understand your setup. – RuiDC May 22 '20 at 07:36
  • *pass* worked in base.py file but I didn't found the query line in operations.py file – Machindra Apr 16 '23 at 18:45
  • @Machindra the package was probably updated, that's why you're unable to find. Anyway, this answer was a workaround until a fix. – Diego Magalhães Apr 19 '23 at 15:31
8

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
Aftabul Islam
  • 454
  • 4
  • 10
6

this problem occurs when you create Django project in pycharm by default settings. It's caused by the default version of django is 2.2.6, so just downgrade the django version to 2.1.7, and error in the terminal is gone.

pip install Django==2.1.7

that's all!

NM666
  • 61
  • 1
  • 2
6

It worked fine with me just open settings.py and include

 import pymysql
 pymysql.version_info = (1, 4, 6, 'final', 0)
 pymysql.install_as_MySQLdb()
Sami
  • 95
  • 2
  • 6
3

Instead of edit internal configuration, try to upgrade the version of mysqlclient

pip3 install -U mysqlclient
fsalazar_sch
  • 348
  • 2
  • 6
  • 17
  • I tried to upgrade mysqlclient but the error is with pymysql dependency. Note that last pymysql release is exactly what django points error (0.9.3). – Diego Magalhães Aug 02 '19 at 14:01
2

The following solution works in Django 3.2.3 to get rid of the 'mysqlclient 1.3.13 or newer is required; you have 0.9.3' problem:

1) pip uninstall mysqlclient # remove old version 2) pip install mysqlclient==1.3.13 3) comment or remove the old workaround in __init__.py of the main app.

# workaround django.core.exceptions.ImproperlyConfigured: #Error loading MySQLdb module. #Did you install mysqlclient? #import pymysql #pymysql.install_as_MySQLdb()

Luis Valverde
  • 431
  • 4
  • 5
1
  1. Install wheel:

    pip install wheel

  2. Download file that you want from here: https://www.lfd.uci.edu/~gohlke/pythonlibs/#mysql-python

  3. Move to your Downloads directory (I guess it'll be cd Downloads)

  4. Install downloaded file with: pip install <file name>

WARNING!! You CANT edit name of downloaded .whl file. It contains some information what is required to install.

Dolidod Teethtard
  • 553
  • 1
  • 7
  • 22
  • 1
    Thankyou - good suggestion as it reveals the oddity of this. On uninstalling and installing again it states "Requirement already satisfied: mysqlclient in c:\users\marvins\envs\djangottio\lib\site-packages (1.4.2)" but the following error still comes up on trying to run the inspect command: ImproperlyConfigured: mysqlclient 1.3.13 or newer is required; you have 0.9.3. – Compoot Apr 12 '19 at 18:52
  • Do I need to come out of the virtual environment? – Compoot Apr 12 '19 at 18:53
  • I tried it again making a new VENV ....on trying to install "pip install mysqlclient" it now came up with this error! error: Microsoft Visual C++ 14.0 is required. Get it with "Microsoft Visual C++ Build Tools": https://visualstudio.microsoft.com/downloads/ – Compoot Apr 12 '19 at 18:57
  • I think virtual enviroment doesnt matter here. But you can try this (tell me if it works so i will edit my answer): pip install wheel then download .whl that is interesing you from here: https://www.lfd.uci.edu/~gohlke/pythonlibs/#mysql-python. And then cd downloads && (If you are using python3): pip install mysqlclient-1.3.8-cp36-cp36m-win_amd64.whl (if you are using python2: pip install mysqlclient-1.3.8-cp27-cp27m-win_amd64.whl) – Dolidod Teethtard Apr 12 '19 at 18:58
  • I don't understand what you are asking me to do. If you read my update, you'll see I've already tried installing a wheel. Uninstalling pip and also re-installing a wheel. Still, the same error – Compoot Apr 12 '19 at 18:59
  • I will write it in answer, i think i am sure that will work – Dolidod Teethtard Apr 12 '19 at 19:01
  • I am not very familiar with windows... But i think you can just remove directory that is showed in error... The worst thing what can happen is that you will must re-install python and pip. – Dolidod Teethtard Apr 12 '19 at 19:09
  • I've already done what you've suggested so cannot accept it as an answer. (see my question and update!). It doesn't work. Thanks though – Compoot Apr 12 '19 at 19:13
0

I had a similar issue, I was getting

"django.core.exceptions.ImproperlyConfigured: mysqlclient 1.3.13 or newer is required; you have 1.3.12."

So I went to the C:\Users\username\Envs\env\Lib\site-packages\django\db\backends\mysql, opened base.py and changed that line...

if version < (1, 3, 13): 

to...

if version < (1, 3, 10):

which is a version lesser than mine and it worked.

Matthijs
  • 2,483
  • 5
  • 22
  • 33
0

If it's not critical for you, as a variant, just downgrade your django framework from 2.2.2(for example) to 2.1.

0

I was finding a solution for this issue from a long time. I was using Ubuntu 18.04 and found this solution to be useful for python version 3.7.5

Step 1. Install libpython3.7-dev via sudo apt-get install

> sudo apt-get install libpython3.7-dev

Step 2: Install mysqlclient

> python3 -m pip install mysqlclient==1.4.6
  • I have the same problem now and I still can't fix it. I've got an error while executing second command: `Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-install-z_tudjri/mysqlclient/` – nick Apr 03 '20 at 10:59
  • Your setuptools might be outdated. Run pip install --upgrade setuptools and check if this works – girishsaraf03 Apr 03 '20 at 12:03
  • yes, it's true. I had just updated setuptools package and successfully installed mysqlclient package. thx – nick Apr 03 '20 at 12:15
0

If you are working with Django on ubuntu, i will suggest you do the following to resolve the issue than editing django specific configuration files

run the following commands in the terminal

  1. sudo apt-get install python3-dev default-libmysqlclient-dev build-essential

  2. pip install mysqlclient

0

Maybe your Django project uses pymysql, instead of upgrading mysqlclient, just do

pip install pymysql -U

This works for me ! :D

  • Please don't add "thank you" as an answer. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation), you will be able to [vote up questions and answers](https://stackoverflow.com/help/privileges/vote-up) that you found helpful. - [From Review](/review/late-answers/32938864) – claudius Oct 18 '22 at 16:25
0

In my case i just deleted all the pymysql code in either settings.py or init.py__. Left out mysqlclient installed. The problem was solved

HB FL3X
  • 1
  • 2
-1

There are the same issue on github. Solution that perfectly fits for me is here.

I just quote an author:

I first pip uninstall pymysql (as it seems it doesn't work anymore)

Then I've used pip install mysqlclient

Last, I checked all my "pymysql" imports and deleted them.

Be sure to go and hit a like button on the comment in the link

Community
  • 1
  • 1
B1Z0N
  • 133
  • 7
-1

Go to your virtual environment directory like mine:

C:\Users\user\.virtualenvs\RPA-Orchestrator-Backend-GwIL98hN\Lib\site-packages\django\db\backends\mysql\base.py

Here you might edit one line to avoid your error in the base.py file,

version = Database.version_info
if version < (0, 9, 3):
   raise ImproperlyConfigured('mysqlclient 1.3.13 or newer is required; you have %s.' % Database.__version__)

Then your problem might solve.

Tomer Shetah
  • 8,413
  • 7
  • 27
  • 35
Shaonsani
  • 178
  • 8