NameError: name ‘_mysql‘ is not defined

报错信息

Traceback (most recent call last):
  File "/Users/xuruilong/Desktop/cmabc_back/.enve/lib/python3.9/site-packages/MySQLdb/__init__.py", line 18, in 
    from . import _mysql
ImportError: dlopen(/Users/xuruilong/Desktop/cmabc_back/.enve/lib/python3.9/site-packages/MySQLdb/_mysql.cpython-39-darwin.so, 0x0002): Library not loaded: /opt/homebrew/opt/mysql/lib/libmysqlclient.22.dylib
  Referenced from: <26B50CE3-5E17-388D-9A37-F5572F81350B> /Users/xuruilong/Desktop/cmabc_back/.enve/lib/python3.9/site-packages/MySQLdb/_mysql.cpython-39-darwin.so
  Reason: tried: '/opt/homebrew/opt/mysql/lib/libmysqlclient.22.dylib' (no such file), '/System/Volumes/Preboot/Cryptexes/OS/opt/homebrew/opt/mysql/lib/libmysqlclient.22.dylib' (no such file), '/opt/homebrew/opt/mysql/lib/libmysqlclient.22.dylib' (no such file), '/usr/local/lib/libmysqlclient.22.dylib' (no such file), '/usr/lib/libmysqlclient.22.dylib' (no such file, not in dyld cache)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "", line 1, in 
  File "/Users/xuruilong/Desktop/cmabc_back/.enve/lib/python3.9/site-packages/MySQLdb/__init__.py", line 24, in 
    version_info, _mysql.version_info, _mysql.__file__
NameError: name '_mysql' is not defined

解决方法

  1. 使用brew 安装mysql-client
    brew install mysql-client
  2. 创建一个lib目录,如果已经存在这个目录,则可以忽略
    sudo mkdir -p /usr/local/lib
    
  3. 创建一个软链接
    # 前面是libmysqlclient这个文件的路径
     ln -s /opt/homebrew/Cellar/mysql-client/8.1.0/lib/libmysqlclient.22.dylib /usr/local/lib 
    

测试

NameError: name ‘_mysql‘ is not defined_第1张图片

你可能感兴趣的:(mysql,django)