DJANGO - My solution on Issue ('_mysql' is not defined) in Lesson 9 of the Setting up the Database Section

Hi, I thought I would share this as I really struggled in my Mac M1 running Big Sur trying to make this work, for about an hour…

After following Mosh instructions, the server was crashing with a very long error ending up like:

NameError: name '_mysql' is not defined

I was able to solve it with a combination of two solutions on this StackOverflow link:

python - NameError: name ‘_mysql’ is not defined after setting change to mysql - Stack Overflow

First:

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**

And Second:

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

After this, I restarted the terminal and ran python manage.py runserver again. No issues.

2 Likes

Where do I need to add that path as I add it it give a syntax error warning ?

For anyone also coming to this section…

This linked worked for me…

Some Specs:

active environment : django_practice
    active env location : /Users/samlafell/opt/miniconda3/envs/django_practice
            shell level : 3
       user config file : /Users/samlafell/.condarc
 populated config files : /Users/samlafell/.condarc
          conda version : 4.11.0
    conda-build version : not installed
         python version : 3.9.5.final.0
       virtual packages : __osx=10.16=0
                          __unix=0=0
                          __archspec=1=x86_64
       base environment : /Users/samlafell/opt/miniconda3  (writable)
      conda av data dir : /Users/samlafell/opt/miniconda3/etc/conda

...

             platform : osx-64
             user-agent : conda/4.11.0 requests/2.27.1 CPython/3.9.5 Darwin/21.3.0 OSX/10.16
                UID:GID : 501:20
             netrc file : None
           offline mode : False

using Mac M1 2021 and Conda (no homebrew)

I was stuck on this for 45 minutes! Thanks