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

使用Python连接MySQL时出现AttributeError:httpcore无NetworkBackend属性

解决方法

方案1:降级httpcore到兼容版本

在命令行执行:

pip install httpcore==0.17.3

该版本保留了NetworkBackend属性,可与当前dnspython版本兼容。

方案2:升级dnspython到适配版本

若不想降级httpcore,可尝试升级dnspython至支持新版本httpcore的版本:

pip install --upgrade dnspython

方案3:替换为pymysql连接库

如果上述方法无效,可改用pymysql作为MySQL连接库,其依赖链更简单,不易出现此类冲突:

  1. 安装pymysql:
pip install pymysql
  1. 修改代码:
import pymysql
from datetime import datetime

def createUsers():
    with pymysql.connect(host="sql.freedb.tech",user="freedb_sami9644",password="########",database="freedb_messangerpro_db") as conn:
        cur = conn.cursor()
        # cur.execute("DROP TABLE IF EXISTS users")
        cur.execute("CREATE TABLE users(\
                    fullname TEXT,\
                    username TEXT PRIMARY KEY,\
                    birthdate DATE,\
                    gender TEXT,\
                    country TEXT,\
                    avatar TEXT,\
                    password TEXT,\
                    session TEXT,\
                    createdon DATE)")
createUsers()

错误原因

这个错误源于httpcore新版本(0.20+)移除了NetworkBackend类,但环境中旧版本的dnspython仍在尝试引用该属性,导致mysql.connector导入失败,本质是依赖库版本不兼容问题。

内容的提问来源于stack exchange,提问作者Samuel Getachew

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.13 19:49:52