django.core.exceptions . improveconfigured: error loading MySQL DB module. Django configuring MySQL database

A, errors,

Traceback (most recent call last):
  File "/home/ubuntu/anaconda2/envs/python3/lib/python3.6/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 "manage.py", line 15, in <module>
    execute_from_command_line(sys.argv)
  File "/home/ubuntu/anaconda2/envs/python3/lib/python3.6/site-packages/django/core/management/__init__.py", line 371, in execute_from_command_line
    utility.execute()
  File "/home/ubuntu/anaconda2/envs/python3/lib/python3.6/site-packages/django/core/management/__init__.py", line 347, in execute
    django.setup()
  File "/home/ubuntu/anaconda2/envs/python3/lib/python3.6/site-packages/django/__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/home/ubuntu/anaconda2/envs/python3/lib/python3.6/site-packages/django/apps/registry.py", line 112, in populate
    app_config.import_models()
  File "/home/ubuntu/anaconda2/envs/python3/lib/python3.6/site-packages/django/apps/config.py", line 198, in import_models
    self.models_module = import_module(models_module_name)
  File "/home/ubuntu/anaconda2/envs/python3/lib/python3.6/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 994, in _gcd_import
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 678, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/home/ubuntu/anaconda2/envs/python3/lib/python3.6/site-packages/django/contrib/auth/models.py", line 2, in <module>
    from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
  File "/home/ubuntu/anaconda2/envs/python3/lib/python3.6/site-packages/django/contrib/auth/base_user.py", line 47, in <module>
    class AbstractBaseUser(models.Model):
  File "/home/ubuntu/anaconda2/envs/python3/lib/python3.6/site-packages/django/db/models/base.py", line 114, in __new__
    new_class.add_to_class('_meta', Options(meta, app_label))
  File "/home/ubuntu/anaconda2/envs/python3/lib/python3.6/site-packages/django/db/models/base.py", line 315, in add_to_class
    value.contribute_to_class(cls, name)
  File "/home/ubuntu/anaconda2/envs/python3/lib/python3.6/site-packages/django/db/models/options.py", line 205, in contribute_to_class
    self.db_table = truncate_name(self.db_table, connection.ops.max_name_length())
  File "/home/ubuntu/anaconda2/envs/python3/lib/python3.6/site-packages/django/db/__init__.py", line 33, in __getattr__
    return getattr(connections[DEFAULT_DB_ALIAS], item)
  File "/home/ubuntu/anaconda2/envs/python3/lib/python3.6/site-packages/django/db/utils.py", line 202, in __getitem__
    backend = load_backend(db['ENGINE'])
  File "/home/ubuntu/anaconda2/envs/python3/lib/python3.6/site-packages/django/db/utils.py", line 110, in load_backend
    return import_module('%s.base' % backend_name)
  File "/home/ubuntu/anaconda2/envs/python3/lib/python3.6/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "/home/ubuntu/anaconda2/envs/python3/lib/python3.6/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?

Ii. Problem Solving
1. Source of problem
After installation of Django, the default database driver is sqlite, but I do not have sqlite database, so change to
mysql, then go to the directory first/first/settings.py, and find:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}

Modified to:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'first',
        'USER': 'python',
        'PASSWORD': '123456',
        'HOST': '192.168.99.124',
        # 'PORT': '',
    }
}

Run the command python manage. Py runserver then executes to start the service at the following Error:
django. Core. Exceptions. ImproperlyConfigured: Error loading MySQLdb module: No module named MySQLdb
2. Solve
Obviously, there is no mysql driver installed, just install it, so I quickly typed the command line:

pip install mysql-python

ImportError: No module named 'ConfigParser'
Python3.x is changed to lowercase relative to PYTHON 2.x, indicating that THE MYSQL python in PIP does not support Python3.x. Maybe the update in PIP is relatively slow, so we will download their source code installation. However, the latest update date of the source code is 2012, and there is still no support for Python3.x!
How does this work?Change the support python3 driver try, heart energizes, grass mud horse, of course, as a result on making a search, found a PyMySQL (https://github.com/PyMySQL/PyMySQL), which, of course, also have it in the PIP, then I quickly on the command line with:

pip install PyMySQL

Django does not recognize this alien monk, how to do, or to do, find mysite/mysite/init.py, enter the following content and save:

import pymysql
pymysql.install_as_MySQLdb()

Then When I ran Python Manager.py Runserver, another prompt popped up:
You have unapplied migrations; Run 'Python manage. Py Migrate' to apply them.
Of course, this prompt does not affect the operation of the native server. At this time, when we visit http://127.0.0.1:8000, we will see the successful prompt:

It worked!

Congratulations on your first Django-powered page.

Of course, you haven't actually done any work yet. Next, start your first app by running python manage.py startapp [app_label].

You're seeing this message because you have DEBUG = True in your Django settings file and you haven't configured any URLs. Get to work!

Of course, I didn't like the warning, and he told me what to do to get rid of him, of course! I quickly copy the contents to the command line:

python manage.py makemigrations
python manage.py migrate

Then restart the service, it's normal!


The environment

    Python version: 3.6.0Django version: 2.5

Read More: