You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python MySQL Connector无法正常使用求助(Ubuntu 18.04环境)

问题解决:Import mysql.connector触发SyntaxError: future feature annotations is not defined

这个问题不需要安装额外依赖库,核心原因是你安装的mysql-connector-python版本和Python 3.6.9不兼容。

问题根源

from __future__ import annotations是Python 3.7才引入的语法特性,而高版本的mysql-connector-python已经放弃了对Python 3.6的支持,代码中使用了该仅3.7+兼容的语法,导致在Python 3.6环境下导入时触发语法错误。

解决步骤

  1. 卸载当前不兼容的版本:
    如果是通过pip安装的:

    pip uninstall mysql-connector-python
    

    如果是通过apt安装的,先移除相关包:

    sudo apt remove mysql-connector-python-py3 mysql-connector-python-py3-dbgsym
    
  2. 安装适配Python 3.6的指定版本(比如8.0.27版本,完美兼容Python 3.6):

    pip install mysql-connector-python==8.0.27
    

完成后重新执行import mysql.connector即可正常运行。

内容的提问来源于stack exchange,提问作者Karel Adams

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.26 19:42:36