django2.1.5链接Mysql出错No module named 'MySQLdb'的解决方法

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/con_knife/article/details/86592107

"C:\Program Files\JetBrains\PyCharm 2018.3.3\bin\runnerw64.exe" D:\Envs\django\Scripts\python.exe D:/mysite/manage.py runserver 127.0.0.1:8000
Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x03A0FAE0>
Traceback (most recent call last):
  File "D:\Envs\django\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 "D:\Envs\django\lib\site-packages\django\utils\autoreload.py", line 225, in wrapper
    fn(*args, **kwargs)
  File "D:\Envs\django\lib\site-packages\django\core\management\commands\runserver.py", line 109, in inner_run
    autoreload.raise_last_exception()
  File "D:\Envs\django\lib\site-packages\django\utils\autoreload.py", line 248, in raise_last_exception
    raise _exception[1]
  File "D:\Envs\django\lib\site-packages\django\core\management\__init__.py", line 337, in execute
    autoreload.check_errors(django.setup)()
  File "D:\Envs\django\lib\site-packages\django\utils\autoreload.py", line 225, in wrapper
    fn(*args, **kwargs)
  File "D:\Envs\django\lib\site-packages\django\__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "D:\Envs\django\lib\site-packages\django\apps\registry.py", line 112, in populate
    app_config.import_models()
  File "D:\Envs\django\lib\site-packages\django\apps\config.py", line 198, in import_models
    self.models_module = import_module(models_module_name)
  File "D:\Envs\django\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:\Envs\django\lib\site-packages\django\contrib\auth\models.py", line 2, in <module>
    from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
  File "D:\Envs\django\lib\site-packages\django\contrib\auth\base_user.py", line 47, in <module>
    class AbstractBaseUser(models.Model):
  File "D:\Envs\django\lib\site-packages\django\db\models\base.py", line 101, in __new__
    new_class.add_to_class('_meta', Options(meta, app_label))
  File "D:\Envs\django\lib\site-packages\django\db\models\base.py", line 305, in add_to_class
    value.contribute_to_class(cls, name)
  File "D:\Envs\django\lib\site-packages\django\db\models\options.py", line 203, in contribute_to_class
    self.db_table = truncate_name(self.db_table, connection.ops.max_name_length())
  File "D:\Envs\django\lib\site-packages\django\db\__init__.py", line 33, in __getattr__
    return getattr(connections[DEFAULT_DB_ALIAS], item)
  File "D:\Envs\django\lib\site-packages\django\db\utils.py", line 202, in __getitem__
    backend = load_backend(db['ENGINE'])
  File "D:\Envs\django\lib\site-packages\django\db\utils.py", line 110, in load_backend
    return import_module('%s.base' % backend_name)
  File "D:\Envs\django\lib\importlib\__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "D:\Envs\django\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?

报错的信息很长但是也要黏贴出来,我的运行环境是python3.7+django2.15,编辑器是pycharm,报错原因链接MySQL。

在项目配置中链接MySQL的配置 信息如下:
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'mysite',
        'USER': 'root',
        'PASSWORD': 'root',
        'HOST': '127.0.0.1',
        'PORT': '3306',
    }
}

方法解析:
MySQLdb只支持Python2.,还不支持3.
可以用PyMySQL代替。安装方法:pip install PyMySQL

首先命令行安装PYMYSQL,pip install pymysql(此处安装也可以使用国内的镜像),然后在需要的项目中,然后把 init.py中添加两行:
import pymysql
pymysql.install_as_MySQLdb()
就可以用 import MySQLdb了。其他的方法与MySQLdb一样。

运行python manage.py runserver,返回以下信息证明MySQL已经链接上了

Performing system checks...

System check identified no issues (0 silenced).

You have 15 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
Run 'python manage.py migrate' to apply them.
January 22, 2019 - 11:39:54
Django version 2.1.5, using settings 'mysite.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.

python:no module named MySQLdb

03-05

想用django开发网站。使用的是python2.7,操作系统win10.已经安装好了Anaconda和mysql。现在想进行连接数据库,已经在网站下载并安装了多个版本的python-mysql驱动(1.2.3版本等),然后再import MySQLdb,还是提示no module named MySQLdbrnrnrn在C:\Anaconda2\Lib\site-packages也已经有了C:\Anaconda2\Lib\site-packages\MySQL_python-1.2.3-py2.7.egg-info和C:\Anaconda2\Lib\site-packages\MySQLdb。rnrn为什么就是不能成功呢?rnrndjango在启动服务的时候,错误为:rnC:\Anaconda2\Django-1.9.4\django\bin\hello>python manage.py runsever 0.0.0.0:8080rnTraceback (most recent call last):rn File "manage.py", line 10, in rn execute_from_command_line(sys.argv)rn File "F:\Anaconda\lib\site-packages\django-1.9.4-py2.7.egg\django\core\management\__init__.py", line 353, in execute_from_command_linern utility.execute()rn File "F:\Anaconda\lib\site-packages\django-1.9.4-py2.7.egg\django\core\management\__init__.py", line 327, in executern django.setup()rn File "F:\Anaconda\lib\site-packages\django-1.9.4-py2.7.egg\django\__init__.py", line 18, in setuprn apps.populate(settings.INSTALLED_APPS)rn File "F:\Anaconda\lib\site-packages\django-1.9.4-py2.7.egg\django\apps\registry.py", line 108, in populatern app_config.import_models(all_models)rn File "F:\Anaconda\lib\site-packages\django-1.9.4-py2.7.egg\django\apps\config.py", line 202, in import_modelsrn self.models_module = import_module(models_module_name)rn File "F:\Anaconda\lib\importlib\__init__.py", line 37, in import_modulern __import__(name)rn File "F:\Anaconda\lib\site-packages\django-1.9.4-py2.7.egg\django\contrib\auth\models.py", line 4, in rn from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManagerrn File "F:\Anaconda\lib\site-packages\django-1.9.4-py2.7.egg\django\contrib\auth\base_user.py", line 49, in rn class AbstractBaseUser(models.Model):rn File "F:\Anaconda\lib\site-packages\django-1.9.4-py2.7.egg\django\db\models\base.py", line 108, in __new__rn new_class.add_to_class('_meta', Options(meta, app_label))rn File "F:\Anaconda\lib\site-packages\django-1.9.4-py2.7.egg\django\db\models\base.py", line 299, in add_to_classrn value.contribute_to_class(cls, name)rn File "F:\Anaconda\lib\site-packages\django-1.9.4-py2.7.egg\django\db\models\options.py", line 263, in contribute_to_classrn self.db_table = truncate_name(self.db_table, connection.ops.max_name_length())rn File "F:\Anaconda\lib\site-packages\django-1.9.4-py2.7.egg\django\db\__init__.py", line 36, in __getattr__rn return getattr(connections[DEFAULT_DB_ALIAS], item)rn File "F:\Anaconda\lib\site-packages\django-1.9.4-py2.7.egg\django\db\utils.py", line 212, in __getitem__rn backend = load_backend(db['ENGINE'])rn File "F:\Anaconda\lib\site-packages\django-1.9.4-py2.7.egg\django\db\utils.py", line 116, in load_backendrn return import_module('%s.base' % backend_name)rn File "F:\Anaconda\lib\importlib\__init__.py", line 37, in import_modulern __import__(name)rn File "F:\Anaconda\lib\site-packages\django-1.9.4-py2.7.egg\django\db\backends\mysql\base.py", line 28, in rn raise ImproperlyConfigured("Error loading MySQLdb module: %s" % e)rndjango.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module: No module named MySQLdb 论坛

没有更多推荐了,返回首页