AWS Amplify Mock ModuleNotFoundError with 3rd Party Libraries

Viewed 14

I have an Amplify application configured and I am trying to test a AWS Lambda function locally using the command amplify mock function, an error occurs however stating that the module requests is not found as shown below.

> 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.

To install the requests package I used the following commands.

cd .\amplify\backend\function\MLFunction\ 
pipenv shell
pipenv install requests

The virtual environment I can see is under C:\Users\<USER>\.virtualenvs\MLFunction-flLToiwL\ and within the path C:\Users\<USER>\.virtualenvs\MLFunction-flLToiwL\Lib\site-packages the requests package is there.

The pipfile under .\amplify\backend\function\MLFunction\ also contains the following.

[[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"

When the function is deployed onto AWS it works fine, it is just local testing using amplify mock function which causes problems.

0 Answers
Related