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

使用write_pandas函数遇snowflake-connector MissingDependencyError问题

解决Snowflake write_pandas报错AttributeError: MissingDependencyError的问题

问题场景

使用geopandas读取Shapefile后,通过snowflake-connector的write_pandas写入Snowflake时触发以下错误:

AttributeError: module 'snowflake.connector.errors' has no attribute 'MissingDependencyError'

用户代码如下:

import geopandas
import pandas as pd
import json
import snowflake.connector
from snowflake.connector.pandas_tools import write_pandas

snowflake_config = {
    'account': account_url,
    'warehouse':'wh_name',
    'database':'db_name',
    'schema':'schmea_name',
    'role': 'role_name'
}
shapefile=geopandas.read_file("path of shap file in zip format")
df = pd.DataFrame(shapefile)
conn = snowflake.connector.connect(
    user = 'username',
    authenticator ='externalbrowser',
    **snowflake_config
)
create_table_query = f'''
        CREATE TABLE IF NOT EXISTS db_name.schema_name.TRIALEXAMPLE (
            globalid STRING,
            CreationDa date,
            EditDate date,
            your_name STRING,
            are_you_a_ STRING,
            which_indu STRING,
            COORDINATES varchar
            
        )
    '''

cursor = conn.cursor()
cursor.execute(create_table_query)
success, nchunks, nrows, _ = write_pandas(conn, df, 'TRIALEXAMPLE')

错误原因

该错误源于snowflake-connector-python 3.2.0版本内部的异常类定义问题:MissingDependencyError在snowflake.connector.errors模块中不存在,但内部代码尝试调用该异常。同时,GeoDataFrame转换后的DataFrame带有非标准索引,触发了依赖检查分支,进而暴露了这个内部bug。

解决方案

方案1:重置DataFrame索引(快速绕过)

在调用write_pandas前,将DataFrame索引重置为标准RangeIndex,避免触发错误分支:

# 新增索引重置代码
df = df.reset_index(drop=True)
success, nchunks, nrows, _ = write_pandas(conn, df, 'TRIALEXAMPLE')

方案2:重新安装完整依赖

强制重新安装包含所有可选依赖的snowflake-connector-python:

pip install snowflake-connector-python[all] --force-reinstall

方案3:降级到稳定版本

若3.2.0版本存在bug,降级至之前的稳定版本(如3.1.0):

pip install snowflake-connector-python==3.1.0

方案4:确认pandas版本兼容

确保pandas版本与snowflake-connector-python兼容,升级pandas到最新稳定版:

pip install pandas --upgrade

验证步骤

  1. 执行上述方案操作后重新运行代码
  2. 检查Snowflake目标表是否成功写入数据
  3. 确认控制台不再出现AttributeError相关报错

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.09 21:35:29