无法在 Python 中导入 mysql.connector

Can't import mysql.connector in Python

正如标题所说,我无法在 Debian 9.13

上的 python 中导入 mysql.connector

这就是我所做的

# python
 Python 2.7.13 (default, Sep 26 2018, 18:42:22) [GCC 6.3.0 20170516] on
 linux2 Type "help", "copyright", "credits" or "license" for more
 information.
>>> import mysql.connector
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.7/dist-packages/mysql/connector/__init__.py", line 53, in <module>
    from .connection import MySQLConnection
  File "/usr/local/lib/python2.7/dist-packages/mysql/connector/connection.py", line 450
    f"This connection is using {tls_version} which is now "
                                                          ^
SyntaxError: invalid syntax

我尝试安装、卸载和重新安装 mysql-connectormysql-connector-pythonmysql-connector-python-rfPython2.7Python3

在有人将此问题标记为重复之前,我已经尝试了此帖子中的所有答案:

This connection is using {tls_version} which is now

Unresolved import mysql.connector PYTHON

import mysql.connector ModuleNotFoundError: No module named 'mysql.connector'; 'mysql' is not a package

ImportError: No module named 'MySQL'

This connection is using {tls_version} which is now, unable to deploy Flask application on Linux

如有任何帮助,我们将不胜感激。

正如@TimRoberts 和@Dhivakar Chelladurai 在评论中所说,解决方案是为 Python2 或 Python3.5 max 找到 MySQL-Connector-Python 的版本,因为这是最后一个Debian 9 的 Python3.

版本
  1. 下载:
wget https://dev.mysql.com/get/Downloads/Connector-Python/mysql-connector-python_2.1.8-1debian9_all.deb
  1. 安装:
sudo dpkg -i mysql-connector-python_2.1.8-1debian9_all.deb

就是这样!