fun local 不支持 python redis 库
- Dominant language
- JavaScript
- Stars
- 931
- Forks
- 128
- PR merge metrics
- No merged PRs in 30d
Description
## 重现方法
template.yml 文件
```
ROSTemplateFormatVersion: '2015-09-01'
Transform: 'Aliyun::Serverless-2018-04-03'
Resources:
redis:
Type: 'Aliyun::Serverless::Service'
Properties:
Description: This is FC service for redis
python:
Type: 'Aliyun::Serverless::Function'
Properties:
Handler: 'index.handler'
Runtime: python3
Timeout: 10
MemorySize: 128
CodeUri: ./
```
requirements.txt
```
redis
```
index.py
```
import os,sys
print(sys.path)
import redis
def handler(event, context):
return 'hello world'
```
执行命令
```
fun install
# 或者
fun build
```
fun local invoke 报错,部署到远端应该一样会报错的
```
FC Invoke Start RequestId: 1ebcb234-1b1e-48b7-b8ad-d1f55cc7e0c0
['/code', '/var/fc/runtime/python3/src', '/usr/local/lib/python36.zip', '/usr/local/lib/python3.6', '/usr/local/lib/python3.6/lib-dynload', '/usr/local/lib/python3.6/site-packages']
2020-02-11T10:26:25.781Z 1ebcb234-1b1e-48b7-b8ad-d1f55cc7e0c0 [ERROR] {'errorMessage': "Unable to import module 'index'", 'errorType': 'ImportError', 'stackTrace': ["ModuleNotFoundError: No module named 'redis'"]}
FC Invoke End RequestId: 1ebcb234-1b1e-48b7-b8ad-d1f55cc7e0c0
{
"errorMessage": "Unable to import module 'index'",
"errorType": "ImportError",
"stackTrace": [
"ModuleNotFoundError: No module named 'redis'"
]
}
```
## 原因分析
redis 安装目录是 .fun/python/lib/python/site-packages/redis

这个目录没有被 PYTHONUSERBASE=/code/.fun/python 所识别出来,如果把后一个 python改成 python3.6 就可以正常运行
改成 .fun/python/lib/python3.6/site-packages/redis 是 OK 的。
## Workaround
追加环境变量 PYTHONPATH: /code/.fun/python/lib/python/site-packages
Contributor guide
No contributing guide indexed for this repository
Research direction
Start with the template.yml, requirements.txt, and index.py reproduction, then run `fun install` or `fun build` followed by `fun local invoke`. Trace how the installed Redis package is placed under `.fun/python/lib/python/site-packages` and how the local Python runtime sets its import path. Done means the sample imports `redis` successfully without the PYTHONPATH workaround.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python, redis
- Domain
- backend, cloud
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 48/100