如何在 Linux 上的 Python 3.9 上安装 flask-mysqldb?

How can I install flask-mysqldb on Python 3.9 on Linux?

我尝试在 Pop!_OS 20.04 上安装软件包 flask-mysqldb。下面是我 运行 的命令和我得到的错误:

[user]@[host]:~$ python3.9 -m pip install flask-mysqldb
Defaulting to user installation because normal site-packages is not writeable
Collecting flask-mysqldb
  Using cached Flask-MySQLdb-0.2.0.tar.gz (2.1 kB)
  Preparing metadata (setup.py) ... done
Collecting Flask>=0.10
  Using cached Flask-2.0.2-py3-none-any.whl (95 kB)
Collecting mysqlclient
  Using cached mysqlclient-2.1.0.tar.gz (87 kB)
  Preparing metadata (setup.py) ... error
  error: subprocess-exited-with-error
  
  × python setup.py egg_info did not run successfully.
  │ exit code: 1
  ╰─> [16 lines of output]
      /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 2, in <module>
        File "<pip-setuptools-caller>", line 34, in <module>
        File "/tmp/pip-install-f1mmzpvw/mysqlclient_315e946d2fe0466f8d910fe1dc97be53/setup.py", line 15, in <module>
          metadata, options = get_config()
        File "/tmp/pip-install-f1mmzpvw/mysqlclient_315e946d2fe0466f8d910fe1dc97be53/setup_posix.py", line 70, in get_config
          libs = mysql_config("libs")
        File "/tmp/pip-install-f1mmzpvw/mysqlclient_315e946d2fe0466f8d910fe1dc97be53/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
      [end of output]
  
  note: This error originates from a subprocess, and is likely not a problem with pip.
error: metadata-generation-failed

× Encountered error while generating package metadata.
╰─> See above for output.

note: This is an issue with the package mentioned above, not pip.
hint: See above for details.

P.S。我的系统自带 Python 3.8.10,但在论坛上看到 flask-mysqldb 与该版本不兼容后,我手动安装了 Python 3.9.

编辑:我在 运行 sudo apt install libmariadb-dev-compat 之后再次尝试,下面是我得到的错误:

musti@pop-os:~$ pip install flask-mysqldb
Defaulting to user installation because normal site-packages is not writeable
Collecting flask-mysqldb
  Using cached Flask-MySQLdb-0.2.0.tar.gz (2.1 kB)
  Preparing metadata (setup.py) ... done
Collecting Flask>=0.10
  Using cached Flask-2.0.2-py3-none-any.whl (95 kB)
Collecting mysqlclient
  Using cached mysqlclient-2.1.0.tar.gz (87 kB)
  Preparing metadata (setup.py) ... done
Collecting click>=7.1.2
  Downloading click-8.0.3-py3-none-any.whl (97 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 97.5/97.5 KB 604.1 kB/s eta 0:00:00
Collecting Jinja2>=3.0
  Downloading Jinja2-3.0.3-py3-none-any.whl (133 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 133.6/133.6 KB 965.6 kB/s eta 0:00:00
Collecting itsdangerous>=2.0
  Using cached itsdangerous-2.0.1-py3-none-any.whl (18 kB)
Collecting Werkzeug>=2.0
  Downloading Werkzeug-2.0.2-py3-none-any.whl (288 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 288.9/288.9 KB 791.8 kB/s eta 0:00:00
Collecting MarkupSafe>=2.0
  Downloading MarkupSafe-2.0.1-cp39-cp39-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.whl (30 kB)
Building wheels for collected packages: flask-mysqldb, mysqlclient
  Building wheel for flask-mysqldb (setup.py) ... done
  Created wheel for flask-mysqldb: filename=Flask_MySQLdb-0.2.0-py3-none-any.whl size=2666 sha256=953b45d216c21c9f1abe115e74c1f1181126194f1d121bcf274c0b67c47938ea
  Stored in directory: /home/musti/.cache/pip/wheels/41/ab/e5/ac1bfe8e719b0c95880c23643ce001363e8240f615f260755e
  Building wheel for mysqlclient (setup.py) ... error
  error: subprocess-exited-with-error
  
  × python setup.py bdist_wheel did not run successfully.
  │ exit code: 1
  ╰─> [44 lines of output]
      mysql_config --version
      ['10.3.32']
      mysql_config --libs
      ['-L/usr/lib/x86_64-linux-gnu/', '-lmariadb']
      mysql_config --cflags
      ['-I/usr/include/mariadb', '-I/usr/include/mariadb/mysql']
      ext_options:
        library_dirs: ['/usr/lib/x86_64-linux-gnu/']
        libraries: ['mariadb']
        extra_compile_args: ['-std=c99']
        extra_link_args: []
        include_dirs: ['/usr/include/mariadb', '/usr/include/mariadb/mysql']
        extra_objects: []
        define_macros: [('version_info', "(2,1,0,'final',0)"), ('__version__', '2.1.0')]
      running bdist_wheel
      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
      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,1,0,'final',0) -D__version__=2.1.0 -I/usr/include/mariadb -I/usr/include/mariadb/mysql -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: No such file or directory
         46 | #include "Python.h"
            |          ^~~~~~~~~~
      compilation terminated.
      error: command '/usr/bin/x86_64-linux-gnu-gcc' failed with exit code 1
      [end of output]
  
  note: This error originates from a subprocess, and is likely not a problem with pip.
  ERROR: Failed building wheel for mysqlclient
  Running setup.py clean for mysqlclient
Successfully built flask-mysqldb
Failed to build mysqlclient
Installing collected packages: Werkzeug, mysqlclient, MarkupSafe, itsdangerous, click, Jinja2, Flask, flask-mysqldb
  Running setup.py install for mysqlclient ... error
  error: subprocess-exited-with-error
  
  × Running setup.py install for mysqlclient did not run successfully.
  │ exit code: 1
  ╰─> [44 lines of output]
      mysql_config --version
      ['10.3.32']
      mysql_config --libs
      ['-L/usr/lib/x86_64-linux-gnu/', '-lmariadb']
      mysql_config --cflags
      ['-I/usr/include/mariadb', '-I/usr/include/mariadb/mysql']
      ext_options:
        library_dirs: ['/usr/lib/x86_64-linux-gnu/']
        libraries: ['mariadb']
        extra_compile_args: ['-std=c99']
        extra_link_args: []
        include_dirs: ['/usr/include/mariadb', '/usr/include/mariadb/mysql']
        extra_objects: []
        define_macros: [('version_info', "(2,1,0,'final',0)"), ('__version__', '2.1.0')]
      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
      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,1,0,'final',0) -D__version__=2.1.0 -I/usr/include/mariadb -I/usr/include/mariadb/mysql -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: No such file or directory
         46 | #include "Python.h"
            |          ^~~~~~~~~~
      compilation terminated.
      error: command '/usr/bin/x86_64-linux-gnu-gcc' failed with exit code 1
      [end of output]
  
  note: This error originates from a subprocess, and is likely not a problem with pip.
error: legacy-install-failure

× Encountered error while trying to install package.
╰─> mysqlclient

note: This is an issue with the package mentioned above, not pip.
hint: See above for output from the failure.

似乎缺少 python-dev 包。尝试使用 apt install python-devapt install python3-dev

安装它