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

IBM Cloud Cloudant使用遇阻:ModuleNotFoundError问题求助

IBM Cloud Cloudant 模块缺失问题排查解决

问题现象

运行Cloudant连接代码时抛出ModuleNotFoundError: No module named 'cloudant'错误,导致代码无法初始化。

代码片段

from cloudant.client import Cloudant
from cloudant.error import CloudantException
def main(param_dict):
    try:
        client = Cloudant.iam(
            account_name=param_dict[USERNAME],
            api_key=param_dict[APIKEY],
            connect=True,
        )
        print(f"Databases: {client.all_dbs()}")
    except CloudantException as cloudant_exception:
        print("unable to connect")
        return {"error": cloudant_exception}
    except (requests.exceptions.RequestException, ConnectionResetError) as err:
        print("connection error")
        return {"error": err}

    return {"dbs": client.all_dbs()}

错误日志

[
  "2023-04-05T15:04:17.941831Z    stderr: Traceback (most recent call last):",
  "2023-04-05T15:04:17.942135Z    stderr: File \"/action/1/bin/exec__.py\", line 44, in <module>",
  "2023-04-05T15:04:17.944270Z    stderr: from main__ import main as main",
  "2023-04-05T15:04:17.944503Z    stderr: File \"/action/1/bin/main__.py\", line 1, in <module>",
  "2023-04-05T15:04:17.944939Z    stderr: from cloudant.client import Cloudant",
  "2023-04-05T15:04:17.945205Z    stderr: ModuleNotFoundError: No module named 'cloudant'",
  "2023-04-05T15:04:17.958247Z    stderr: Command exited abruptly during initialization.",
  "2023-04-05T15:04:17.958Z       stderr: The action did not initialize or run as expected. Log data might be missing."
]

解决方法

  • 声明依赖包:在项目根目录创建requirements.txt文件,添加cloudant==2.15.0(选择与IBM Cloud Python runtime兼容的版本,推荐2.x系列)
  • 部署时包含依赖:使用IBM Cloud CLI部署函数时,通过--requirements参数指定依赖文件,示例命令:
    ibmcloud fn action update your-action-name main.py --kind python:3.9 --requirements requirements.txt
    
  • 验证Runtime兼容性:确保使用的Python runtime版本(如python:3.9)支持cloudant包,cloudant 2.x要求Python 3.6及以上版本
  • 本地预测试:先在本地环境执行pip install cloudant安装依赖,运行代码确认逻辑正常后再部署到云端

内容的提问来源于stack exchange,提问作者Amine El Hammoumi

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.26 04:54:54