django.core.exceptions.ImproperlyConfigured: mysqlclient 1.3.3 or newer is required; you have 0.9.3.None

报错环境:

python=3.7,django=2.2,PyMySQL=0.9.3

抛出异常:

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

报错场景:

  1、启动django项目

  2、更新表:python manage.py makemigrations

报错原因:安装Django后不想折腾mysqlclient那堆库文件,直接装了pymysql替代mysqlclient。还是老办法,__init__.py 中patch一下:

import pymysql
pymysql.install_as_MySQLdb()

  启动项目出现以下异常:

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

  看来是Django2.2对mysqlclient的要求提高了: 1.3.13. 但pymysql的版本没有跟上。

看了下tracelog指向的异常抛出处的代码, 发现如下代码片段:

果然是有个版本判断并raise了异常,而且校验的是Database库的version_info属性。那pymysql中的version_onfo属性是怎么返回的呢?找到pymysql源码,发现如下片段:

解决办法:不管三七二十一,直接简单粗暴,干掉这个判断就完事了

这个异常到此解决完了,紧跟着又来一个报错:

  File "C:\Users\Administrator\AppData\Roaming\Python\Python37\site-packages\django\db\backends\mysql\operations.py", line 147, in last_executed_query
    query = query.decode(errors=‘replace‘)
AttributeError: ‘str‘ object has no attribute ‘decode‘

是的,py3默认str是unicode编码,通过encode方法编码成bytes类型,后者才有decode解码方法。点进去看源码,果然没错,没encode哪里来的decode。

 解决办法:

  到django的github去翻这个文件这个方法的最新/历史版本,结果最新master分支内容如下:force_str方法解决了这个问题,只需要把下面代码替换上面的代码就可以了

from django.utils.encoding import force_str
def last_executed_query(self, cursor, sql, params):
        # With MySQLdb, cursor objects have an (undocumented) "_executed"
        # attribute where the exact query sent to the database is saved.
        # See MySQLdb/cursors.py in the source distribution.
        # MySQLdb returns string, PyMySQL bytes.
        return force_str(getattr(cursor, ‘_executed‘, None), errors=‘replace‘)

至此,项目可以正常启动了,表更新也没问题。

该方案只是避开了版本判断,不确定Django2.2是否依赖mysqlclient的新特性,因此生产环境还是建议部署mysqlclient,而非pymysql。

  mysqlclient与pymysql区别是什么?

    mysqlclient是mysql官方提供的Python SDK,安装时依赖mysql-dev与python-dev相关的库函数完成当前系统平台的编译,因为使用大量C库,性能会比pymysql优秀得多。SDK更新和维护也有官方保障。

    pymysql是第三方在MySQL通信协议上实现的SDK,所有与数据库的交互都是通过MySQL独有的通信协议完成,性能上会比mysqlclient有劣势。且版本更新可能滞后,且维护不一定到位。但好处就是pip install安装很容易,不像mysqlclient先要把编译依赖的C库装好,在一些建档任务和开发环境快速搭建上有优势。

  

原文地址:https://www.cnblogs.com/aizhinong/p/12312103.html

时间: 2024-08-29 04:41:44

django.core.exceptions.ImproperlyConfigured: mysqlclient 1.3.3 or newer is required; you have 0.9.3.None的相关文章

关于报错:django.core.exceptions.ImproperlyConfigured: mysqlclient 1.3.3 or newer is required; you have 0.7.11.None

线上项目转到测试,setting连接本地数据库报错. 1 django.core.exceptions.ImproperlyConfigured: mysqlclient 1.3.3 or newer is required; you have 0.7.11.None Python安装路劲下的Python36-32\Lib\site-packages\django\db\backends\mysql\base.py文件下. 注释下面语句 1 if version < (1, 3, 3): 2 r

Django2.2连接mysql数据库出现django.core.exceptions.ImproperlyConfigured: mysqlclient 1.3.3 or newer is required; you have 0.7.11.None问题

在使用Django2.2开发的时候,想要使用mysql数据库,在settings.py文件中更改命令: DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': 'python', 'USER': "root", 'PASSWORD': "yhr2323214310", 'HOST': '', 'PORT': '' }} 再安装好pymysql,然后在__init__.py文件中

Django3 中遇到django.core.exceptions.ImproperlyConfigured mysqlclient 1.3.13 or newer is required; you have 0.9.3.异常的解决方案

转自:https://yuntianti.com/posts/fix-django3-mysqlclient-import-error/ Django 近期发布了3.0版本,其中首度支持了asyncio让人兴奋, 为此引入了新的网关接口协议ASGI.按异步IO的实现原理,即使使用ASGI替代WSGI部署Web应用,如果使用了一些同步库,肯定还是会阻塞IOLoop的,使应用失去异步特性.但是Django的应用场景很多是有状态的,不得不使用一些带状态的库和函数.比如在MySQL中执行事务,或一些前后

django.core.exceptions.ImproperlyConfigured: mysqlclient 1.3.13 or newer is required; you have 0.9.3. | Django报错

Django报错 | "django.core.exceptions.ImproperlyConfigured: mysqlclient 1.3.13 or newer is required; you have 0.9.3.问题解决方案 1 问题分析 django.core.exceptions.ImproperlyConfigured: mysqlclient 1.3.13 or newer is required; you have 0.9.3. 解读:django.core.except

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

异常汇总:https://www.cnblogs.com/dotnetcrazy/p/9192089.html 这个是Django对MySQLdb版本的限制,我们使用的是PyMySQL,所以不用管它 原文地址:https://www.cnblogs.com/dotnetcrazy/p/10779304.html

django2.2/mysql ImproperlyConfigured: mysqlclient 1.3.13 or newer is required; you have 0.9.3

报错环境 python=3.6.5,django=2.2,PyMySQL=0.9.3 …… django.core.exceptions.ImproperlyConfigured: mysqlclient 1.3.13 or newer is required; you have 0.9.3. 解决方法: Django连接MySQL时默认使用MySQLdb驱动,但MySQLdb不支持Python3,因此这里将MySQL驱动设置为pymysql,使用 pip install pymysql 进行安

Django启动时找不到mysqlclient处理 django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module. Did you install mysqlclient?

在使用Django2.0 并配置了mysql作为数据库时,启动报错: 报错1:找不到mysqlclient django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module. Did you install mysqlclient? 解决方法 项目(settings.py同级)目录中__init__.py中添加 import pymysql pymysql.install_as_MySQLdb() 报错2:版本检查

后台创建管理员 createsuperuser 时 报django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module. Did you install mysqlclient?

结果版本0.9.3 django.core.exceptions.ImproperlyConfigured: mysqlclient 1.3.13 or newer is required; you have 0.9.3. 需要pip指定版本号安装 结果清华源没有这个版本,换阿里云 pip install -i https://mirrors.aliyun.com/pypi/simple/ pymysql==1.3.13 阿里也没有,只能国外了 pip install jupyter -i ht

django.db.migrations.exceptions.MigrationSchemaMissing和raise ImproperlyConfigured(&#39;mysqlclient 1.3.13 or newer is required; you have %s.&#39; % Database.__version__)

1.使用Python3.7 + Django2.2 + MySQL 5.5 在执行(python manage.py migrate)命令时出现错误django.db.migrations.exceptions.MigrationSchemaMissing 原因是 所以,需要重新安装高版本的sql,安装好之后,问题就解决了 2.Django数据同步过程中遇到的问题: 1)raise ImproperlyConfigured('mysqlclient 1.3.13 or newer is requ