欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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

程序员文章站 2022-05-29 11:34:35
...

Pycharm2019.1.1初次配置mysql,不要用PyMySQL 0.9.3

涉及环境

  • PyCharm 2019.1.1
    Build #PY-191.6605.12, built on April 3, 2019
    JRE: 11.0.2+9-b159.34 amd64
    JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
  • Windows 10 1809
  • Django version 2.2.2
  • Python 3.7.1
  • mysqlclient 1.4.2.post1
  • MySQL Community Server 8.0.16

测试过程

PyCharm默认的Virtualenv用来隔离测试项目很不错。
在新建Django项目后,SQLite配置成mysql。

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'testdatabase',
        'USER': 'root',
        'PASSWORD': 'root',
        'HOST': '127.0.0.1',
        'PORT': '3306',
    }
}

shift+F10,我艹,出错了!

Watching for file changes with StatReloader
Exception in thread django-main-thread:
Traceback (most recent call last):
  File "D:\Exercises\PycharmProjects\Virtualenv\test620\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:\Program Files\Python37\lib\threading.py", line 917, in _bootstrap_inner
    self.run()
  File "C:\Program Files\Python37\lib\threading.py", line 865, in run
    self._target(*self._args, **self._kwargs)
  File "D:\Exercises\PycharmProjects\Virtualenv\test620\lib\site-packages\django\utils\autoreload.py", line 54, in wrapper
    fn(*args, **kwargs)
  File "D:\Exercises\PycharmProjects\Virtualenv\test620\lib\site-packages\django\core\management\commands\runserver.py", line 109, in inner_run
    autoreload.raise_last_exception()
  File "D:\Exercises\PycharmProjects\Virtualenv\test620\lib\site-packages\django\utils\autoreload.py", line 77, in raise_last_exception
    raise _exception[1]
  File "D:\Exercises\PycharmProjects\Virtualenv\test620\lib\site-packages\django\core\management\__init__.py", line 337, in execute
    autoreload.check_errors(django.setup)()
  File "D:\Exercises\PycharmProjects\Virtualenv\test620\lib\site-packages\django\utils\autoreload.py", line 54, in wrapper
    fn(*args, **kwargs)
  File "D:\Exercises\PycharmProjects\Virtualenv\test620\lib\site-packages\django\__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "D:\Exercises\PycharmProjects\Virtualenv\test620\lib\site-packages\django\apps\registry.py", line 114, in populate
    app_config.import_models()
  File "D:\Exercises\PycharmProjects\Virtualenv\test620\lib\site-packages\django\apps\config.py", line 211, in import_models
    self.models_module = import_module(models_module_name)
  File "C:\Program Files\Python37\lib\importlib\__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 728, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "D:\Exercises\PycharmProjects\Virtualenv\test620\lib\site-packages\django\contrib\auth\models.py", line 2, in <module>
    from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
  File "D:\Exercises\PycharmProjects\Virtualenv\test620\lib\site-packages\django\contrib\auth\base_user.py", line 47, in <module>
    class AbstractBaseUser(models.Model):
  File "D:\Exercises\PycharmProjects\Virtualenv\test620\lib\site-packages\django\db\models\base.py", line 117, in __new__
    new_class.add_to_class('_meta', Options(meta, app_label))
  File "D:\Exercises\PycharmProjects\Virtualenv\test620\lib\site-packages\django\db\models\base.py", line 321, in add_to_class
    value.contribute_to_class(cls, name)
  File "D:\Exercises\PycharmProjects\Virtualenv\test620\lib\site-packages\django\db\models\options.py", line 204, in contribute_to_class
    self.db_table = truncate_name(self.db_table, connection.ops.max_name_length())
  File "D:\Exercises\PycharmProjects\Virtualenv\test620\lib\site-packages\django\db\__init__.py", line 28, in __getattr__
    return getattr(connections[DEFAULT_DB_ALIAS], item)
  File "D:\Exercises\PycharmProjects\Virtualenv\test620\lib\site-packages\django\db\utils.py", line 201, in __getitem__
    backend = load_backend(db['ENGINE'])
  File "D:\Exercises\PycharmProjects\Virtualenv\test620\lib\site-packages\django\db\utils.py", line 110, in load_backend
    return import_module('%s.base' % backend_name)
  File "C:\Program Files\Python37\lib\importlib\__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "D:\Exercises\PycharmProjects\Virtualenv\test620\lib\site-packages\django\db\backends\mysql\base.py", line 20, in <module>
    ) from err
django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module.
Did you install mysqlclient?

这一看不是没有导入MySQLdb module么?

原因分析

很多教程中说python版本支持之类的,
导入的PyMySQL 0.9.3
配置里安装下。
再在Django的__init__.py中加入如下代码:

import pymysql
pymysql.install_as_MySQLdb()

成功了?却不是我。
我的却出现了以下错误。

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

感觉这是历史遗留了的最大的坑,

到底是什么鬼?

解决方案

不要搞PyMySQL,我就爱原生方法。

1. PyCharm的Settings里直接安装mysqlclient 1.4.2.post1。
2. shift+F10 测试下,搞定。

终于可以正常玩儿了。

附赠个Database面板连接MySQL时,需要在URL后面加上?开始的神奇字符,连不上的时候试试吧,完整URL如下:

jdbc:mysql://localhost:3306/youdatabasename?useUnicode=true&characterEncoding=UTF-8&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC