Django:未加载python manage.py createdb库:libmysqlclient.20.dylib

时间:2018-12-03 03:45:58

标签: python mysql django

我对Django几乎是陌生的。

当我使用python manage.py createdb发送到django项目的终端时,出现了错误。

Traceback (most recent call last):
  File "/Users/{ItIsMyName}/.pyenv/versions/anaconda3-5.0.0/lib/python3.6/site-packages/django/db/backends/mysql/base.py", line 26, in <module>
    import MySQLdb as Database
  File "/Users/{ItIsMyName}/.pyenv/versions/anaconda3-5.0.0/lib/python3.6/site-packages/MySQLdb/__init__.py", line 18, in <module>
    import _mysql
ImportError: dlopen(/Users/{ItIsMyName}/.pyenv/versions/anaconda3-5.0.0/lib/python3.6/site-packages/_mysql.cpython-36m-darwin.so, 2): Library not loaded: /usr/local/opt/mysql/lib/libmysqlclient.20.dylib
  Referenced from: /Users/{ItIsMyName}/.pyenv/versions/anaconda3-5.0.0/lib/python3.6/site-packages/_mysql.cpython-36m-darwin.so
  Reason: image not found

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "manage.py", line 14, in <module>
    execute_from_command_line(sys.argv)
  File "/Users/{ItIsMyName}/.pyenv/versions/anaconda3-5.0.0/lib/python3.6/site-packages/django/core/management/__init__.py", line 364, in execute_from_command_line
    utility.execute()
  File "/Users/{ItIsMyName}/.pyenv/versions/anaconda3-5.0.0/lib/python3.6/site-packages/django/core/management/__init__.py", line 338, in execute
    django.setup()
  File "/Users/{ItIsMyName}/.pyenv/versions/anaconda3-5.0.0/lib/python3.6/site-packages/django/__init__.py", line 27, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/Users/{ItIsMyName}/.pyenv/versions/anaconda3-5.0.0/lib/python3.6/site-packages/django/apps/registry.py", line 108, in populate
    app_config.import_models()
  File "/Users/{ItIsMyName}/.pyenv/versions/anaconda3-5.0.0/lib/python3.6/site-packages/django/apps/config.py", line 202, in import_models
    self.models_module = import_module(models_module_name)
  File "/Users/{ItIsMyName}/.pyenv/versions/anaconda3-5.0.0/lib/python3.6/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 978, in _gcd_import
  File "<frozen importlib._bootstrap>", line 961, in _find_and_load
  File "<frozen importlib._bootstrap>", line 950, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 655, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 678, in exec_module
  File "<frozen importlib._bootstrap>", line 205, in _call_with_frames_removed
  File "/Users/{ItIsMyName}/.pyenv/versions/anaconda3-5.0.0/lib/python3.6/site-packages/django/contrib/auth/models.py", line 4, in <module>
    from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
  File "/Users/{ItIsMyName}/.pyenv/versions/anaconda3-5.0.0/lib/python3.6/site-packages/django/contrib/auth/base_user.py", line 52, in <module>
    class AbstractBaseUser(models.Model):
  File "/Users/{ItIsMyName}/.pyenv/versions/anaconda3-5.0.0/lib/python3.6/site-packages/django/db/models/base.py", line 124, in __new__
    new_class.add_to_class('_meta', Options(meta, app_label))
  File "/Users/{ItIsMyName}/.pyenv/versions/anaconda3-5.0.0/lib/python3.6/site-packages/django/db/models/base.py", line 325, in add_to_class
    value.contribute_to_class(cls, name)
  File "/Users/{ItIsMyName}/.pyenv/versions/anaconda3-5.0.0/lib/python3.6/site-packages/django/db/models/options.py", line 214, in contribute_to_class
    self.db_table = truncate_name(self.db_table, connection.ops.max_name_length())
  File "/Users/{ItIsMyName}/.pyenv/versions/anaconda3-5.0.0/lib/python3.6/site-packages/django/db/__init__.py", line 33, in __getattr__
    return getattr(connections[DEFAULT_DB_ALIAS], item)
  File "/Users/{ItIsMyName}/.pyenv/versions/anaconda3-5.0.0/lib/python3.6/site-packages/django/db/utils.py", line 211, in __getitem__
    backend = load_backend(db['ENGINE'])
  File "/Users/{ItIsMyName}/.pyenv/versions/anaconda3-5.0.0/lib/python3.6/site-packages/django/db/utils.py", line 115, in load_backend
    return import_module('%s.base' % backend_name)
  File "/Users/{ItIsMyName}/.pyenv/versions/anaconda3-5.0.0/lib/python3.6/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "/Users/{ItIsMyName}/.pyenv/versions/anaconda3-5.0.0/lib/python3.6/site-packages/django/db/backends/mysql/base.py", line 30, in <module>
    'Did you install mysqlclient or MySQL-python?' % e
