python 3.6使用mysql-connector-python报错:SyntaxError: future feature annotations is not defined

python 3.6使用mysql-connector-python报错:

Traceback (most recent call last):

File "E:\newcode\bigdata\pysparkTest\etl\export_doris_schema.py", line 1, in <module>

import mysql.connector

File "D:\ProgramData\Anaconda3\lib\site-packages\mysql\connector\__init__.py", line 32, in <module>

from .connection_cext import CMySQLConnection

File "D:\ProgramData\Anaconda3\lib\site-packages\mysql\connector\connection_cext.py", line 40, in <module>

from .abstracts import MySQLConnectionAbstract

File "D:\ProgramData\Anaconda3\lib\site-packages\mysql\connector\abstracts.py", line 33

from __future__ import annotations

^

SyntaxError: future feature annotations is not defined

Builder

错误分析

这个错误是由于Python版本兼容性问题导致的。错误信息显示:

SyntaxError: future feature annotations is not defined

问题原因:

  1. from future import annotations 是Python 3.7+才支持的特性
  2. 您当前使用的mysql-connector-python版本可能需要Python 3.7或更高版本
  3. 您的Python环境版本可能低于3.7,或者mysql-connector-python版本过新

解决方案

方案1:升级Python版本(推荐)

如果可能的话,升级到Python 3.7或更高版本:

# 检查当前Python版本
python --version

方案2:降级mysql-connector-python版本

卸载当前版本并安装兼容的版本:

pip uninstall mysql-connector-python
pip install mysql-connector-python==8.0.33

方案3:使用替代库(最简单)

使用PyMySQL替代mysql-connector-python,它对Python版本要求更宽松:

下面的是一个使用pymysql导出doris表结构的示例

import pymysql
from pymysql import Error

def export_doris_schema(host, port, user, password, database, output_file):
    try:
        # 连接到 Doris 的 MySQL 接口
        connection = pymysql.connect(
            host=host,
            port=port,
            user=user,
            password=password,
            database=database,
            charset='utf8mb4'
        )

        print(f"✅ 成功连接到 Doris 数据库: {database}")

        cursor = connection.cursor()

        # 获取所有表名
        cursor.execute("SHOW TABLES")
        tables = cursor.fetchall()

        with open(output_file, 'w', encoding='utf-8') as f:
            for table in tables:
                table_name = table[0]
                print(f"📋 正在导出表结构: {table_name}")

                cursor.execute(f"SHOW CREATE TABLE `{table_name}`")
                create_stmt = cursor.fetchone()[1]

                f.write(f"-- 表名:{table_name}\n")
                f.write(create_stmt + ";\n\n")

        print(f"🎉 所有表结构已成功导出到:{output_file}")

    except Error as e:
        print(f"❌ 数据库连接或操作失败: {e}")
    except Exception as e:
        print(f"❌ 发生未知错误: {e}")

    finally:
        if 'connection' in locals():
            cursor.close()
            connection.close()
            print("🔒 数据库连接已关闭")

# 配置参数(替换为你的实际信息)
config = {
    "host": "10.0.42.25",        # Doris FE 地址
    "port": 9030,                     # Doris MySQL 协议端口
    "user": "root",
    "password": "",
    "database": "zgcn",      # 要导出的数据库名
    "output_file": "doris_schema.txt" # 导出文件名
}

# 执行导出
export_doris_schema(**config)

关注公众号“大模型全栈程序员”回复“小程序”获取1000个小程序打包源码。更多免费资源在http://www.gitweixin.com/?p=2627

发表评论

邮箱地址不会被公开。 必填项已用*标注