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

Publish date: 2024-05-05

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

  • convert sqlite3 db to mysql db
  • change Django settings.py 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 [client] database=djangolocaldb user=root password=ROOTPASSWORD default-character-set=utf8 

    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 settings.py like this as the web page suggested. Here's how:

    # settings.py ... # Database # https://docs.djangoproject.com/en/3.0/ref/settings/#databases DATABASES = { '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 ran python manage.py runserver with my venv activated, I got a brutal traceback like this(I ran python manage.py migrate first, and the traceback looked almost the same anyway):

    (.venv) ➜ django-local-blog git:(master) ✗ python manage.py 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/__init__.py", 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/_mysql.cpython-37m-darwin.so, 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/_mysql.cpython-37m-darwin.so 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/threading.py", line 926, in _bootstrap_inner self.run() File "/Users/gwanghyeongim/.pyenv/versions/3.7.6/lib/python3.7/threading.py", 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/autoreload.py", 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/runserver.py", line 109, in inner_run autoreload.raise_last_exception() File "/Users/gwanghyeongim/Documents/py/coreyMS_pj/django-local-blog/.venv/lib/python3.7/site-packages/django/utils/autoreload.py", 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/__init__.py", line 357, in execute autoreload.check_errors(django.setup)() File "/Users/gwanghyeongim/Documents/py/coreyMS_pj/django-local-blog/.venv/lib/python3.7/site-packages/django/utils/autoreload.py", line 53, in wrapper fn(*args, **kwargs) File "/Users/gwanghyeongim/Documents/py/coreyMS_pj/django-local-blog/.venv/lib/python3.7/site-packages/django/__init__.py", line 24, in setup apps.populate(settings.INSTALLED_APPS) File "/Users/gwanghyeongim/Documents/py/coreyMS_pj/django-local-blog/.venv/lib/python3.7/site-packages/django/apps/registry.py", line 114, in populate app_config.import_models() File "/Users/gwanghyeongim/Documents/py/coreyMS_pj/django-local-blog/.venv/lib/python3.7/site-packages/django/apps/config.py", 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/__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 "/Users/gwanghyeongim/Documents/py/coreyMS_pj/django-local-blog/.venv/lib/python3.7/site-packages/django/contrib/auth/models.py", 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/base_user.py", 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/base.py", 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/base.py", 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/options.py", 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/__init__.py", 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/utils.py", 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/utils.py", line 111, in load_backend return import_module('%s.base' % backend_name) File "/Users/gwanghyeongim/.pyenv/versions/3.7.6/lib/python3.7/importlib/__init__.py", 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/base.py", 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/__init__.py", 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 settings.py, made db in mysql, but none of the steps made it any helpful yet.

    And I noticed that even I changed my settings.py 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!

    EDITED on Aug 18, 2020

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

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

    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.

  • 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 manage.py migrate works.

    4

    17 Answers

    This did the job for me! Just install libmysqlclient-dev (sudo apt-get install libmysqlclient-dev for Ubuntu). Sometimes, the lib files simply are missing even if you just installed mysql. :)

    So, I'm answering my own question. Since my blog has database, I gave it a shot to make another project without db, start fresh.

    What I noticed was there's a problem importing MySQLdb module(sub module of mysqlclient) with this traceback: Library not loaded: @rpath/libmysqlclient.21.dylib.

    For browsing a few hours I realised that for some reason Mac security setting keeps this from being imported properly.

    On mysqlclient library github I found one issue reporting the same as mine. It suggests I run cp -r /usr/local/mysql/lib/* /usr/local/lib/. After this I set settings.py for django.db.backends.mysql, ran python manage.py migrate and it worked. So for empty database, this could be a solution. Still struggling with database one though.

    I use

    3

    this worked for me:

    add this to PATH:

    export DYLD_LIBRARY_PATH="/usr/local/mysql/lib:$PATH" 

    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.

    1

    I was facing the same problem on my MacOS (Big Sur) and I fixed it by doing this cp -r /usr/local/mysql/lib/* /usr/local/lib/

    0

    This solved the issue for me:

    Since Python3 is not able to connect with Python through mysqldb, you need to install an additional module to fix things. installing mysqlclient caused me to have the same NameError: : name '_mysql' is not defined problem.

    However, by using pymysql, and adding the code line pymysql.install_as_MySQLdb() at the top of my Flask app, I managed to get it running without any errors!

    More info on mysql modules

    1

    I just had a similar problem and couldnt find solution for hours

    >>> import MySQLdb Traceback (most recent call last): File "/{path-to-venv}/lib/python3.7/site-packages/MySQLdb/__init__.py", line 18, in <module> from . import _mysql ImportError: /{path-to-venv}/lib/python3.7/site-packages/MySQLdb/_mysql.cpython-37m-arm-linux-gnueabihf.so: failed to map segment from shared object During handling of the above exception, another exception occurred: Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/{path-to-venv}/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 

    so if anyone here is like me and has the virtualenv on a mounted partition/disk, you have to mount it with exec, thats the whole problem.

    Remount the partition with executable permission as explained in: https://askubuntu.com/questions/311438/how-to-make-tmp-executable.

    If you mount the drive with fstab, see: https://askubuntu.com/questions/678857/fstab-doesnt-mount-with-exec.

    (well, that was 10hours of trying and debugging well spend lol)

    1

    I agree with Melvyn.

    you can see your MySQL library link by typing like:

    (quantum) chaiyudeMacBook-Pro:quantum chaiyu$ python Python 3.8.7 (v3.8.7:6503f05dd5, Dec 21 2020, 12:45:15) [Clang 6.0 (clang-600.0.57)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> import MySQLdb as Database Traceback (most recent call last): File "/Users/chaiyu/Envs/quantum/lib/python3.8/site-packages/MySQLdb/__init__.py", line 18, in <module> from . import _mysql ImportError: dlopen(/Users/chaiyu/Envs/quantum/lib/python3.8/site-packages/MySQLdb/_mysql.cpython-38-darwin.so, 2): Library not loaded: /usr/local/opt/mysql/lib/libmysqlclient.21.dylib Referenced from: /Users/chaiyu/Envs/quantum/lib/python3.8/site-packages/MySQLdb/_mysql.cpython-38-darwin.so Reason: image not found During handling of the above exception, another exception occurred: Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/Users/chaiyu/Envs/quantum/lib/python3.8/site-packages/MySQLdb/__init__.py", line 24, in <module> version_info, _mysql.version_info, _mysql.__file__ NameError: name '_mysql' is not defined 

    then type:

    (quantum) chaiyudeMacBook-Pro:quantum chaiyu$ otool -L /Users/chaiyu/Envs/quantum/lib/python3.8/site-packages/MySQLdb/_mysql.cpython-38-darwin.so /Users/chaiyu/Envs/quantum/lib/python3.8/site-packages/MySQLdb/_mysql.cpython-38-darwin.so: /usr/local/opt/mysql/lib/libmysqlclient.21.dylib (compatibility version 21.0.0, current version 21.0.0) /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1252.250.1) (quantum) chaiyudeMacBook-Pro:quantum chaiyu$ ls -l /usr/local/opt/mysql/lib/libmysqlclient.21.dylib ls: /usr/local/opt/mysql/lib/libmysqlclient.21.dylib: No such file or directory 

    and then, I found the library MySQL linked to does not exist.

    I had the same error and it was working for a while. I did an update on MacOS BigSur then it stopped working with this error.

    To fix this for me it was a simple uninstall reinstall of both django and mysqlclient.

    The uninstall/reinstall of just mysqlclient itself did not do the trick. Also the order may help. Here are the commands in the order i did it in:

    pip uninstall mysqlclient pip uninstall django pip install django pip install mysqlclient 

    Note: this will install the latest versions, so if you have specific versions, make sure you install those versions.

    0

    This worked for me

    brew install mysql 

    for mac

    Assume you are activating Python 3 venv

    brew install mysql pip install mysqlclient 

    for ubuntu interminal run

    sudo apt-get install python3-dev default-libmysqlclient-dev build-essential pip install mysqlclient 

    for # Red Hat / CentOS

    sudo yum install python3-devel mysql-devel pip install mysqlclient 

    Finally after spending like decade on this issue found a good answer.

    mysqlclient is a Python 3 compatible fork of the original Python MySQL driver, MySQLdb. It still provides a Python module called MySQLdb. On install, it compiles against either the MariaDB client library or MySQL client library - whichever one you have installed.

    Solution Found Here By Adam(Genius Guy)

    From Apple Silicon M1 Mac and a conda environment the only solution that worked for me was to use Miniforge installation, which is comparable to Miniconda, but with conda-forge as the default channel. There is an Apple Silicon option that solved the problem for me: https://github.com/Haydnspass/miniforge#download

    (Solution from https://towardsdatascience.com/using-conda-on-an-m1-mac-b2df5608a141)

    2

    If you're using bash, use:

    open -t .bash_profile 

    and add the following:

    export DYLD_LIBRARY_PATH="/usr/local/mysql/lib:$PATH" 

    If you're using Zsh, use:

    open -t ~/.zshrc 

    and add the following:

    export DYLD_LIBRARY_PATH="/usr/local/mysql/lib:$PATH" 

    although I used _mysql for 10 years, the MySQL website tells the recommended way for Python 3 at: https://dev.mysql.com/doc/connector-python/en/preface.html

    Conversion may take some time, but for me less than an hour. Here an example with new code:

    import mysql.connector cnx = mysql.connector.connect(user='mensfort', password='zhongcan', host='127.0.0.1', database='restaurant') cursor = cnx.cursor() cursor.execute('select dongle from personnel') for dongle in cursor: print(dongle) cursor.close() cnx.close() 

    Please use your own password, database name, queries, this is just an example.

    Please do UNINSTALL this. This is not working for me:

    pip uninstall mysql-connector 

    Please INSTALL this first. It works great:

    pip install mysql-connector-python 

    On a MacBook Pro M1 macOS Monterey, running this command didn't work:

    export DYLD_LIBRARY_PATH="/usr/local/mysql/lib:$PATH"

    But this worked for me:

    export DYLD_LIBRARY_PATH=/usr/local/mysql/lib 

    Reverting back from MySQL Server 8.x.x to 5.7.x worked for me.

    Django supports MySQL 5.5.x - 5.7.x. MySQL 8 and later aren’t supported.

    Found @ Django Docs

    ncG1vNJzZmirpJawrLvVnqmfpJ%2Bse6S7zGiorp2jqbawutJobWxpYG6GeYOOp5imnZWnv7C%2BjKeYpp1dosa0vctmoKxlnqTBbrDEn6CnnZRirqfAxKtkrJ2kqbavs4ycn5qml5p6tbuMprCsqZw%3D