Skip to content

mysqlclient for windows

直接使用pip命令安装,老天保佑你不报错!

pip install mysqlclient

如果报错,就去这个链接中下载对应的whl文件: https://www.lfd.uci.edu/~gohlke/pythonlibs/#mysqlclient1832669388868681728.png 然后使用下面命令进行安装,我这里以3.6版本为例:

pip install mysqlclient‑1.3.13‑cp36‑cp36m‑win_amd64.whl

如果执行报错,就把amd64位的换成32位的再试试。 参考:https://blog.csdn.net/cn_1937/article/details/81533544

ubuntu

我没有试过在Ubuntu上安装,也是翻帖子翻到了一个解决方案,套路也是先安装依赖,然后再安装mysqlclient模块。

sudo apt-get install libmysqlclient-dev
sudo apt install libssl-dev
sudo apt install libcrypto++-dev
sudo pip3 install mysqlclient

在这个博客的评论区:https://blog.csdn.net/cn_1937/article/details/81533544

常见报错

Ubuntu20.04安装mysqlclient失败

Ubuntu20.04 + python3.9

bash
(day06) root@VM-4-6-ubuntu:/data/www/day06# pip install mysqlclient
Looking in indexes: http://mirrors.tencentyun.com/pypi/simple
Collecting mysqlclient
  Using cached http://mirrors.tencentyun.com/pypi/packages/de/9c/b176826e8994551ce826404dab97e305a4bb76c8b0a4e016fabda2901c71/mysqlclient-2.2.0.tar.gz (89 kB)
  Installing build dependencies ... done
  Getting requirements to build wheel ... error
  error: subprocess-exited-with-error
  
  × Getting requirements to build wheel did not run successfully.
 exit code: 1
  ╰─> [22 lines of output]
      Trying pkg-config --exists mysqlclient
      Command 'pkg-config --exists mysqlclient' returned non-zero exit status 1.
      Trying pkg-config --exists mariadb
      Command 'pkg-config --exists mariadb' returned non-zero exit status 1.
      Traceback (most recent call last):
        File "/envs/day06/lib/python3.8/site-packages/pip/_vendor/pyproject_hooks/_in_process/_in_process.py", line 353, in <module>
          main()
        File "/envs/day06/lib/python3.8/site-packages/pip/_vendor/pyproject_hooks/_in_process/_in_process.py", line 335, in main
          json_out['return_val'] = hook(**hook_input['kwargs'])
        File "/envs/day06/lib/python3.8/site-packages/pip/_vendor/pyproject_hooks/_in_process/_in_process.py", line 118, in get_requires_for_build_wheel
          return hook(config_settings)
        File "/tmp/pip-build-env-fr9ts06w/overlay/lib/python3.8/site-packages/setuptools/build_meta.py", line 341, in get_requires_for_build_wheel
          return self._get_build_requires(config_settings, requirements=['wheel'])
        File "/tmp/pip-build-env-fr9ts06w/overlay/lib/python3.8/site-packages/setuptools/build_meta.py", line 323, in _get_build_requires
          self.run_setup()
        File "/tmp/pip-build-env-fr9ts06w/overlay/lib/python3.8/site-packages/setuptools/build_meta.py", line 338, in run_setup
          exec(code, locals())
        File "<string>", line 154, in <module>
        File "<string>", line 48, in get_config_posix
        File "<string>", line 27, in find_package_name
      Exception: Can not find valid pkg-config name.
      Specify MYSQLCLIENT_CFLAGS and MYSQLCLIENT_LDFLAGS env vars manually
      [end of output]
  
  note: This error originates from a subprocess, and is likely not a problem with pip.
error: subprocess-exited-with-error

解决思路下载依赖:

bash
sudo apt-get update
sudo apt-get install libmysqlclient-dev -y
sudo apt-get install build-essential libssl-dev libffi-dev -y
sudo apt-get install python3-dev -y

然后重新执行安装就没问题了:

bash
(day06) root@VM-4-6-ubuntu:/data/www/day06# pip install mysqlclient
Looking in indexes: http://mirrors.tencentyun.com/pypi/simple
Collecting mysqlclient
  Using cached http://mirrors.tencentyun.com/pypi/packages/de/9c/b176826e8994551ce826404dab97e305a4bb76c8b0a4e016fabda2901c71/mysqlclient-2.2.0.tar.gz (89 kB)
  Installing build dependencies ... done
  Getting requirements to build wheel ... done
  Installing backend dependencies ... done
  Preparing metadata (pyproject.toml) ... done
Building wheels for collected packages: mysqlclient
  Building wheel for mysqlclient (pyproject.toml) ... done
  Created wheel for mysqlclient: filename=mysqlclient-2.2.0-cp38-cp38-linux_x86_64.whl size=128510 sha256=86b1b9de4d3857dd3d0204c0e423b699a21c5e470546d12950cbd004889239af
  Stored in directory: /root/.cache/pip/wheels/56/b8/93/ee16ec072a1842a902f8dba67399e31ec1ec5941e98ec835aa
Successfully built mysqlclient
Installing collected packages: mysqlclient
Successfully installed mysqlclient-2.2.0
(day06) root@VM-4-6-ubuntu:/data/www/day06#

参考:https://code84.com/30903.html