问题解决:from . import _mysql ImportError: Library not loaded: @rpath/libmysqlclient.21.dylib

When connected with today python mysql error:

  File "/Users/zhbink/Documents/GitHub/Scrapy/ArticleSpider/ArticleSpider/pipelines.py", line 5, in <module>
    import MySQLdb
  File "/Users/zhbink/opt/anaconda3/envs/django/lib/python3.7/site-packages/MySQLdb/__init__.py", line 18, in <module>
    from . import _mysql
ImportError: dlopen(/Users/zhbink/opt/anaconda3/envs/django/lib/python3.7/site-packages/MySQLdb/_mysql.cpython-37m-darwin.so, 2): Library not loaded: @rpath/libmysqlclient.21.dylib
  Referenced from: /Users/zhbink/opt/anaconda3/envs/django/lib/python3.7/site-packages/MySQLdb/_mysql.cpython-37m-darwin.so
  Reason: image not found

Library use is MySQLdb, then consider the possibility that I am using this python version is not compatible with the library.
After access to information that MySQLdbis not compatible py3.5 later.


Solution:
Use pymysqlinstead ofMySQLdb

step:

  1. Installation pymysql:pip install pymysql

  2. Open the project __init__.py, or directly at the very beginning of the current py file and add the following:

import pymysql 
pymysql.install_as_MySQLdb()

Here Insert Picture Description

Published 673 original articles · won praise 644 · views 380 000 +

Guess you like

Origin blog.csdn.net/zhaohaibo_/article/details/104452033