云函数添加外部依赖

以华为云函数为例,使用python添加依赖需要先知道项目的层级结构

Python项目结构:

|---Project
|---index.py

比如该项目需要用到requests依赖,但云函数默认环境没有该依赖,这时候就需要用到云函数的层,可以把他理解为本地依赖仓库,将压缩包上传到层,其结构为:

Python层结构:

|---layer.zip
|---python_modules
|---requests
|----...

下载依赖方式有(在liunx系统下执行,win环境下bin是exe后缀)

pip install -t ./python_modules requests

在云函数代码中,添加临时环境变量

import sys
sys.path.append('python_modules')

学习参考:https://developer.huawei.com/consumer/cn/doc/development/AppGallery-connect-Guides/agc-cloudfunction-layer-0000001354455701

posted @ 2022-12-14 02:25  Naihe\  阅读(173)  评论(0)    收藏  举报
// 音乐播放器