0

I was trying to run python manage.py run server while working with Django after changing my settings.py from this:

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

to this:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'storefront',
    }
}

But I was getting this error:

File "/Users/aditi/.local/share/virtualenvs/storefront-4vgosAeV/lib/python3.12/site-packages/django/db/backends/mysql/base.py", line 16, in <module>
    import MySQLdb as Database
  File "/Users/aditi/.local/share/virtualenvs/storefront-4vgosAeV/lib/python3.12/site-packages/MySQLdb/__init__.py", line 24, in <module>
    version_info, _mysql.version_info, _mysql.__file__
                  ^^^^^^
NameError: name '_mysql' is not defined


This is what my __init__.py looks like:
try:
    from MySQLdb.release import version_info
    from . import _mysql

    assert version_info == _mysql.version_info
except Exception:
    raise ImportError(
        "this is MySQLdb version {}, but _mysql is version {!r}\n_mysql: {!r}".format(
            version_info, _mysql.version_info, _mysql.__file__
        )
    )

I downloaded MySQL from this link, the x86 DMG option: https://dev.mysql.com/downloads/mysql/

I have Sonoma 14.3. I am running Python 3.12.7. Please let me know how I can fix this error.

I tried adding this to my .zshrc file:

export PATH="/usr/local/mysql/bin:$PATH"

But it did not work.

UPDATE: I got it to work with PyMySQL, thank you for the help everyone!

2 Answers 2

0

Either "from MySQLdb.release import version_info" or "from . import _mysql" fails, and _mysql is not defined in your except block. It would be better to change your init.py to something like this:

from MySQLdb.release import version_info
from . import _mysql
if version_info != _mysql.version_info:
    raise ImportError(
    "this is MySQLdb version {}, but _mysql is version {!r}\n_mysql: {!r}".format(
        version_info, _mysql.version_info, _mysql.__file__
    )
)

At least, you will see which import fails and probably why.

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

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.