简体   繁体   中英

Trying to connect Django and MySQL

I've been using Django with Sqlite to learn and now wanted to start on a first real project. I'm looking to use MySQL as the database. I installed MySQLClient but keep running into this error when trying to run a migration. I've spent several hours trying to look through other posts but just can't seem to figure this out. Any direction on this out would be greatly appreciated.

My setup is: Python - 3.8 Django - 3.0.6 MySQL - 8 MSQLClient - 1.4.6 macOS - Catalina

UPDATE: I was able to install MySQL client using pip install instead of using brew install or the tar file. Reduced the errors down to this:

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/usman/PycharmProjects/Webproject/venv/lib/python3.8/site-packages/django/core/management/__init__.py", line 401, in execute_from_command_line
    utility.execute()
  File "/Users/usman/PycharmProjects/Webproject/venv/lib/python3.8/site-packages/django/core/management/__init__.py", line 377, in execute
    django.setup()
  File "/Users/usman/PycharmProjects/Webproject/venv/lib/python3.8/site-packages/django/__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/Users/usman/PycharmProjects/Webproject/venv/lib/python3.8/site-packages/django/apps/registry.py", line 91, in populate
    app_config = AppConfig.create(entry)
  File "/Users/usman/PycharmProjects/Webproject/venv/lib/python3.8/site-packages/django/apps/config.py", line 90, in create
    module = import_module(entry)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 783, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/Users/usman/PycharmProjects/Webproject/venv/lib/python3.8/site-packages/MySQLdb/__init__.py", line 18, in <module>
    from . import _mysql
ImportError: dlopen(/Users/usman/PycharmProjects/Webproject/venv/lib/python3.8/site-packages/MySQLdb/_mysql.cpython-38-darwin.so, 2): Library not loaded: @rpath/libmysqlclient.21.dylib
  Referenced from: /Users/usman/PycharmProjects/Webproject/venv/lib/python3.8/site-packages/MySQLdb/_mysql.cpython-38-darwin.so
  Reason: image not found

After struggling with this for a few days, I finally got things to work.

Here is the process I followed, in case it helps anyone else.

  1. I had been playing around with a lot of tools/installations. So, I decided to do a clean OS install.

  2. Used Homebrew to install Python and set Python 3 as my default for venv following this guide: https://opensource.com/article/19/5/python-3-default-mac

  3. Installed MySQL

  4. Installed MySql-Client using Homebrew

  5. Pip install Wheel & Pip Install mysqlclient

  6. Run migration and good to go!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM