尝试“pip install mysqlclient”时出现“OSError:找不到mysql_config” - Django

时间:2017-12-18 14:33:04

标签: pip virtualenv mysql-python macos-high-sierra

我对Django和Web开发相对较新。

我正在尝试

pip install mysqlclient

在我的

virtualenv -p python3

将Django 2.0连接到mySQL。但是,我收到了这个错误:

Collecting mysqlclient
Using cached mysqlclient-1.3.12.tar.gz
Complete output from command python setup.py egg_info:
/bin/sh: mysql_config: command not found
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/private/var/folders/43/md5vpqrx0mx8627sq04slbz00000gn/T/pip-build-l8ea3vja/mysqlclient/setup.py", line 17, in <module>
    metadata, options = get_config()
  File "/private/var/folders/43/md5vpqrx0mx8627sq04slbz00000gn/T/pip-build-l8ea3vja/mysqlclient/setup_posix.py", line 44, in get_config
    libs = mysql_config("libs_r")
  File "/private/var/folders/43/md5vpqrx0mx8627sq04slbz00000gn/T/pip-build-l8ea3vja/mysqlclient/setup_posix.py", line 26, in mysql_config
    raise EnvironmentError("%s not found" % (mysql_config.path,))
OSError: mysql_config not found

----------------------------------------
Command "python setup.py egg_info" failed with error code 1 in /private/var/folders/43/md5vpqrx0mx8627sq04slbz00000gn/T/pip-build-l8ea3vja/mysqlclient/

我几个小时都在寻找答案,是的,我已经检查了thisthis,但似乎没有任何效果。

非常感谢任何帮助!

3 个答案:

答案 0 :(得分:2)

您应该首先克隆mysqlclient repo:

git clone https://github.com/PyMySQL/mysqlclient-python

然后你必须得到mysql-connector-c。你可以这样做:

brew install mysql-connector-c

然后在文本编辑器中打开/usr/local/bin/mysql_config

来自Github issue

mysql_config中有一些行如下:

# Create options 
libs="-L$pkglibdir"
libs="$libs -l "

应该是:

#Create options 
libs="-L$pkglibdir"
libs="$libs -lmysqlclient -lssl -lcrypto"

保存该文件并更改回下载目录的目录。现在打开site.cfg文件。 取消注释该行(删除#)

#mysql_config = /usr/local/bin/mysql_config

保存并运行:

python3 setup.py install

答案 1 :(得分:2)

the python package "mysqlclient" is depended on C system library to drive mysql connect,if you on ubuntu 16.4, just do as below:

sudo apt-get install libmysqlclient-dev
sudo pip install mysqlclient

答案 2 :(得分:0)

对于 CentOS,它对我有用:

yum install gcc mysql-devel openssl-devel

#Install library explicitly, otherwise the python3 setup.py install will fail
pip3 install "mysqlclient==2.0.3" --global-option=build_ext --global-option="-L/usr/lib64/mysql/"