使用 pip 和 Python 3.6 在 Ubuntu 16.04 上安装 mysqlclient 时出错

Error Installing mysqlclient on Ubuntu 16.04 using pip and Python 3.6

我尝试在 Ubuntu 16.04 Xenial 上安装 mysqlclient 时出现奇怪的错误点 + Python 3.6:

 pip install mysqlclient

输出:

 _mysql.c:40:20: fatal error: Python.h: No such file or directory
 compilation terminated.
 error: command 'x86_64-linux-gnu-gcc' failed with exit status 1

installation requirements 之后,我已经尝试安装所需的库,但到目前为止运气不好。

sudo apt-get install python3-dev libmysqlclient-dev

有人知道这个问题的解决方法吗?

我发现了问题,似乎是为了在 python3.6 中安装 mysqlclient python3.6-dev 是必需的.

只需打开终端并运行执行以下命令:

sudo apt-get install python3.6-dev libmysqlclient-dev

您在尝试安装库时可能会遇到以下错误:

Reading state information...
E: Unable to locate package python3.6-dev
E: Couldn't find any package by glob 'python3.6-dev'
E: Couldn't find any package by regex 'python3.6-dev'

如果是这种情况,请先尝试添加以下存储库:

sudo add-apt-repository ppa:jonathonf/python-3.6
sudo apt-get update

这样做之后,您应该能够同时安装 python3.6python3.6-dev包。


我在 python 3.6.3 上安装 mysqlclient 时遇到了问题,所以 我降级到版本 3.6.2 并且它有效。

sudo apt-get install build-essential checkinstall
sudo apt-get install libreadline-gplv2-dev  libncursesw5-dev libssl-dev libsqlite3-dev tk-dev libgdbm-dev libc6-dev libbz2-dev
cd /usr/bin
sudo wget https://www.python.org/ftp/python/3.6.2/Python-3.6.2.tgz
sudo tar xzf Python-3.6.2.tgz
cd Python-3.6.2
sudo ./configure --enable-optimizations
sudo make altinstall

python3.6 应该指向 3.6.2

python --version # Python 3.6.2

现在安装 mysqlclient

python3.6 -m pip install mysqlclient

read more

我遇到过同样的问题:

#sudo pip3 install mysqlclient fails with mysql_config not found
sudo apt-get install libmysqlclient-dev
#without pip3 it will not going to work for python3
sudo pip3 install mysqlclient

用上面的方法解决了。

reference link

我遇到了这个错误

/usr/bin/ld: cannot find -lssl
/usr/bin/ld: cannot find -lcrypto
collect2: error: ld returned 1 exit status
error: command 'x86_64-linux-gnu-gcc' failed with exit status 1

1个解

打开新终端并运行这个命令

sudo apt-get install libssl-dev

之后安装 mysqlclient

pip install mysqlclient

2 解

安装MySQL数据库连接器

First thing we will need to do is install python3-dev. You can install python3-dev by running the following command:

sudo apt-get install python3-dev

Once python3-dev is installed, we can install the necessary Python and MySQL development headers and libraries:

sudo apt-get install python3-dev libmysqlclient-dev

Then, we will use pip3 to install the mysqlclient library from PyPi. Since our version of pip points to pip3, we can just use pip.

pip install mysqlclient

参考Link: How To Create a Django App and Connect it to a Database