Django application fails with error "NameError: name '_mysql' is not defined"

With Python 3.7.9 and Django 3, the application always fails with the error “NameError: name ‘_mysql’ is not defined”.

We are doing some house cleaning to the project, disabling and enabling YUM repos, and removing and reinstalling packages. The issue happened when trying to restart the service after the change.

Please see the exception trace and pip package list attached in the Details section below, and let me know if you need to see the yum package information. We highly appreciate your help.

Details:

1. The full exception trace:

Exception in thread django-main-thread:
Traceback (most recent call last):
  File "/data/app/venv3.7/lib/python3.7/site-packages/MySQLdb/__init__.py", line 18, in <module>
    from . import _mysql
ImportError: libperconaserverclient.so.21: cannot open shared object file: No such file or directory

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/lib/python3.7/threading.py", line 926, in _bootstrap_inner
    self.run()
  File "/usr/local/lib/python3.7/threading.py", line 870, in run
    self._target(*self._args, **self._kwargs)
  File "/data/app/venv3.7/lib/python3.7/site-packages/django/utils/autoreload.py", line 53, in wrapper
    fn(*args, **kwargs)
  File "/data/app/venv3.7/lib/python3.7/site-packages/django/core/management/commands/runserver.py", line 110, in inner_run
    autoreload.raise_last_exception()
  File "/data/app/venv3.7/lib/python3.7/site-packages/django/utils/autoreload.py", line 76, in raise_last_exception
    raise _exception[1]
  File "/data/app/venv3.7/lib/python3.7/site-packages/django/core/management/__init__.py", line 357, in execute
    autoreload.check_errors(django.setup)()
  File "/data/app/venv3.7/lib/python3.7/site-packages/django/utils/autoreload.py", line 53, in wrapper
    fn(*args, **kwargs)
  File "/data/app/venv3.7/lib/python3.7/site-packages/django/__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/data/app/venv3.7/lib/python3.7/site-packages/django/apps/registry.py", line 114, in populate
    app_config.import_models()
  File "/data/app/venv3.7/lib/python3.7/site-packages/django/apps/config.py", line 211, in import_models
    self.models_module = import_module(models_module_name)
  File "/usr/local/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 "/data/app/venv3.7/lib/python3.7/site-packages/django/contrib/auth/models.py", line 2, in <module>
    from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
  File "/data/app/venv3.7/lib/python3.7/site-packages/django/contrib/auth/base_user.py", line 48, in <module>
    class AbstractBaseUser(models.Model):
  File "/data/app/venv3.7/lib/python3.7/site-packages/django/db/models/base.py", line 122, in __new__
    new_class.add_to_class('_meta', Options(meta, app_label))
  File "/data/app/venv3.7/lib/python3.7/site-packages/django/db/models/base.py", line 326, in add_to_class
    value.contribute_to_class(cls, name)
  File "/data/app/venv3.7/lib/python3.7/site-packages/django/db/models/options.py", line 206, in contribute_to_class
    self.db_table = truncate_name(self.db_table, connection.ops.max_name_length())
  File "/data/app/venv3.7/lib/python3.7/site-packages/django/db/__init__.py", line 28, in __getattr__
    return getattr(connections[DEFAULT_DB_ALIAS], item)
  File "/data/app/venv3.7/lib/python3.7/site-packages/django/db/utils.py", line 214, in __getitem__
    backend = load_backend(db['ENGINE'])
  File "/data/app/venv3.7/lib/python3.7/site-packages/django/db/utils.py", line 111, in load_backend
    return import_module('%s.base' % backend_name)
  File "/usr/local/lib/python3.7/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "/data/app/venv3.7/lib/python3.7/site-packages/django/db/backends/mysql/base.py", line 15, in <module>
    import MySQLdb as Database
  File "/data/app/venv3.7/lib/python3.7/site-packages/MySQLdb/__init__.py", line 24, in <module>
    version_info, _mysql.version_info, _mysql.__file__
NameError: name '_mysql' is not defined

2. The pip list:

amqp==5.0.2
anyjson==0.3.3
appdirs==1.4.4
arrow==0.13.2
asgiref==3.3.1
attrs==20.3.0
backcall==0.2.0
backports.functools-lru-cache==1.6.1
beautifulsoup4==4.9.3
billiard==3.6.3.0
caniusepython3==7.2.0
celery==5.0.4
certifi==2020.12.5
chardet==3.0.4
click==7.1.2
click-didyoumean==0.0.3
click-plugins==1.1.1
click-repl==0.1.6
decorator==4.4.2
distlib==0.3.1
Django==3.1.4
django-cors-headers==3.5.0
django-debug-toolbar==3.2
django-easy-pjax==1.3.0
django-rest-auth==0.9.5
djangorestframework==3.12.2
fake-factory==9999.9.9
fissix==20.8.0
gunicorn==20.0.4
idna==2.10
importlab==0.5.1
importlib-metadata==3.1.1
ipython==7.19.0
ipython-genutils==0.2.0
jedi==0.17.2
kombu==5.0.2
line-profiler==3.1.0
lxml==4.6.2
mock==4.0.2
modernize==0.8.0
mysqlclient==2.0.1
networkx==2.5
ninja==1.10.0.post2
packaging==20.7
parso==0.7.1
pexpect==4.8.0
pickleshare==0.7.5
Pillow==8.0.1
pip==21.0
pip-review==1.1.0
pipdeptree==1.0.0
prompt-toolkit==3.0.8
ptyprocess==0.6.0
Pygments==2.7.3
pyparsing==2.4.7
python-dateutil==2.8.1
pytype==2020.12.2
pytz==2020.4
PyYAML==5.3.1
qrcode==6.1
requests==2.25.0
setproctitle==1.2.1
setuptools==51.3.3
six==1.15.0
soupsieve==2.0.1
sqlparse==0.4.1
traitlets==5.0.5
typed-ast==1.4.1
urllib3==1.26.2
vine==5.0.0
wcwidth==0.2.5
wheel==0.36.2
yet-another-django-profiler==1.1.0
zipp==3.4.0

The real error is higher up in the traceback:

This looks like you’re somehow trying to use the Percona MySQL server distribution without having all the libraries installed. There’s some system-level package you’re missing - it’s not something that can be installed with PIP, it’ll be installed through your package manager. (e.g. apt or yum or whatever)

1 Like

Good catch, @KenWhitesell, and thank you for the reminder on the details.

We are doing some house cleaning on the server, including disabling and enabling YUM repos and removing and reinstalling packages. Previously, we installed the Python pip packages with compiling; therefore, the underlying YUM package changes caused dependency conflicts in the self-compiled pip binaries.

We resolved the issue by recompiling the pip freeze list using the command pip install -r pip-freeze.txt --force-reinstall --no-cache-dir.