最新消息:20210917 已从crifan.com换到crifan.org

【部分解决】mac中运行Django报错:django.core.exceptions.ImproperlyConfigured mysqlclient 1.3.13 or newer is required you have 0.9.3

Mac crifan 524浏览 0评论
折腾:
【未解决】本地搭建和部署运行Django项目
期间,mac本地已安装好pipenv环境,然后去本地运行Django去调试:
python manage.py runserver 0.0.0.0:8000
结果报错:
✗ python manage.py runserver 0.0.0.0:8000
Watching for file changes with StatReloader
Exception in thread django-main-thread:
Traceback (most recent call last):
  File "/usr/local/Cellar/python/3.7.3/Frameworks/Python.framework/Versions/3.7/lib/python3.7/threading.py", line 917, in _bootstrap_inner
    self.run()
  File "/usr/local/Cellar/python/3.7.3/Frameworks/Python.framework/Versions/3.7/lib/python3.7/threading.py", line 865, in run
    self._target(*self._args, **self._kwargs)
  File "/Users/crifan/.local/share/virtualenvs/xxx-felNIzdJ/lib/python3.7/site-packages/django/utils/autoreload.py", line 54, in wrapper
    fn(*args, **kwargs)
  File "/Users/crifan/.local/share/virtualenvs/xxx-felNIzdJ/lib/python3.7/site-packages/django/core/management/commands/runserver.py", line 109, in inner_run
    autoreload.raise_last_exception()
  File "/Users/crifan/.local/share/virtualenvs/xxx-felNIzdJ/lib/python3.7/site-packages/django/utils/autoreload.py", line 77, in raise_last_exception
    raise _exception[1]
  File "/Users/crifan/.local/share/virtualenvs/xxx-felNIzdJ/lib/python3.7/site-packages/django/core/management/__init__.py", line 337, in execute
    autoreload.check_errors(django.setup)()
  File "/Users/crifan/.local/share/virtualenvs/xxx-felNIzdJ/lib/python3.7/site-packages/django/utils/autoreload.py", line 54, in wrapper
    fn(*args, **kwargs)
  File "/Users/crifan/.local/share/virtualenvs/xxx-felNIzdJ/lib/python3.7/site-packages/django/__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/Users/crifan/.local/share/virtualenvs/xxx-felNIzdJ/lib/python3.7/site-packages/django/apps/registry.py", line 114, in populate
    app_config.import_models()
  File "/Users/crifan/.local/share/virtualenvs/xxx-felNIzdJ/lib/python3.7/site-packages/django/apps/config.py", line 211, in import_models
    self.models_module = import_module(models_module_name)
  File "/Users/crifan/.local/share/virtualenvs/xxx-felNIzdJ/lib/python3.7/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 "/Users/crifan/.local/share/virtualenvs/xxx-felNIzdJ/lib/python3.7/site-packages/django/contrib/auth/models.py", line 2, in <module>
    from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
  File "/Users/crifan/.local/share/virtualenvs/xxx-felNIzdJ/lib/python3.7/site-packages/django/contrib/auth/base_user.py", line 47, in <module>
    class AbstractBaseUser(models.Model):
  File "/Users/crifan/.local/share/virtualenvs/xxx-felNIzdJ/lib/python3.7/site-packages/django/db/models/base.py", line 117, in __new__
    new_class.add_to_class('_meta', Options(meta, app_label))
  File "/Users/crifan/.local/share/virtualenvs/xxx-felNIzdJ/lib/python3.7/site-packages/django/db/models/base.py", line 321, in add_to_class
    value.contribute_to_class(cls, name)
  File "/Users/crifan/.local/share/virtualenvs/xxx-felNIzdJ/lib/python3.7/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 "/Users/crifan/.local/share/virtualenvs/xxx-felNIzdJ/lib/python3.7/site-packages/django/db/__init__.py", line 28, in __getattr__
    return getattr(connections[DEFAULT_DB_ALIAS], item)
  File "/Users/crifan/.local/share/virtualenvs/xxx-felNIzdJ/lib/python3.7/site-packages/django/db/utils.py", line 201, in __getitem__
    backend = load_backend(db['ENGINE'])
  File "/Users/crifan/.local/share/virtualenvs/xxx-felNIzdJ/lib/python3.7/site-packages/django/db/utils.py", line 110, in load_backend
    return import_module('%s.base' % backend_name)
  File "/Users/crifan/.local/share/virtualenvs/xxx-felNIzdJ/lib/python3.7/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "/Users/crifan/.local/share/virtualenvs/xxx-felNIzdJ/lib/python3.7/site-packages/django/db/backends/mysql/base.py", line 36, in <module>
    raise ImproperlyConfigured('mysqlclient 1.3.13 or newer is required; you have %s.' % Database.__version__)
django.core.exceptions.ImproperlyConfigured: mysqlclient 1.3.13 or newer is required; you have 0.9.3.
django.core.exceptions.ImproperlyConfigured: mysqlclient 1.3.13 or newer is required; you have 0.9.3.
django2.2/mysql ImproperlyConfigured: mysqlclient 1.3.13 or newer is required; you have 0.9.3 – Hayley-L – CSDN博客
但是自己此处
app/__init__.py
已经有
import pymysql
pymysql.install_as_MySQLdb()
且Pipfile
中已有:
[packages]
django = "*"
pymysql = "*"
django.core.exceptions.ImproperlyConfigured: mysqlclient 1.3.3 or newer is required; you have 0.7.11 – 潜行100的博客 – CSDN博客
【填坑】python3 使用django连接数据库报ImproperlyConfigured – 简书
python manage.py inspectdb
也是输出同样错误
Python3.6 用Django连接MySQL – 我的大金金 – 博客园
其做法还是去直接注释掉:
site-packages\Django-2.0-py3.6.egg\django\db\backends\mysql
中的:
if version < (1, 3, 3):
     raise ImproperlyConfigured("mysqlclient 1.3.3 or newer is required; you have %s" % Database.__version__) 
感觉不妥。
python – Have a problem using Django 2.2 with PyMySQL – Stack Overflow
看看当前django版本:
✗ pipenv graph
Django==2.2.2
  - pytz [required: Any, installed: 2019.1]
  - sqlparse [required: Any, installed: 0.3.0]
imgkit==1.0.2
Jinja2==2.10.1
  - MarkupSafe [required: >=0.23, installed: 1.1.1]
Pillow==6.0.0
pycrypto==2.6.1
PyMySQL==0.9.3
requests==2.22.0
  - certifi [required: >=2017.4.17, installed: 2019.3.9]
  - chardet [required: >=3.0.2,<3.1.0, installed: 3.0.4]
  - idna [required: >=2.5,<2.9, installed: 2.8]
  - urllib3 [required: >=1.21.1,<1.26,!=1.25.1,!=1.25.0, installed: 1.25.3]
xlwt==1.3.0
此处是Django 2.2.2
PyMySQL 0.9.3
Error loading MySQLdb Module ‘Did you install mysqlclient or MySQL-python?’ – Stack Overflow
“Then, edit the __init__.py file in your project origin dir(the same as settings.py)”
去找到此处的
pywe_django/settings.py
同目录下的
pywe_django/__init__.py
加上:
import pymysql
pymysql.install_as_MySQLdb()
结果:
问题依旧。
重新安装了pymsql,版本还是之前的:
 ✗ pipenv install pymysql
Installing pymysql…
Adding pymysql to Pipfile's [packages]…
✔ Installation Succeeded
Installing dependencies from Pipfile.lock (0dd26f)…
  🐍   ▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉ 15/15 — 00:00:06
➜  xxx git:(master) ✗ pipenv graph
Django==2.2.2
  - pytz [required: Any, installed: 2019.1]
  - sqlparse [required: Any, installed: 0.3.0]
imgkit==1.0.2
Jinja2==2.10.1
  - MarkupSafe [required: >=0.23, installed: 1.1.1]
Pillow==6.0.0
pycrypto==2.6.1
PyMySQL==0.9.3
requests==2.22.0
  - certifi [required: >=2017.4.17, installed: 2019.3.9]
  - chardet [required: >=3.0.2,<3.1.0, installed: 3.0.4]
  - idna [required: >=2.5,<2.9, installed: 2.8]
  - urllib3 [required: >=1.21.1,<1.26,!=1.25.1,!=1.25.0, installed: 1.25.3]
xlwt==1.3.0
去试试:
➜  xxx git:(master) ✗ pipenv install mysqlclient
Installing mysqlclient…
Adding mysqlclient to Pipfile's [packages]…
✔ Installation Succeeded
Pipfile.lock (966ad1) out of date, updating to (0dd26f)…
Locking [dev-packages] dependencies…
Locking [packages] dependencies…
✔ Success!
Updated Pipfile.lock (966ad1)!
Installing dependencies from Pipfile.lock (966ad1)…
  🐍   ▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉ 16/16 — 0
➜  xxx git:(master) ✗ pipenv graph
Django==2.2.2
  - pytz [required: Any, installed: 2019.1]
  - sqlparse [required: Any, installed: 0.3.0]
imgkit==1.0.2
Jinja2==2.10.1
  - MarkupSafe [required: >=0.23, installed: 1.1.1]
mysqlclient==1.4.2.post1
Pillow==6.0.0
pycrypto==2.6.1
PyMySQL==0.9.3
requests==2.22.0
  - certifi [required: >=2017.4.17, installed: 2019.3.9]
  - chardet [required: >=3.0.2,<3.1.0, installed: 3.0.4]
  - idna [required: >=2.5,<2.9, installed: 2.8]
  - urllib3 [required: >=1.21.1,<1.26,!=1.25.1,!=1.25.0, installed: 1.25.3]
xlwt==1.3.0
结果,问题依旧。
再去把:
pywe_django/__init__.py
app/__init__.py
都注释掉:
# import pymysql
# pymysql.install_as_MySQLdb()
结果:
就可以了。
【总结】
此处mac中本地运行Django报错:
django.core.exceptions.ImproperlyConfigured: mysqlclient 1.3.13 or newer is required; you have 0.9.3.
解决办法是:
去掉之前的:
app/__init__.py
中的:
import pymysql
pymysql.install_as_MySQLdb()
比如变成注释掉:
# import pymysql
# pymysql.install_as_MySQLdb()
然后安装mysqlclient
pipenv install mysqlclient
就可以了。
-》或许内部此时只是使用了mysqlclient
而不是pymysql
-》但先不管了,至少mac本地可以调试了。
【后记】
Compatibility with Django 2.2 · Issue #790 · PyMySQL/PyMySQL
此处是:
生产环境中,目前Django已经通过:
import pymysql
pymysql.install_as_MySQLdb()
在用pymysql了。
感觉是:
如果生产环境中,根据上述改动,估计就改用mysqlclient了
但是
“mysqlclient returns bytes object
PyMySQL returns str object”
感觉不兼容啊,容易出问题啊
另外试了试,mac此处
app/__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
另外,再去在线服务器中看看此处使用的版本:
root@VM-158-80-ubuntu:/var/log/mysql# pip3 show django
---
Metadata-Version: 2.1
Name: Django
Version: 2.1
Summary: A high-level Python Web framework that encourages rapid development and clean, pragmatic design.
Home-page: https://www.djangoproject.com/
Author: Django Software Foundation
Author-email: foundation@djangoproject.com
Installer: pip
License: BSD
Location: /usr/local/lib/python3.5/dist-packages
Requires: pytz
Classifiers:
  Development Status :: 5 - Production/Stable
  Environment :: Web Environment
  Framework :: Django
  Intended Audience :: Developers
  License :: OSI Approved :: BSD License
  Operating System :: OS Independent
  Programming Language :: Python
  Programming Language :: Python :: 3
  Programming Language :: Python :: 3.5
  Programming Language :: Python :: 3.6
  Programming Language :: Python :: 3.7
  Programming Language :: Python :: 3 :: Only
  Topic :: Internet :: WWW/HTTP
  Topic :: Internet :: WWW/HTTP :: Dynamic Content
  Topic :: Internet :: WWW/HTTP :: WSGI
  Topic :: Software Development :: Libraries :: Application Frameworks
  Topic :: Software Development :: Libraries :: Python Modules
Entry-points:
  [console_scripts]
  django-admin = django.core.management:execute_from_command_line
You are using pip version 8.1.1, however version 19.1.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
root@VM-158-80-ubuntu:/var/log/mysql# pip3 show mysqlclient
You are using pip version 8.1.1, however version 19.1.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
root@VM-158-80-ubuntu:/var/log/mysql# pip3 show pymysql
---
Metadata-Version: 2.1
Name: PyMySQL
Version: 0.9.2
Summary: Pure Python MySQL Driver
Home-page: https://github.com/PyMySQL/PyMySQL/
Author: yutaka.matsubara
Author-email: yutaka.matsubara@gmail.com
Installer: pip
License: "MIT"
Location: /usr/local/lib/python3.5/dist-packages
Requires: cryptography
Classifiers:
  Development Status :: 5 - Production/Stable
  Programming Language :: Python :: 2
  Programming Language :: Python :: 2.7
  Programming Language :: Python :: 3
  Programming Language :: Python :: 3.4
  Programming Language :: Python :: 3.5
  Programming Language :: Python :: 3.6
  Programming Language :: Python :: 3.7
  Programming Language :: Python :: Implementation :: CPython
  Programming Language :: Python :: Implementation :: PyPy
  Intended Audience :: Developers
  License :: OSI Approved :: MIT License
  Topic :: Database
You are using pip version 8.1.1, however version 19.1.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
root@VM-158-80-ubuntu:/var/log/mysql# 
可见:
Django 2.1
pymysql 0.9.2
Python是3.5.2
然后再去看看其有没有修改代码,忽略报错
其没有修改:
/usr/local/lib/python3.5/dist-packages/django/db/backends/mysql/base.py
中版本检测代码还是在的:
version = Database.version_info
if version < (1, 3, 7):
    raise ImproperlyConfigured('mysqlclient 1.3.7 or newer is required; you have %s.' % Database.__version__)
所以,mac本地还是尽量要去在不改变代码的前提下去跑起来
不过想到了,可以把另外一个Django项目去部署到服务器上,用pipenv,看看运行是否会出错

转载请注明:在路上 » 【部分解决】mac中运行Django报错:django.core.exceptions.ImproperlyConfigured mysqlclient 1.3.13 or newer is required you have 0.9.3

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
92 queries in 0.194 seconds, using 23.43MB memory