0

I installed mysqlclient inside my python virtual enviorment to connect to a mysql database.

But when I change the settings.py database engine settings from sqllite to mysql:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': BASE_DIR / 'db.sqlite3',
    }
}

I get this weird error telling me I havn't installed 'mysqlclient' package

Exception in thread django-main-thread:
Traceback (most recent call last):
  File "/home/reza/.local/share/virtualenvs/store-api-wAMLFNlP/lib/python3.10/site-packages/django/db/backends/mysql/base.py", line 15, in <module>
    import MySQLdb as Database
  File "/home/reza/.local/share/virtualenvs/store-api-wAMLFNlP/lib/python3.10/site-packages/MySQLdb/__init__.py", line 17, in <module>
    from . import _mysql
ImportError: libmysqlclient.so.21: cannot open shared object file: No such file or directory

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/usr/lib/python3.10/threading.py", line 1016, in _bootstrap_inner
    self.run()
  File "/usr/lib/python3.10/threading.py", line 953, in run
    self._target(*self._args, **self._kwargs)
  File "/home/reza/.local/share/virtualenvs/store-api-wAMLFNlP/lib/python3.10/site-packages/django/utils/autoreload.py", line 64, in wrapper
    fn(*args, **kwargs)
  File "/home/reza/.local/share/virtualenvs/store-api-wAMLFNlP/lib/python3.10/site-packages/django/core/management/commands/runserver.py", line 125, in inner_run
    autoreload.raise_last_exception()
  File "/home/reza/.local/share/virtualenvs/store-api-wAMLFNlP/lib/python3.10/site-packages/django/utils/autoreload.py", line 87, in raise_last_exception
    raise _exception[1]
  File "/home/reza/.local/share/virtualenvs/store-api-wAMLFNlP/lib/python3.10/site-packages/django/core/management/__init__.py", line 394, in execute
    autoreload.check_errors(django.setup)()
  File "/home/reza/.local/share/virtualenvs/store-api-wAMLFNlP/lib/python3.10/site-packages/django/utils/autoreload.py", line 64, in wrapper
    fn(*args, **kwargs)
  File "/home/reza/.local/share/virtualenvs/store-api-wAMLFNlP/lib/python3.10/site-packages/django/__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/home/reza/.local/share/virtualenvs/store-api-wAMLFNlP/lib/python3.10/site-packages/django/apps/registry.py", line 116, in populate
    app_config.import_models()
  File "/home/reza/.local/share/virtualenvs/store-api-wAMLFNlP/lib/python3.10/site-packages/django/apps/config.py", line 269, in import_models
    self.models_module = import_module(models_module_name)
  File "/usr/lib/python3.10/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 883, in exec_module
  File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
  File "/home/reza/.local/share/virtualenvs/store-api-wAMLFNlP/lib/python3.10/site-packages/django/contrib/auth/models.py", line 3, in <module>
    from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
  File "/home/reza/.local/share/virtualenvs/store-api-wAMLFNlP/lib/python3.10/site-packages/django/contrib/auth/base_user.py", line 57, in <module>
    class AbstractBaseUser(models.Model):
  File "/home/reza/.local/share/virtualenvs/store-api-wAMLFNlP/lib/python3.10/site-packages/django/db/models/base.py", line 143, in __new__
    new_class.add_to_class("_meta", Options(meta, app_label))
  File "/home/reza/.local/share/virtualenvs/store-api-wAMLFNlP/lib/python3.10/site-packages/django/db/models/base.py", line 371, in add_to_class
    value.contribute_to_class(cls, name)
  File "/home/reza/.local/share/virtualenvs/store-api-wAMLFNlP/lib/python3.10/site-packages/django/db/models/options.py", line 243, in contribute_to_class
    self.db_table, connection.ops.max_name_length()
  File "/home/reza/.local/share/virtualenvs/store-api-wAMLFNlP/lib/python3.10/site-packages/django/utils/connection.py", line 15, in __getattr__
    return getattr(self._connections[self._alias], item)
  File "/home/reza/.local/share/virtualenvs/store-api-wAMLFNlP/lib/python3.10/site-packages/django/utils/connection.py", line 62, in __getitem__
    conn = self.create_connection(alias)
  File "/home/reza/.local/share/virtualenvs/store-api-wAMLFNlP/lib/python3.10/site-packages/django/db/utils.py", line 193, in create_connection
    backend = load_backend(db["ENGINE"])
  File "/home/reza/.local/share/virtualenvs/store-api-wAMLFNlP/lib/python3.10/site-packages/django/db/utils.py", line 113, in load_backend
    return import_module("%s.base" % backend_name)
  File "/usr/lib/python3.10/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "/home/reza/.local/share/virtualenvs/store-api-wAMLFNlP/lib/python3.10/site-packages/django/db/backends/mysql/base.py", line 17, in <module>
    raise ImproperlyConfigured(
django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module.
Did you install mysqlclient?

I'm sure installed 'mysqlclient' successfully and this is Pipfile.lock file :

{
    "_meta": {
        "hash": {
            "sha256": "fe52abfe897fbfb2bf81adbfa85d0678bc42ddce6bcd83f3e37e0a921d34273a"
        },
        "pipfile-spec": 6,
        "requires": {
            "python_version": "3.10"
        },
        "sources": [
            {
                "name": "pypi",
                "url": "https://pypi.org/simple",
                "verify_ssl": true
            }
        ]
    },
    "default": {
        "asgiref": {
            "hashes": [
                "sha256:89b2ef2247e3b562a16eef663bc0e2e703ec6468e2fa8a5cd61cd449786d4f6e",
                "sha256:9e0ce3aa93a819ba5b45120216b23878cf6e8525eb3848653452b4192b92afed"
            ],
            "markers": "python_version >= '3.7'",
            "version": "==3.7.2"
        },
        "django": {
            "hashes": [
                "sha256:7e4225ec065e0f354ccf7349a22d209de09cc1c074832be9eb84c51c1799c432",
                "sha256:860ae6a138a238fc4f22c99b52f3ead982bb4b1aad8c0122bcd8c8a3a02e409d"
            ],
            "index": "pypi",
            "markers": "python_version >= '3.8'",
            "version": "==4.2.4"
        },
        "mysqlclient": {
            "hashes": [
                "sha256:004fe1d30d2c2ff8072f8ea513bcec235fd9b896f70dad369461d0ad7e570e98",
                "sha256:04368445f9c487d8abb7a878e3d23e923e6072c04a6c320f9e0dc8a82efba14e",
                "sha256:530ece9995a36cadb6211b9787f0c9e05cdab6702549bdb4236af5e9b535ed6a",
                "sha256:5670679ff1be1cc3fef0fa81bf39f0cd70605ba121141050f02743eb878ac114",
                "sha256:68837b6bb23170acffb43ae411e47533a560b6360c06dac39aa55700972c93b2",
                "sha256:955dba905a7443ce4788c63fdb9f8d688316260cf60b20ff51ac3b1c77616ede",
                "sha256:9c6b142836c7dba4f723bf9c93cc46b6e5081d65b2af807f400dda9eb85a16d0"
            ],
            "index": "pypi",
            "markers": "python_version >= '3.8'",
            "version": "==2.2.0"
        },
        "sqlparse": {
            "hashes": [
                "sha256:5430a4fe2ac7d0f93e66f1efc6e1338a41884b7ddf2a350cedd20ccc4d9d28f3",
                "sha256:d446183e84b8349fa3061f0fe7f06ca94ba65b426946ffebe6e3e8295332420c"
            ],
            "markers": "python_version >= '3.5'",
            "version": "==0.4.4"
        },
        "typing-extensions": {
            "hashes": [
                "sha256:440d5dd3af93b060174bf433bccd69b0babc3b15b1a8dca43789fd7f61514b36",
                "sha256:b75ddc264f0ba5615db7ba217daeb99701ad295353c45f9e95963337ceeeffb2"
            ],
            "markers": "python_version < '3.11'",
            "version": "==4.7.1"
        }
    },
    "develop": {}
}

If I change the

'ENGINE': 'django.db.backends.mysql',

back to

'ENGINE': 'django.db.backends.sqllite',

the error goes away.

1

1 Answer 1

0

You are pointing the mysql database to the sqlite file, hence the ImproperlyConfigured error. The error message being displayed is misleading, and is probably a minor bug, assuming there is a way to distinguish between mysqlclient being installed and an improper configuration.

You need to define the following for a mysql database:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql', 
        'NAME': DB_NAME,
        'USER': DB_USERNAME,
        'PASSWORD': DB_PASSWORD,
        'HOST': 'localhost',
        'PORT': '3306',
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

It is the same erorr. Acturally ENGINE line is the line that generates the error

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.