![Serverless实践系列(二):为Python云函数打包依赖](https://static001.infoq.cn/resource/image/c4/88/c49145e0248c96e2c0ff7f941f001a88.jpg)
在使用无服务器云函数 SCF 时通常会遇到导入第三方库的问题,很多开发者比较头疼是:应该如何打包进去?这里,推荐几个不错的方法。
方法 1: 官方方案
官方方案地址:
https://cloud.tencent.com/document/product/583/9702
在这个方案中,基本上有以下几个步骤:
获得依赖列表
安装依赖到目录
生成 zip
测试
以安装 Pillow 为例: 安装 pipreqs,可以使用 pip install pipreqs,这个是一个可以获取本文项目中依赖的工具
![](https://static001.infoq.cn/resource/image/a4/c6/a4c658511a6de2852ff63edcf84062c6.jpg)
建立文件夹,并且建立测试文件:
![](https://static001.infoq.cn/resource/image/7a/df/7a91c3c880ae3383f84faef623df2ddf.jpg)
文件内容:
获得需要依赖,生成文件:
![](https://static001.infoq.cn/resource/image/a0/cb/a07a089cd11f7225d2474f0a8aa626cb.jpg)
文件内容:
![](https://static001.infoq.cn/resource/image/67/34/67eedcdb831c564c73253d25c75eb334.jpg)
安装 requirements:
![](https://static001.infoq.cn/resource/image/ac/81/ac3fda763ffb324123fc0c01cab18f81.jpg)
查看结果与本地测试:
![](https://static001.infoq.cn/resource/image/a8/62/a8866e00ce21770c20fe1513ed35ef62.jpg)
压缩文档:
![](https://static001.infoq.cn/resource/image/c2/f0/c2429993f1847d948d0d7fabbc7d3af0.jpg)
云函数中测试:
![](https://static001.infoq.cn/resource/image/04/60/049e4947abee394f41dc3919219ed960.jpg)
提示错误:
![](https://static001.infoq.cn/resource/image/a8/4c/a8095818cd5f9897bf78c65cc789074c.jpg)
按照云函数要求写 main_handler:
![](https://static001.infoq.cn/resource/image/53/90/53f7ed3388a171bcf041451a4cf17a90.jpg)
至此,通过官方提供的方法安装完成。
方法 2: Pycharm 快速安装
首先,新建项目 pillowtest,这里要选择 virtualenv
![](https://static001.infoq.cn/resource/image/0a/98/0a3698cedf6d40fd332b672e58131798.jpg)
然后,建立文件index.py:
![](https://static001.infoq.cn/resource/image/0e/48/0e3d3837261eb5db5c6449c140fde648.jpg)
安装 Pillow:
![](https://static001.infoq.cn/resource/image/90/7d/90a6f8e179f0f06e7ddd1ea8b7c9237d.jpg)
![](https://static001.infoq.cn/resource/image/73/19/738ecdfb47af179d0b8d7b1b8f3e1519.jpg)
安装完成之后,可以看到:
![](https://static001.infoq.cn/resource/image/a5/db/a5e9902a7a6efa5d18f8847c52f521db.jpg)
此时我们将安装过来的文档移动到 pillowtest 的目录下,然后将非 venv 目录进行压缩:
![](https://static001.infoq.cn/resource/image/43/85/4311a178b1429c74ec1d0ce4ca264c85.jpg)
![](https://static001.infoq.cn/resource/image/57/09/57bfec50cf4a540cf087c5752d4fa109.jpg)
此时,我们将结果上传到云函数,并适当修改index.py:
![](https://static001.infoq.cn/resource/image/5a/00/5afe564431ed5c2cfe596724848d7e00.jpg)
![](https://static001.infoq.cn/resource/image/4c/60/4cc27f08cc3bee8b56b4ab01fba5bc60.jpg)
方法 3: 自行复制法
这种方法是指,我们用了某个包,可以自己下载源码并且将其复制到目录下,具体方法可以参考上一篇实战:
《Serverless实践系列(一):如何通过SCF与自然语言处理为网站赋能》
作者介绍:
刘宇,腾讯云 Serverless 团队后台研发工程师。毕业于浙江大学,先后参与腾讯云云函数产品研发、自动扩缩容、CLI 等模块建设以及社区相关工作。本文转载自微信公众号 ServerlessCloudNative(ID:ServerlessGo)
相关文章:
《Serverless 实践系列(一):如何通过 SCF 与自然语言处理为网站赋能》
《Serverless 实践系列(三):突破传统 OJ 瓶颈,“判题姬”接入云函数》
《Serverless 实践系列(四):网站监控脚本的实现》
公众号推荐:
AIGC 技术正以惊人的速度重塑着创新的边界,InfoQ 首期《大模型领航者AIGC实践案例集锦》电子书,深度对话 30 位国内顶尖大模型专家,洞悉大模型技术前沿与未来趋势,精选 10 余个行业一线实践案例,全面展示大模型在多个垂直行业的应用成果,同时,揭秘全球热门大模型效果,为创业者、开发者提供决策支持和选型参考。关注「AI前线」,回复「领航者」免费获取电子书。
![](https://static001.geekbang.org/resource/image/b4/f4/b4b2dc5e41bda1a344a3179afd0049f4.png)
评论