在从我部署的Python3.7 Lambda函数中调用函数时,我遇到了一些问题,从错误消息来看,这些函数与numpy
相关。这个问题指出,无法导入软件包,尽管尝试了我读到的许多解决方案,但我没有任何成功,我想知道下一步要测试什么或如何进一步调试。
我试过以下几种方法:
1.安装Docker,添加插件serverless-python-requirements,在yml中配置
1.在要捆绑和部署的应用程序目录中安装软件包,pip install -t src/vendor -r requirements.txt --no-cache-dir
1.卸载setuptools
和numpy
并按此顺序重新安装
运行sls invoke -f auth
后显示错误信息:
{
"errorMessage": "Unable to import module 'data': Unable to import required dependencies:\nnumpy: \n\nIMPORTANT: PLEASE READ THIS FOR ADVICE ON HOW TO SOLVE THIS ISSUE!\n\nImporting the numpy c-extensions failed.\n- Try uninstalling and reinstalling numpy.\n- If you have already done that, then:\n 1. Check that you expected to use Python3.7 from \"/var/lang/bin/python3.7\",\n and that you have no directories in your PATH or PYTHONPATH that can\n interfere with the Python and numpy version \"1.18.1\" you're trying to use.\n 2. If (1) looks fine, you can open a new issue at\n https://github.com/numpy/numpy/issues. Please include details on:\n - how you installed Python\n - how you installed numpy\n - your operating system\n - whether or not you have multiple versions of Python installed\n - if you built from source, your compiler versions and ideally a build log\n\n- If you're working with a numpy git repository, try `git clean -xdf`\n (removes all files not under version control) and rebuild numpy.\n\nNote: this error has many possible causes, so please don't comment on\nan existing issue about this - open a new one instead.\n\nOriginal error was: No module named 'numpy.core._multiarray_umath'\n",
"errorType": "Runtime.ImportModuleError"
}
提供的是我的设置:
OS: Mac OS X
Local Python: /Users/me/miniconda3/bin/python
Local Python version: Python 3.7.4
无服务器环境信息(Runtime = Python3.7):
Operating System: darwin
Node Version: 12.14.0
Framework Version: 1.67.3
Plugin Version: 3.6.6
SDK Version: 2.3.0
Components Version: 2.29.1
Docker:
Docker version 19.03.13, build 4484c46d9d
serverless.yml:
service: understand-your-sleep-api
plugins:
- serverless-python-requirements
- serverless-offline-python
custom:
pythonRequirements:
dockerizePip: true # non-linux
slim: true
useStaticCache: false
useDownloadCache: false
invalidateCaches: true
provider:
name: aws
runtime: python3.7
stage: ${opt:stage, 'dev'}
region: us-east-1
iamRoleStatements:
- Effect: Allow
Action:
- ssm:GetParameter
Resource: "arn:aws:ssm:us-east-1:*id*:parameter/*"
environment:
STAGE: ${self:provider.stage}
functions:
auth:
handler: data.auth
events:
- http:
path: /auth
method: get
cors: true
package:
exclude:
- env.yml
- node_modules/**
requirements.txt:
pandas==1.0.0
fitbit==0.3.1
oauthlib==3.1.0
requests==2.22.0
requests-oauthlib==1.3.0
data.py:
import sys
sys.path.insert(0, 'src/vendor') # Location of packages that follow
import json
from datetime import timedelta, datetime, date
import math
import pandas as pd
from requests_oauthlib import OAuth2Session
from urllib.parse import urlparse, parse_qs
import fitbit
import requests
import webbrowser
import base64
import os
import logging
def auth(event, context):
...
4条答案
按热度按时间rm5edbpk1#
vxf3dgd42#
我用
zipinfo .requirements.zip
检查了一下,发现macos的dynlib加载的是linux的so文件。我使用dockerizePip修复了这个问题:非Linux
请注意,如果工作目录中已存在. www.example.com,则不会触发此操作requirements.zip,因此在运行sls deploy之前,请执行
git clean -xfd
iszxjhcz3#
由于您使用的是
serverless-python-requirements
插件,它会为您打包库。换句话说,您不需要手动执行pip install -t src/vendor -r requirements.txt --no-cache-dir
所有这些操作。要解决这个问题,请删除
src/vendor
和data.py
中的以下两行:然后坐下来,让
serverless-python-requirements
为您做这项工作。aemubtdh4#
如果这对其他人有帮助:我在安装了
serverless-python-requirements
并部署了我的应用程序之后才在我的开发机器上安装了Docker,我认为这让我陷入了一种奇怪的状态。无论我做什么,我总是得到相同的numpy
错误。我的部署只有在我强制删除
serverless-python-requirements
缓存并重新部署后才开始工作。我运行serverless deploy --verbose
来找出该高速缓存的位置:然后将其删除:
然后下一个
serverless deploy
工作!