解决pip3 install mysqlclinet 报错 OSError: mysql_config not found

  • Django开发项目时使用MySQL数据库,然而部署到Ubuntu服务器的时候会经常出错。

  • Django连接MySQL数据库需要依赖第三方库mysqlclient,然而服务器通过pip3 install mysqlclient 命令安装第三方库mysqlclient时会经常报错:

ERROR: Command errored out with exit status 1:
     command: /home/bright/web/venv/bin/python -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-ffv9cnlj/mysqlclient/setup.py'"'"'; __file__='"'"'/tmp/pip-install-ffv9cnlj/mysqlclient/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' egg_info --egg-base /tmp/pip-install-ffv9cnlj/mysqlclient/pip-egg-info
         cwd: /tmp/pip-install-ffv9cnlj/mysqlclient/
    Complete output (12 lines):
    /bin/sh: 1: mysql_config: not found
    /bin/sh: 1: mariadb_config: not found
    /bin/sh: 1: mysql_config: not found
    Traceback (most recent call last):
      File "", line 1, in <module>
      File "/tmp/pip-install-ffv9cnlj/mysqlclient/setup.py", line 16, in <module>
        metadata, options = get_config()
      File "/tmp/pip-install-ffv9cnlj/mysqlclient/setup_posix.py", line 61, in get_config
        libs = mysql_config("libs")
      File "/tmp/pip-install-ffv9cnlj/mysqlclient/setup_posix.py", line 29, in mysql_config
        raise EnvironmentError("%s not found" % (_mysql_config_path,))
    OSError: mysql_config not found
    ----------------------------------------
ERROR: Command errored out with exit status 1: python setup.py egg_info Check the logs for full command output.

  • apt 安装的 mysql 的 mysql_config 文件缺失了,或者说系统里没有mysql_config 文件,,,,

  • 解决方案:

sudo apt install libmysqlclient-dev

然后再:

pip3 install mysqlclient

发现:

Building wheels for collected packages: mysqlclient
  Building wheel for mysqlclient (setup.py) ... done
  Created wheel for mysqlclient: filename=mysqlclient-1.4.6-cp37-cp37m-linux_x86_64.whl size=100352 sha256=07ec9df2d857581eee041a6ae7740149919a265d19776c7eb8dbce47b4108324
  Stored in directory: /home/bright/.cache/pip/wheels/42/b3/81/881eb2e7c762ba99ec0349eeb2b8b03760871295b5800d7a51
Successfully built mysqlclient
Installing collected packages: mysqlclient
Successfully installed mysqlclient-1.4.6

mysqlclient 安装成功。

这样即使服务器里没有安装MySQL数据库,也不会报错。

你可能感兴趣的:(Django)