Fail to import local modules
paoloantinori opened this issue · 6 comments
I'm submitting a...
[ ] Regression (a behavior that used to work and stopped working in a new release)
[x] Bug report
[ ] Performance issue
[ ] Feature request
[ ] Documentation issue or request
[ ] Other... Please describe:
I cannot find any filesystem structure that would allow me to import a local module, not even using one of the official examples like https://github.com/alexa/skill-sample-python-city-guide/tree/master/lambda/py
Expected Behavior
Correctly load modules and build locally
Current Behavior
Fails to build.
Full logs here
Traceback (most recent call last): File "/data/repo/github/personal/alexa_pumrus/pumrus/venv/lib/python3.9/site-packages/ask_sdk_local_debug/config/skill_invoker_config.py", line 78, in __initialize_skill_invoker self.spec.loader.exec_module(skill_invoker) File "", line 790, in exec_module File "", line 228, in _call_with_frames_removed File "/data/repo/github/personal/test_modules/paolo/lambda/lambda_function.py", line 24, in from alexa import data, util ModuleNotFoundError: No module named 'alexa'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/data/repo/github/personal/alexa_pumrus/pumrus/venv/lib/python3.9/site-packages/ask_sdk_local_debug/config/skill_invoker_config.py", line 87, in __get_skill_builder_func
return getattr(self.__initialize_skill_invoker(), self.skill_handler)
File "/data/repo/github/personal/alexa_pumrus/pumrus/venv/lib/python3.9/site-packages/ask_sdk_local_debug/config/skill_invoker_config.py", line 80, in __initialize_skill_invoker
raise LocalDebugSdkException(
ask_sdk_local_debug.exception.LocalDebugSdkException: Failed to load the module from /data/repo/github/personal/test_modules/paolo/lambda/lambda_function.py : No module named 'alexa'During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/lib64/python3.9/runpy.py", line 197, in _run_module_as_main
return _run_code(code, main_globals, None,
File "/usr/lib64/python3.9/runpy.py", line 87, in _run_code
exec(code, run_globals)
File "/home/pantinor/.vscode/extensions/ms-python.python-2020.12.424452561/pythonFiles/lib/python/debugpy/main.py", line 45, in
cli.main()
File "/home/pantinor/.vscode/extensions/ms-python.python-2020.12.424452561/pythonFiles/lib/python/debugpy/../debugpy/server/cli.py", line 444, in main
run()
File "/home/pantinor/.vscode/extensions/ms-python.python-2020.12.424452561/pythonFiles/lib/python/debugpy/../debugpy/server/cli.py", line 285, in run_file
runpy.run_path(target_as_str, run_name=compat.force_str("main"))
File "/usr/lib64/python3.9/runpy.py", line 268, in run_path
return _run_module_code(code, init_globals, run_name,
File "/usr/lib64/python3.9/runpy.py", line 97, in _run_module_code
_run_code(code, mod_globals, init_globals,
File "/usr/lib64/python3.9/runpy.py", line 87, in _run_code
exec(code, run_globals)
File "/data/repo/github/personal/alexa_pumrus/pumrus/venv/lib/python3.9/site-packages/ask_sdk_local_debug/local_debugger_invoker.py", line 57, in
local_debug_invoker = LocalDebuggerInvoker(sys.argv[1:])
File "/data/repo/github/personal/alexa_pumrus/pumrus/venv/lib/python3.9/site-packages/ask_sdk_local_debug/local_debugger_invoker.py", line 49, in init
self.web_socket_client = create_web_socket_client(parsed_args)
File "/data/repo/github/personal/alexa_pumrus/pumrus/venv/lib/python3.9/site-packages/ask_sdk_local_debug/util/websocket_util.py", line 60, in create_web_socket_client
skill_invoker_config = create_skill_invoker_config(parsed_args)
File "/data/repo/github/personal/alexa_pumrus/pumrus/venv/lib/python3.9/site-packages/ask_sdk_local_debug/util/websocket_util.py", line 126, in create_skill_invoker_config
return SkillInvokerConfiguration(
File "/data/repo/github/personal/alexa_pumrus/pumrus/venv/lib/python3.9/site-packages/ask_sdk_local_debug/config/skill_invoker_config.py", line 48, in init
self.skill_builder_func = self.__get_skill_builder_func()
File "/data/repo/github/personal/alexa_pumrus/pumrus/venv/lib/python3.9/site-packages/ask_sdk_local_debug/config/skill_invoker_config.py", line 89, in __get_skill_builder_func
raise LocalDebugSdkException(
ask_sdk_local_debug.exception.LocalDebugSdkException: Handler function does not exist. Make sure that the skill file path:/data/repo/github/personal/test_modules/paolo/lambda/lambda_function.py and the handler name:lambda_handler are correct. Exception:Failed to load the module from /data/repo/github/personal/test_modules/paolo/lambda/lambda_function.py : No module named 'alexa'
Possible Solution
No clue
Steps to Reproduce (for bugs)
Just that line with that project structure
https://github.com/alexa/skill-sample-python-city-guide/blob/master/lambda/py/lambda_function.py#L24
Context
It prevents from build the function locally
Your Environment
- ASK SDK for Python used:
1.1.0
- Operating System and version: `Fedora release 33 (Thirty Three)
Python version info
- Python version used for development:
Python 3.9.1
Hey @paoloantinori , thanks for raising this issue. This is specifically not an issue with the ASK SDK itself. Instead it seems to be with how the skill code is structured in https://github.com/alexa/skill-sample-python-city-guide/tree/master/lambda/py folder. The sample was written keeping in mind that it will be deployed on AWS Lambda, and hence was structured using python package alexa
, that can be loaded during AWS Lambda run since I guess the whole directory is added to the PythonPath variable and hence any module/package underneath it is included.
However, when ask-sdk-local-debug
is run, the __main__
method is passed the required variables, which will directly load the module onto the path and invoke it, and hence will not be able to figure out other packages present. This seems to be a bug on our ask-sdk-local-debug
package, and we will try and fix this.
In the mean time, to manually load the local directory onto the python path, you can try adding the following lines in the code, before the line https://github.com/alexa/skill-sample-python-city-guide/blob/master/lambda/py/lambda_function.py#L24
import sys
import os
sys.path.append(os.path.dirname(__file__))
I could get the local debugger running successfully from our ask toolkit extension, with the above mentioned hack. Can you please try and let us know if this unblocks you for now?
Hi, yes this workaround works perfectly both locally and when deployed!
Thank you!
Hi @nikhilym , since you are checking this:
there is acollateral issue with apl
templates:
if they are in my lambda/
folder, to load them correctly in local development I need to provide paths that start with lambda/...
while instead, when I deploy on Amazon servers, I need to remove the lambda/
prefix.
Is there a strategy to have it working on both environments without messing with paths?
Hey @paoloantinori , we did discuss about this issue during the bug fix and here are the options, depending on how you are using the ask-sdk-local-debug
package :
- If you are using it through our
ask-toolkit
vscode extension and the default launch configuration, then vscode debugger provides an option to configure the current working directorycwd
where the debugger is launched. By default, it is the workspace folder which is the top level folder containing all your skill resources. This is the reason why you needed the additionallambda/
path for loading the resources, when using local debugger. To resolve this, set thecwd
to the skill's lambda folder in yourlaunch.json
. Here is thelaunch.json
I tried in my skill :
{
"version": "0.2.0",
"configurations": [
{
"name": "Debug Alexa Skill (Python)",
"type": "python",
"request": "launch",
"program": "${command:ask.debugAdapterPath}",
"args": [
"--accessToken",
"${command:ask.accessToken}",
"--skillId",
"${command:ask.skillIdFromWorkspace}",
"--skillHandler",
"lambda_handler",
"--skillFilePath",
"${workspaceFolder}/lambda/lambda_function.py",
"--region",
"NA"
],
"console": "internalConsole",
"cwd": "${workspaceFolder}/lambda"
}
]
}
- If you are using it through our
ask cli's
ask run
command, then unfortunately you are stuck here. But the good thing is we are already fixing it as I speak and it will be handled in the future releases of ASK CLI.
Sorry for the inconvenience and thanks for reporting the issue. Please let us know if this helps. Thanks.
Thank you @nikhilym, great support!
Glad you got it working @paoloantinori . Please let us know if you face any other issues or have feature requests/feedback about our tools. Have a good one!!