gpt4 book ai didi

python - NameError:设置更改为mysql后未定义名称 '_mysql'

转载 作者:行者123 更新时间:2023-12-02 09:47:09 28 4
gpt4 key购买 nike

我的本地计算机上有一个运行sqlite3 db的Django博客。我想要的是

  • 将sqlite3 db转换为mysql db
  • 更改Django settings.py文件以提供MySQL数据库

  • 在进行第一步之前,我跳到了第二步。我关注了 this web page(在MacOS上)。我在root用户上创建了名为 djangolocaldb的数据库,并在 /etc/mysql/my.cnf中包含了这些信息,如下所示:
    # /etc/mysql/my.cnf

    [client]
    database=djangolocaldb
    user=root
    password=ROOTPASSWORD
    default-character-set=utf8
    当然,我创建了db,但没有创建表。
    mysql> show databases;
    +--------------------+
    | Database |
    +--------------------+
    | djangolocaldb |
    | employees |
    | information_schema |
    | mydatabase |
    | mysql |
    | performance_schema |
    | sys |
    +--------------------+
    7 rows in set (0.00 sec)
    我像建议的网页一样更改了 settings.py。这是如何做:
    # 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',
    }
    }
    }

    ...
    现在,当我在激活 python manage.py runserver的情况下运行 venv时,我得到了这样的残酷回溯(我先运行 python manage.py migrate,并且回溯看上去几乎相同):
    (.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
    因此,此 NameError: name '_mysql' is not defined就是问题所在。我之前安装了 mysqlclient,更改了 settings.py,使mysql中的数据库,但没有一个步骤使它有所帮助。
    而且我注意到,即使我将 settings.py更改回sqlite3,我的博客也会吐出相同的_mysql undefined 错误。所以我最终恢复了提交,现在我回到了sqlite3(至少我的博客正在运行它)。
    我猜可能是我没有先转换数据,但我不是100%确信。
    任何建议将不胜感激。先感谢您!
    于2020年8月18日编辑
    如果以某种方式运行 _mysql not defined仍然看到 python manage.py migrate错误,请检查以下两个设置。
  • 如果像这样在python虚拟环境中运行mysqlclient时看到pip freeze,则
  • (.venv) ➜  SqlDjango git:(master) ✗ pip freeze
    ...
    Django==3.0.8
    mysqlclient==2.0.1
    ...
    确保您在python虚拟环境中安装了 mysqlclient,而不是 mysql-client。前者支持python3,而后者支持python2,我认为这可能不是您想要的。
  • 确保将以下行放入bash配置文件中(对于zsh为~/.zshrc,对于bash为~/.bashrc~/.bash_profile等)
  • export DYLD_LIBRARY_PATH=/usr/local/mysql/lib/
    之后,通过runnig source ~/.your_shell_config_file进行更改。请参阅 python manage.py migrate的作品。

    最佳答案

    所以作为一个完整的答案:
    如果您使用python软件包mysqlclient,则仍然需要从Oracle / MySQL安装mysql客户端。它包含python包使用的C库。让事情变得更加混乱:python包实际上是用C编写的,以提高速度。要在MacOS上安装此库:

    % brew install mysql-client
    还有一个 pure python package,具有更有吸引力的MIT许可证,如果您的公司或客户不允许GPL,则可以作为解决方案。但是,它不受官方支持,并且在两个发行版之间可能会发生一些细微的错误。 YMMV。

    关于python - NameError:设置更改为mysql后未定义名称 '_mysql',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63109987/

    28 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com