macos Django找不到mysqlclient

时间:2019-12-11 15:34:43

标签: python mysql django

我是Django 2.2.8 python3.7

我创建了一个Django虚拟环境,我也安装了mysqlclient

这是我的所有书包,我有mysqlclient。

Package             Version
------------------- -------
asgiref             3.2.3  
Django              2.2.8  
django-filter       2.2.0  
djangorestframework 3.10.3 
mysqlclient         1.4.6  
pip                 19.0.3 
pytz                2019.3 
setuptools          40.8.0 
sqlparse            0.3.0  

除数据库链接外,我没有进行任何设置

这是错误消息

Traceback (most recent call last):
  File "/Users/a1/PycharmProjects/xuexi/xunilufei/lufei01/lib/python3.7/site-packages/django/db/backends/mysql/base.py", line 15, in <module>
    import MySQLdb as Database
  File "/Users/a1/PycharmProjects/xuexi/xunilufei/lufei01/lib/python3.7/site-packages/MySQLdb/__init__.py", line 18, in <module>
    from . import _mysql
ImportError: dlopen(/Users/a1/PycharmProjects/xuexi/xunilufei/lufei01/lib/python3.7/site-packages/MySQLdb/_mysql.cpython-37m-darwin.so, 2): Library not loaded: @rpath/libmysqlclient.21.dylib
  Referenced from: /Users/a1/PycharmProjects/xuexi/xunilufei/lufei01/lib/python3.7/site-packages/MySQLdb/_mysql.cpython-37m-darwin.so
  Reason: image not found

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "manage.py", line 21, in <module>
    main()
  File "manage.py", line 17, in main
    execute_from_command_line(sys.argv)
  File "/Users/a1/PycharmProjects/xuexi/xunilufei/lufei01/lib/python3.7/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
    utility.execute()
  File "/Users/a1/PycharmProjects/xuexi/xunilufei/lufei01/lib/python3.7/site-packages/django/core/management/__init__.py", line 357, in execute
    django.setup()
  File "/Users/a1/PycharmProjects/xuexi/xunilufei/lufei01/lib/python3.7/site-packages/django/__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/Users/a1/PycharmProjects/xuexi/xunilufei/lufei01/lib/python3.7/site-packages/django/apps/registry.py", line 114, in populate
    app_config.import_models()
  File "/Users/a1/PycharmProjects/xuexi/xunilufei/lufei01/lib/python3.7/site-packages/django/apps/config.py", line 211, in import_models
    self.models_module = import_module(models_module_name)
  File "/Library/Frameworks/Python.framework/Versions/3.7/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/a1/PycharmProjects/xuexi/xunilufei/lufei01/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/a1/PycharmProjects/xuexi/xunilufei/lufei01/lib/python3.7/site-packages/django/contrib/auth/base_user.py", line 47, in <module>
    class AbstractBaseUser(models.Model):
  File "/Users/a1/PycharmProjects/xuexi/xunilufei/lufei01/lib/python3.7/site-packages/django/db/models/base.py", line 117, in __new__
    new_class.add_to_class('_meta', Options(meta, app_label))
  File "/Users/a1/PycharmProjects/xuexi/xunilufei/lufei01/lib/python3.7/site-packages/django/db/models/base.py", line 321, in add_to_class
    value.contribute_to_class(cls, name)
  File "/Users/a1/PycharmProjects/xuexi/xunilufei/lufei01/lib/python3.7/site-packages/django/db/models/options.py", line 204, in contribute_to_class
    self.db_table = truncate_name(self.db_table, connection.ops.max_name_length())
  File "/Users/a1/PycharmProjects/xuexi/xunilufei/lufei01/lib/python3.7/site-packages/django/db/__init__.py", line 28, in __getattr__
    return getattr(connections[DEFAULT_DB_ALIAS], item)
  File "/Users/a1/PycharmProjects/xuexi/xunilufei/lufei01/lib/python3.7/site-packages/django/db/utils.py", line 201, in __getitem__
    backend = load_backend(db['ENGINE'])
  File "/Users/a1/PycharmProjects/xuexi/xunilufei/lufei01/lib/python3.7/site-packages/django/db/utils.py", line 110, in load_backend
    return import_module('%s.base' % backend_name)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "/Users/a1/PycharmProjects/xuexi/xunilufei/lufei01/lib/python3.7/site-packages/django/db/backends/mysql/base.py", line 20, in <module>
    ) from err
django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module.
Did you install mysqlclient?

我真的找不到问题

谢谢您的帮助 我没有进行任何设置 除了数据库链接,我没有进行任何设置

DATABASES = {
   'default': {
          'ENGINE': 'django.db.backends.mysql', 
          'NAME': 'jialuffei',                       
          'USER': 'root',                       
          # 'PASSWORD': 'mysql',                   
          'PASSWORD': '060606',                   
          'HOST': '',                           
          'PORT': '',                           
          'OPTIONS': {
             'autocommit': True,
         }
    }
}

2 个答案:

答案 0 :(得分:1)

可能不是,但是可以肯定的是,您是否在settings.py中添加并配置了数据库引擎?我的示例是Django(1.10)的旧版本,但仍适用于您的版本,我记得有问题并必须配置引擎。

我怀疑,如果未配置该错误,您甚至不会得到该错误,因此此答案可能是不必要的,但是我只是在思考过程。

# https://docs.djangoproject.com/en/1.10/ref/settings/#databases
# https://docs.djangoproject.com/en/2.2/ref/settings/#databases
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'db_name',
        'HOST': 'localhost',
        'PORT': '3306',
        'USER': 'db_user',
        'PASSWORD': 'db_password',
    }
}

答案 1 :(得分:0)

您可能要使用mysql连接器而不是DATABASES = { 'default': { 'ENGINE': 'mysql.connector.django', 'NAME': 'db_name', 'HOST': 'localhost', 'PORT': '3306', 'USER': 'db_user', 'PASSWORD': 'db_password', } } 。您可以签出mysql配置here

{{1}}