最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

python - NameError: name '_mysql' is not defined Mac Django - Stack Overflow

programmeradmin2浏览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: /

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!

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/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!

Share Improve this question edited yesterday Aditi B asked Feb 16 at 21:02 Aditi BAditi B 12 bronze badges New contributor Aditi B is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
Add a comment  | 

2 Answers 2

Reset to default 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.

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

发布评论

评论列表(0)

  1. 暂无评论