django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module: dlopen(/Users/{ItIsMyName}/.pyenv/versions/anaconda3-5.0.0/lib/python3.6/site-packages/_mysql.cpython-36m-darwin.so, 2): Library not loaded: /usr/local/opt/mysql/lib/libmysqlclient.20.dylib
  Referenced from: /Users/{ItIsMyName}/.pyenv/versions/anaconda3-5.0.0/lib/python3.6/site-packages/_mysql.cpython-36m-darwin.so
  Reason: image not found.
Did you install mysqlclient or MySQL-python?

我在python shell上也有错误

Python 3.6.2 |Anaconda custom (64-bit)| (default, Sep 21 2017, 18:29:43)
[GCC 4.2.1 Compatible Clang 4.0.1 (tags/RELEASE_401/final)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import MySQLdb
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/masanarimakino/.pyenv/versions/anaconda3-5.0.0/lib/python3.6/site-packages/MySQLdb/__init__.py", line 18, in <module>
    import _mysql
ImportError: dlopen(/Users/masanarimakino/.pyenv/versions/anaconda3-5.0.0/lib/python3.6/site-packages/_mysql.cpython-36m-darwin.so, 2): Library not loaded: /usr/local/opt/mysql/lib/libmysqlclient.20.dylib
  Referenced from: /Users/masanarimakino/.pyenv/versions/anaconda3-5.0.0/lib/python3.6/site-packages/_mysql.cpython-36m-darwin.so
  Reason: image not found

我一次又一次地在stackoverflow上搜索了类似的问题。但是它们都不是Django上的错误。

在这里,我几乎倾向于理解错误的原因,这是mysql的版本升级。

但是我不知道该如何解决。

所以,请告诉我如何解决。

此外,我现在已经安装了mysqlclient。 (版本1.3.13)

这里是每个版本和环境。
环境
pc:mac os 10.13.1

版本
蟒蛇:3.6.2
夹层:4.3.1
django:1.11.16
mysqlclient:1.3.13


新错误1

Collecting mysqlclient
  Downloading https://files.pythonhosted.org/packages/ec/fd/83329b9d3e14f7344d1cb31f128e6dbba70c5975c9e57896815dbb1988ad/mysqlclient-1.3.13.tar.gz (90kB)
    100% |████████████████████████████████| 92kB 3.2MB/s
    Complete output from command python setup.py egg_info:
    Traceback (most recent call last):
      File "<string>", line 1, in <module>
      File "/private/var/folders/j6/g61rgqwj27z30t9xsxpyqj_00000gn/T/pip-install-ntfyq9j4/mysqlclient/setup.py", line 18, in <module>
        metadata, options = get_config()
      File "/private/var/folders/j6/g61rgqwj27z30t9xsxpyqj_00000gn/T/pip-install-ntfyq9j4/mysqlclient/setup_posix.py", line 60, in get_config
        libraries = [dequote(i[2:]) for i in libs if i.startswith('-l')]
      File "/private/var/folders/j6/g61rgqwj27z30t9xsxpyqj_00000gn/T/pip-install-ntfyq9j4/mysqlclient/setup_posix.py", line 60, in <listcomp>
        libraries = [dequote(i[2:]) for i in libs if i.startswith('-l')]
      File "/private/var/folders/j6/g61rgqwj27z30t9xsxpyqj_00000gn/T/pip-install-ntfyq9j4/mysqlclient/setup_posix.py", line 13, in dequote
        raise Exception("Wrong MySQL configuration: maybe https://bugs.mysql.com/bug.php?id=86971 ?")
    Exception: Wrong MySQL configuration: maybe https://bugs.mysql.com/bug.php?id=86971 ?

    ----------------------------------------
Command "python setup.py egg_info" failed with error code 1 in /private/var/folders/j6/g61rgqwj27z30t9xsxpyqj_00000gn/T/pip-install-ntfyq9j4/mysqlclient/

1 个答案:

答案 0 :(得分:1)

如果您使用的是Linux操作系统,则需要在操作系统上再安装一个软件包:

Linux:

sudo apt-get install libmysqlclient-dev

reference

Mac OS:

brew install mysql

Mac os libmysqlclient-dev

Stackoverflow answer

或Windows

Windows libmysqlclient-dev