Django项目找不到MySQLdb模块及无法安装mysql-python问题(mac os)

今天用Mac电脑搭建Django项目,执行启动脚本时,出现“raise ImproperlyConfigured("Error loading MySQLdb module: %s" % e)django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module: No module named MySQLdb”

提示没有MySQLdb模块。此时想到去安装python连接mysql的模块mysql-python

1 执行安装mysql-python

命令:pip install mysql-python

返回:提示报错

_mysql.c:44:10: fatal error: 'my_config.h' file not found

  #include "my_config.h"

           ^~~~~~~~~~~~~

  1 error generated.

  error: command 'cc' failed with exit status 1

  ----------------------------------------

  ERROR: Failed building wheel for MySQL-python

分析:mysql头文件没有此时需要安装mysql-connector-c

2 检查mysql-connector-c有没有

命令:brew search mysql-connector-c

返回:==> Formulae

mysql-connector-c                        mysql-connector-c++

命令:brew list|grep mysql

返回:mysql

3 用brew安装mysql-connector-c

命令:brew install mysql-connector-c

返回:又开始报了错

Error: Cannot install mysql-connector-c because conflicting formulae are installed.

  mysql: because both install MySQL client libraries

Please `brew unlink mysql` before continuing.

Unlinking removes a formula's symlinks from /usr/local. You can

link the formula again after the install finishes. You can --force this

install, but the build may fail or cause obscure side effects in the

resulting software.

分析:按照提示先unlink mysql

4 unlink mysql

命令:brew unlink mysql

返回:Unlinking /usr/local/Cellar/mysql/8.0.16... 87 symlinks removed

5 再次安装mysql-connector-c

命令: brew install mysql-connector-c

返回:

==> Downloading https://homebrew.bintray.com/bottles/mysql-connector-c-6.1.11.mo

==> Downloading from https://akamai.bintray.com/e6/e659605251a2ac9a57da2c781e89d

######################################################################## 100.0%

==> Pouring mysql-connector-c-6.1.11.mojave.bottle.tar.gz

 /usr/local/Cellar/mysql-connector-c/6.1.11: 79 files, 15.3MB

此时查看mysql-connector-c已经安装成功,离成功近了一步

由于刚刚unlink了mysql,这时候还需要link回来

6 link mysql

命令:brew link mysql

返回:Linking /usr/local/Cellar/mysql/8.0.16... 

Error: Could not symlink bin/my_print_defaults

Target /usr/local/bin/my_print_defaults

is a symlink belonging to mysql-connector-c. You can unlink it:

  brew unlink mysql-connector-c

To force the link and overwrite all conflicting files:

  brew link --overwrite mysql

To list all files that would be deleted:

  brew link --overwrite --dry-run mysql

分析:按照提示强制link

7 强制link mysql

命令:brew link --overwrite mysql

返回:Linking /usr/local/Cellar/mysql/8.0.16... 124 symlinks created

此时link成功 然后再次安装mysql-mython

8 再次安装mysql-mython

命令:pip install mysql-python

返回:报了一个其他的错

cc -bundle -undefined dynamic_lookup -Wl,-F. build/temp.macosx-10.14-intel-2.7/_mysql.o -L/usr/local/Cellar/mysql/8.0.16/lib -lmysqlclient -lssl -lcrypto -o build/lib.macosx-10.14-intel-2.7/_mysql.so

  ld: library not found for -lssl

  clang: error: linker command failed with exit code 1 (use -v to see invocation)

  error: command 'cc' failed with exit status 1

  ----------------------------------------

  ERROR: Failed building wheel for mysql-python

  Running setup.py clean for mysql-python

Failed to build mysql-python

Installing collected packages: mysql-python

  Running setup.py install for mysql-python ... error

其实这时候你如果安装的是mysqlclient也是报的这个错。

9 解决方法1

使用pymysql模块(需要安装pip install pymysql)

在Django工程下的__init__.py中添加:

import pymysql

pymysql.install_as_MySQLdb()

欢迎大家指出第8步的问题该如何解决

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值