NameError: name ‘_mysql’ is not defined after setting change to mysql

I have a running Django blog with sqlite3 db at my local machine. What I want is to

  1. convert sqlite3 db to mysql db
  2. change Django file to serve MySQL db

Before I ran into the first step, I jumped into the second first. I followed this web page (on MacOS). I created databases called djangolocaldb on root user and have those infos in /etc/mysql/my.cnf like this:

# /etc/mysql/my.cnf


Of course I created db, but not table within it.

mysql> show databases;
| Database           |
| djangolocaldb      |
| employees          |
| information_schema |
| mydatabase         |
| mysql              |
| performance_schema |
| sys                |
7 rows in set (0.00 sec)

I changed like this as the web page suggested. Here’s how:



# Database

        'default': {
            'ENGINE': 'django.db.backends.mysql',
            #'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
            'OPTIONS' : {
                'read_default_file': '/etc/mysql/my.cnf',


Now, when I run python runserver with my venv activated, I got a brutal traceback like this(I ran python migrate first, and the traceback looked almost the same anyway):

(.venv) ➜  django-local-blog git:(master) ✗ python runserver
Watching for file changes with StatReloader
Exception in thread django-main-thread:
Traceback (most recent call last):
  File "/Users/gwanghyeongim/Documents/py/coreyMS_pj/django-local-blog/.venv/lib/python3.7/site-packages/MySQLdb/", line 18, in <module>
    from . import _mysql
ImportError: dlopen(/Users/gwanghyeongim/Documents/py/coreyMS_pj/django-local-blog/.venv/lib/python3.7/site-packages/MySQLdb/, 2): Library not loaded: @rpath/libmysqlclient.21.dylib
  Referenced from: /Users/gwanghyeongim/Documents/py/coreyMS_pj/django-local-blog/.venv/lib/python3.7/site-packages/MySQLdb/
  Reason: image not found

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/gwanghyeongim/.pyenv/versions/3.7.6/lib/python3.7/", line 926, in _bootstrap_inner
  File "/Users/gwanghyeongim/.pyenv/versions/3.7.6/lib/python3.7/", line 870, in run
    self._target(*self._args, **self._kwargs)
  File "/Users/gwanghyeongim/Documents/py/coreyMS_pj/django-local-blog/.venv/lib/python3.7/site-packages/django/utils/", line 53, in wrapper
    fn(*args, **kwargs)
  File "/Users/gwanghyeongim/Documents/py/coreyMS_pj/django-local-blog/.venv/lib/python3.7/site-packages/django/core/management/commands/", line 109, in inner_run
  File "/Users/gwanghyeongim/Documents/py/coreyMS_pj/django-local-blog/.venv/lib/python3.7/site-packages/django/utils/", line 76, in raise_last_exception
    raise _exception[1]
  File "/Users/gwanghyeongim/Documents/py/coreyMS_pj/django-local-blog/.venv/lib/python3.7/site-packages/django/core/management/", line 357, in execute
  File "/Users/gwanghyeongim/Documents/py/coreyMS_pj/django-local-blog/.venv/lib/python3.7/site-packages/django/utils/", line 53, in wrapper
    fn(*args, **kwargs)
  File "/Users/gwanghyeongim/Documents/py/coreyMS_pj/django-local-blog/.venv/lib/python3.7/site-packages/django/", line 24, in setup
  File "/Users/gwanghyeongim/Documents/py/coreyMS_pj/django-local-blog/.venv/lib/python3.7/site-packages/django/apps/", line 114, in populate
  File "/Users/gwanghyeongim/Documents/py/coreyMS_pj/django-local-blog/.venv/lib/python3.7/site-packages/django/apps/", line 211, in import_models
    self.models_module = import_module(models_module_name)
  File "/Users/gwanghyeongim/.pyenv/versions/3.7.6/lib/python3.7/importlib/", 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/gwanghyeongim/Documents/py/coreyMS_pj/django-local-blog/.venv/lib/python3.7/site-packages/django/contrib/auth/", line 2, in <module>
    from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
  File "/Users/gwanghyeongim/Documents/py/coreyMS_pj/django-local-blog/.venv/lib/python3.7/site-packages/django/contrib/auth/", line 47, in <module>
    class AbstractBaseUser(models.Model):
  File "/Users/gwanghyeongim/Documents/py/coreyMS_pj/django-local-blog/.venv/lib/python3.7/site-packages/django/db/models/", line 121, in __new__
    new_class.add_to_class('_meta', Options(meta, app_label))
  File "/Users/gwanghyeongim/Documents/py/coreyMS_pj/django-local-blog/.venv/lib/python3.7/site-packages/django/db/models/", line 325, in add_to_class
    value.contribute_to_class(cls, name)
  File "/Users/gwanghyeongim/Documents/py/coreyMS_pj/django-local-blog/.venv/lib/python3.7/site-packages/django/db/models/", line 208, in contribute_to_class
    self.db_table = truncate_name(self.db_table, connection.ops.max_name_length())
  File "/Users/gwanghyeongim/Documents/py/coreyMS_pj/django-local-blog/.venv/lib/python3.7/site-packages/django/db/", line 28, in __getattr__
    return getattr(connections[DEFAULT_DB_ALIAS], item)
  File "/Users/gwanghyeongim/Documents/py/coreyMS_pj/django-local-blog/.venv/lib/python3.7/site-packages/django/db/", line 207, in __getitem__
    backend = load_backend(db['ENGINE'])
  File "/Users/gwanghyeongim/Documents/py/coreyMS_pj/django-local-blog/.venv/lib/python3.7/site-packages/django/db/", line 111, in load_backend
    return import_module('%s.base' % backend_name)
  File "/Users/gwanghyeongim/.pyenv/versions/3.7.6/lib/python3.7/importlib/", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "/Users/gwanghyeongim/Documents/py/coreyMS_pj/django-local-blog/.venv/lib/python3.7/site-packages/django/db/backends/mysql/", line 16, in <module>
    import MySQLdb as Database
  File "/Users/gwanghyeongim/Documents/py/coreyMS_pj/django-local-blog/.venv/lib/python3.7/site-packages/MySQLdb/", line 24, in <module>
    version_info, _mysql.version_info, _mysql.__file__
NameError: name '_mysql' is not defined

So this NameError: name '_mysql' is not defined is the problem. I installed mysqlclient before, changed, made db in mysql, but none of the steps made it any helpful yet.

And I noticed that even I changed my back to sqlite3, my blog spit the same _mysql not defined error. So I ended up reverting my commit and now I’m back to sqlite3 (at least my blog is running with it).

I’m guessing it could be that I didn’t convert data first, but I’m not 100% sure of it.

Any suggestion would be much appreciated. Thank you in advance!

EDITTED on Aug 18, 2020

If you still see _mysql not defined error when you run python migrate somehow, check out the following two settings.

  1. If you see mysqlclient when you run pip freeze in your python virtual environment like so,
(.venv) ➜  SqlDjango git:(master) ✗ pip freeze

make sure you installed mysqlclient in your python virtual environment, not mysql-client. The former supports python3, whereas the latter supports python2, which I assume is not probably what you want.

  1. Make sure you put the following line in your bash config file(~/.zshrc for zsh, or ~/.bashrc or ~/.bash_profile for bash etc.)
export DYLD_LIBRARY_PATH=/usr/local/mysql/lib/

After that, apply change by runnig source ~/.your_shell_config_file. See python migrate works.


So as a full answer:

If you use the python package mysqlclient you still need to install the mysql client from Oracle/MySQL. This contains the C-library that the python package uses. To make things more confusing: the python package is in fact written in C for speed increases. To install this library on MacOS:

% brew install mysql-client

There’s also a pure python package, with a more attractive MIT License, which can be a solution if your company or client does not allow GPL. However, it’s not officially supported and some subtle bugs can occur in between releases. YMMV.

Leave a Reply

Your email address will not be published. Required fields are marked *