WK1*_*123 5 python amazon-web-services aws-lambda pipenv aws-amplify
我配置了 Amplify 应用程序,并尝试使用命令在本地测试 AWS Lambda 函数amplify mock function,但出现错误,指出requests未找到该模块,如下所示。
> amplify mock function
? Select the function to mock MLFunction
? Provide the path to the event JSON object relative to C:\Users\warre\OneDrive\Documents\College\Msc\FinalYearProject\AWS\Amplify_Tutorial\amplify-app\amplify\backend\function\MLFunction src/event.json
Ensuring latest function changes are built...
Starting execution...
Traceback (most recent call last):
File "C:\Users\warre\.amplify\lib\amplify-python-function-runtime-provider\shim\shim.py", line 44, in <module>
main(sys.argv[1:])
File "C:\Users\warre\.amplify\lib\amplify-python-function-runtime-provider\shim\shim.py", line 22, in main
spec.loader.exec_module(handlerModule)
File "<frozen importlib._bootstrap_external>", line 790, in exec_module
File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
File "C:\Users\warre\OneDrive\Documents\College\Msc\FinalYearProject\AWS\Amplify_Tutorial\amplify-app\amplify\backend\function\MLFunction\src\index.py", line 2, in <module>
import requests
ModuleNotFoundError: No module named 'requests'
MLFunction failed with the following error:
Error: Command failed with exit code 1: pipenv run python3 C:\Users\warre\.amplify\lib\amplify-python-function-runtime-provider\shim\shim.py C:\Users\warre\OneDrive\Documents\College\Msc\FinalYearProject\AWS\Amplify_Tutorial\amplify-app\amplify\backend\function\MLFunction\src\index.py handler
Traceback (most recent call last):
File "C:\Users\warre\.amplify\lib\amplify-python-function-runtime-provider\shim\shim.py", line 44, in <module>
main(sys.argv[1:])
File "C:\Users\warre\.amplify\lib\amplify-python-function-runtime-provider\shim\shim.py", line 22, in main
spec.loader.exec_module(handlerModule)
File "<frozen importlib._bootstrap_external>", line 790, in exec_module
File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
File "C:\Users\warre\OneDrive\Documents\College\Msc\FinalYearProject\AWS\Amplify_Tutorial\amplify-app\amplify\backend\function\MLFunction\src\index.py", line 2, in <module>
import requests
ModuleNotFoundError: No module named 'requests'
at makeError (C:\snapshot\repo\build\node_modules\execa\lib\error.js:60:11)
at handlePromise (C:\snapshot\repo\build\node_modules\execa\index.js:118:26)
at processTicksAndRejections (internal/process/task_queues.js:95:5)
at async pythonInvoke (C:\snapshot\repo\build\node_modules\amplify-python-function-runtime-provider\lib\util\invokeUtil.js:30:24)
at async timeConstrainedInvoker (C:\snapshot\repo\build\node_modules\amplify-util-mock\lib\func\index.js:134:12)
at async start (C:\snapshot\repo\build\node_modules\amplify-util-mock\lib\func\index.js:111:20)
at async Object.run (C:\snapshot\repo\build\node_modules\amplify-util-mock\lib\commands\mock\function.js:21:5)
at async Object.executeAmplifyCommand (C:\snapshot\repo\build\node_modules\amplify-util-mock\lib\amplify-plugin-index.js:47:3)
at async executePluginModuleCommand (C:\snapshot\repo\build\node_modules\@aws-amplify\cli-internal\lib\execution-manager.js:142:5)
at async executeCommand (C:\snapshot\repo\build\node_modules\@aws-amplify\cli-internal\lib\execution-manager.js:40:9)
at async Object.run (C:\snapshot\repo\build\node_modules\@aws-amplify\cli-internal\lib\index.js:165:13) {
shortMessage: 'Command failed with exit code 1: pipenv run python3 C:\\Users\\warre\\.amplify\\lib\\amplify-python-function-runtime-provider\\shim\\shim.py C:\\Users\\warre\\OneDrive\\Documents\\College\\Msc\\FinalYearProject\\AWS\\Amplify_Tutorial\\amplify-app\\amplify\\backend\\function\\MLFunction\\src\\index.py handler',
command: 'pipenv run python3 C:\\Users\\warre\\.amplify\\lib\\amplify-python-function-runtime-provider\\shim\\shim.py C:\\Users\\warre\\OneDrive\\Documents\\College\\Msc\\FinalYearProject\\AWS\\Amplify_Tutorial\\amplify-app\\amplify\\backend\\function\\MLFunction\\src\\index.py handler',
escapedCommand: 'pipenv run python3 "C:\\Users\\warre\\.amplify\\lib\\amplify-python-function-runtime-provider\\shim\\shim.py" "C:\\Users\\warre\\OneDrive\\Documents\\College\\Msc\\FinalYearProject\\AWS\\Amplify_Tutorial\\amplify-app\\amplify\\backend\\function\\MLFunction\\src\\index.py" handler',
exitCode: 1,
signal: undefined,
signalDescription: undefined,
stdout: '',
stderr: 'Traceback (most recent call last):\r\n' +
' File "C:\\Users\\warre\\.amplify\\lib\\amplify-python-function-runtime-provider\\shim\\shim.py", line 44, in <module>\r\n' +
' main(sys.argv[1:])\r\n' +
' File "C:\\Users\\warre\\.amplify\\lib\\amplify-python-function-runtime-provider\\shim\\shim.py", line 22, in main\r\n' +
' spec.loader.exec_module(handlerModule)\r\n' +
' File "<frozen importlib._bootstrap_external>", line 790, in exec_module\r\n' +
' File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed\r\n' +
' File "C:\\Users\\warre\\OneDrive\\Documents\\College\\Msc\\FinalYearProject\\AWS\\Amplify_Tutorial\\amplify-app\\amplify\\backend\\function\\MLFunction\\src\\index.py", line 2, in <module>\r\n' +
' import requests\r\n' +
"ModuleNotFoundError: No module named 'requests'",
failed: true,
timedOut: false,
isCanceled: false,
killed: false
}
Finished execution.
Run Code Online (Sandbox Code Playgroud)
为了安装该requests软件包,我使用了以下命令。
cd .\amplify\backend\function\MLFunction\
pipenv shell
pipenv install requests
Run Code Online (Sandbox Code Playgroud)
我可以看到的虚拟环境位于请求包所在C:\Users\<USER>\.virtualenvs\MLFunction-flLToiwL\路径的下方和内部。C:\Users\<USER>\.virtualenvs\MLFunction-flLToiwL\Lib\site-packages
下面的 pipfile.\amplify\backend\function\MLFunction\还包含以下内容。
[[source]]
name = "pypi"
url = "https://pypi.org/simple"
verify_ssl = true
[dev-packages]
[packages]
src = {editable = true, path = "./src"}
requests = "*"
[requires]
python_version = "3.9"
Run Code Online (Sandbox Code Playgroud)
当该功能部署到AWS上时,它工作正常,只是本地测试使用会amplify mock function导致问题。