在 ubuntu20 中安装了 mysqlclient,遇到了不少错误,原因在于 mysqlclient 需要依赖一些动态链接库,还有就是和 python 版本有关系

环境介绍:
ubuntu20 出厂自带 python3.8 ,但是我自己安装了 python3.9

使用命令:sudo apt install python3.9 便可以安装 python3.9

子标题:

  • 安装Python mysqlclient出现“OSError: mysql_config not found”错误
  • 跨系统 python 版本安装 python3.9-dev
  • 为 ubuntu20 安装 python3.9-dev
  • ubuntu 自带的 python 是3.8 ,怎么给 3.9 安装 mysqlclient

报错一:

完整报错信息

    ERROR: Command errored out with exit status 1:
     command: /home/bot/.local/share/virtualenvs/django-twitter-IaucORjf/bin/python -c 'import io, os, sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-ad2qu_uy/mysqlclient_df40fd48e0dc469e8a102567b171b787/setup.py'"'"'; __file__='"'"'/tmp/pip-install-ad2qu_uy/mysqlclient_df40fd48e0dc469e8a102567b171b787/setup.py'"'"';f = getattr(tokenize, '"'"'open'"'"', open)(__file__) if os.path.exists(__file__) else io.StringIO('"'"'from setuptools import setup; setup()'"'"');code = f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' egg_info --egg-base /tmp/pip-pip-egg-info-ksb3c8ih
         cwd: /tmp/pip-install-ad2qu_uy/mysqlclient_df40fd48e0dc469e8a102567b171b787/
    Complete output (15 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 "<string>", line 1, in <module>
      File "/tmp/pip-install-ad2qu_uy/mysqlclient_df40fd48e0dc469e8a102567b171b787/setup.py", line 15, in <module>
        metadata, options = get_config()
      File "/tmp/pip-install-ad2qu_uy/mysqlclient_df40fd48e0dc469e8a102567b171b787/setup_posix.py", line 70, in get_config
        libs = mysql_config("libs")
      File "/tmp/pip-install-ad2qu_uy/mysqlclient_df40fd48e0dc469e8a102567b171b787/setup_posix.py", line 31, in mysql_config
        raise OSError("{} not found".format(_mysql_config_path))
    OSError: mysql_config not found
    mysql_config --version
    mariadb_config --version
    mysql_config --libs
    ----------------------------------------
WARNING: Discarding https://pypi.tuna.tsinghua.edu.cn/packages/3c/df/59cd2fa5e48d0804d213bdcb1acb4d08c403b61c7ff7ed4dd4a6a2deb3f7/mysqlclient-2.0.3.tar.gz#sha256=f6ebea7c008f155baeefe16c56cd3ee6239f7a5a9ae42396c2f1860f08a7c432 (from https://pypi.tuna.tsinghua.edu.cn/simple/mysqlclient/) (requires-python:>=3.5). Command errored out with exit status 1: python setup.py egg_info Check the logs for full command output.
ERROR: Could not find a version that satisfies the requirement mysqlclient==2.0.3 (from versions: 1.3.0, 1.3.1, 1.3.2, 1.3.3, 1.3.4, 1.3.5, 1.3.6, 1.3.7, 1.3.8, 1.3.9, 1.3.10, 1.3.11rc1, 1.3.11, 1.3.12, 1.3.13, 1.3.14, 1.4.0rc1, 1.4.0rc2, 1.4.0rc3, 1.4.0, 1.4.1, 1.4.2, 1.4.2.post1, 1.4.3, 1.4.4, 1.4.5, 1.4.6, 2.0.0, 2.0.1, 2.0.2, 2.0.3)
ERROR: No matching distribution found for mysqlclient==2.0.3

解决方法:

sudo apt install libmysqlclient-dev python3-dev
如果你使用的 python 解释器的版本是和系统自带的 python 版本一致 ,那就没有问题。

如果你用的不是系统自带的 python 解释器(版本一致就没有问题),继续往下看

报错二:

   ERROR: Command errored out with exit status 1:
     command: /home/bot/.local/share/virtualenvs/django-twitter-IaucORjf/bin/python -u -c 'import io, os, sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-rq7aoqrc/mysqlclient_40fa5be227e344b0a851378f6a96cc9a/setup.py'"'"'; __file__='"'"'/tmp/pip-install-rq7aoqrc/mysqlclient_40fa5be227e344b0a851378f6a96cc9a/setup.py'"'"';f = getattr(tokenize, '"'"'open'"'"', open)(__file__) if os.path.exists(__file__) else io.StringIO('"'"'from setuptools import setup; setup()'"'"');code = f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record /tmp/pip-record-n8yzv7f4/install-record.txt --single-version-externally-managed --compile --install-headers /home/bot/.local/share/virtualenvs/django-twitter-IaucORjf/include/site/python3.9/mysqlclient
         cwd: /tmp/pip-install-rq7aoqrc/mysqlclient_40fa5be227e344b0a851378f6a96cc9a/
    Complete output (46 lines):
    mysql_config --version
    ['8.0.26']
    mysql_config --libs
    ['-L/usr/lib/x86_64-linux-gnu', '-lmysqlclient', '-lpthread', '-ldl', '-lz', '-lssl', '-lcrypto', '-lresolv', '-lm', '-lrt']
    mysql_config --cflags
    ['-I/usr/include/mysql']
    ext_options:
      library_dirs: ['/usr/lib/x86_64-linux-gnu']
      libraries: ['mysqlclient', 'pthread', 'dl', 'resolv', 'm', 'rt']
      extra_compile_args: ['-std=c99']
      extra_link_args: []
      include_dirs: ['/usr/include/mysql']
      extra_objects: []
      define_macros: [('version_info', "(2,0,3,'final',0)"), ('__version__', '2.0.3')]
    running install
    running build
    running build_py
    creating build
    creating build/lib.linux-x86_64-3.9
    creating build/lib.linux-x86_64-3.9/MySQLdb
    copying MySQLdb/__init__.py -> build/lib.linux-x86_64-3.9/MySQLdb
    copying MySQLdb/_exceptions.py -> build/lib.linux-x86_64-3.9/MySQLdb
    copying MySQLdb/connections.py -> build/lib.linux-x86_64-3.9/MySQLdb
    copying MySQLdb/converters.py -> build/lib.linux-x86_64-3.9/MySQLdb
    copying MySQLdb/cursors.py -> build/lib.linux-x86_64-3.9/MySQLdb
    copying MySQLdb/release.py -> build/lib.linux-x86_64-3.9/MySQLdb
    copying MySQLdb/times.py -> build/lib.linux-x86_64-3.9/MySQLdb
    creating build/lib.linux-x86_64-3.9/MySQLdb/constants
    copying MySQLdb/constants/__init__.py -> build/lib.linux-x86_64-3.9/MySQLdb/constants
    copying MySQLdb/constants/CLIENT.py -> build/lib.linux-x86_64-3.9/MySQLdb/constants
    copying MySQLdb/constants/CR.py -> build/lib.linux-x86_64-3.9/MySQLdb/constants
    copying MySQLdb/constants/ER.py -> build/lib.linux-x86_64-3.9/MySQLdb/constants
    copying MySQLdb/constants/FIELD_TYPE.py -> build/lib.linux-x86_64-3.9/MySQLdb/constants
    copying MySQLdb/constants/FLAG.py -> build/lib.linux-x86_64-3.9/MySQLdb/constants
    warning: build_py: byte-compiling is disabled, skipping.
    
    running build_ext
    building 'MySQLdb._mysql' extension
    creating build/temp.linux-x86_64-3.9
    creating build/temp.linux-x86_64-3.9/MySQLdb
    x86_64-linux-gnu-gcc -pthread -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O2 -Wall -g -fstack-protector-strong -Wformat -Werror=format-security -g -fwrapv -O2 -g -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -fPIC -Dversion_info=(2,0,3,'final',0) -D__version__=2.0.3 -I/usr/include/mysql -I/home/bot/.local/share/virtualenvs/django-twitter-IaucORjf/include -I/usr/include/python3.9 -c MySQLdb/_mysql.c -o build/temp.linux-x86_64-3.9/MySQLdb/_mysql.o -std=c99
    MySQLdb/_mysql.c:46:10: fatal error: Python.h: 没有那个文件或目录
       46 | #include "Python.h"
          |          ^~~~~~~~~~
    compilation terminated.
    error: command '/usr/bin/x86_64-linux-gnu-gcc' failed with exit code 1
    ----------------------------------------
ERROR: Command errored out with exit status 1: /home/bot/.local/share/virtualenvs/django-twitter-IaucORjf/bin/python -u -c 'import io, os, sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-rq7aoqrc/mysqlclient_40fa5be227e344b0a851378f6a96cc9a/setup.py'"'"'; __file__='"'"'/tmp/pip-install-rq7aoqrc/mysqlclient_40fa5be227e344b0a851378f6a96cc9a/setup.py'"'"';f = getattr(tokenize, '"'"'open'"'"', open)(__file__) if os.path.exists(__file__) else io.StringIO('"'"'from setuptools import setup; setup()'"'"');code = f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record /tmp/pip-record-n8yzv7f4/install-record.txt --single-version-externally-managed --compile --install-headers /home/bot/.local/share/virtualenvs/django-twitter-IaucORjf/include/site/python3.9/mysqlclient Check the logs for full command output.

因为我用了 3.9 ,之前的命令是装的 3.8 对应的dev,所以会找不到

使用如下命令解决问题

sudo apt install  python3.9-dev

universe_king
3.4k 声望678 粉丝