Local debugging Lambda in VS Code leads to error "Unable to open socks.py: Unable to read file"
See original GitHub issueDescription:
Local debugging Lambda in VS Code leads to error “Unable to open socks.py: Unable to read file” path/to/my/project/app/urllib3/contrib/socks.py
this seems to only happen when I am importing other modules into the Lambda. A lambda with first-party modules like json work fine.
Steps to reproduce:
- Spin up new Hello World sam app via AWS Toolkit in VS Code, python 3.8, architecture x86_64
- Create launch.json via CodeLens “AWS: Add Debug Configuration” in Template.yml
- add
simple-salesforceto requirements.txt (but could be any 3rd party module) - add
from simple_salesforce import Salesforceto the top of app.py - press F5 to start local debugging
Observed result:
Added debug level to the AWS Toolkit extension in VS Code
2021-10-08` 12:46:05 [INFO]: Preparing to debug locally: Lambda "app.lambda_handler"
2021-10-08 12:46:05 [INFO]: Building SAM application...
2021-10-08 12:46:05 [INFO]: Running command: (not started) [/usr/local/bin/sam build --debug --build-dir /tmp/aws-toolkit-vscode/vsctkNElW2P/output --template /Users/sedenfield/lambda-python3.8/template.yaml --manifest /tmp/aws-toolkit-vscode/vsctkNElW2P/debug-requirements.txt]
2021-10-08 12:46:06 [INFO]: 2021-10-08 12:46:06,091 | Telemetry endpoint configured to be https://aws-serverless-tools-telemetry.us-west-2.amazonaws.com/metrics
2021-10-08 12:46:06 [INFO]: 2021-10-08 12:46:06,092 | Using config file: samconfig.toml, config environment: default
2021-10-08 12:46:06 [INFO]: 2021-10-08 12:46:06,092 | Expand command line arguments to:
2021-10-08 12:46:06,092 | --template_file=/Users/sedenfield/lambda-python3.8/template.yaml --build_dir=/tmp/aws-toolkit-vscode/vsctkNElW2P/output --manifest=/tmp/aws-toolkit-vscode/vsctkNElW2P/debug-requirements.txt --cache_dir=.aws-sam/cache
2021-10-08 12:46:06 [INFO]: 2021-10-08 12:46:06,136 | 'build' command is called
2021-10-08 12:46:06 [INFO]: 2021-10-08 12:46:06,140 | No Parameters detected in the template
2021-10-08 12:46:06 [INFO]: 2021-10-08 12:46:06,165 | 2 stacks found in the template
2021-10-08 12:46:06 [INFO]: 2021-10-08 12:46:06,165 | No Parameters detected in the template
2021-10-08 12:46:06 [INFO]: 2021-10-08 12:46:06,179 | 2 resources found in the stack
2021-10-08 12:46:06,179 | No Parameters detected in the template
2021-10-08 12:46:06 [INFO]: 2021-10-08 12:46:06,192 | Found Serverless function with name='HelloWorldFunction' and CodeUri='hello_world/'
2021-10-08 12:46:06 [INFO]: 2021-10-08 12:46:06,192 | --base-dir is not presented, adjusting uri hello_world/ relative to /Users/sedenfield/lambda-python3.8/template.yaml
2021-10-08 12:46:06,193 | No Parameters detected in the template
2021-10-08 12:46:06 [INFO]: 2021-10-08 12:46:06,206 | Instantiating build definitions
2021-10-08 12:46:06 [INFO]: 2021-10-08 12:46:06,207 | No previous build graph found, generating new one
2021-10-08 12:46:06,207 | Unique function build definition found, adding as new (Function Build Definition: BuildDefinition(python3.8, /Users/sedenfield/lambda-python3.8/hello_world, Zip, x86_64, , dcfaa98e-ecbf-4bd1-865f-66e873975fda, {}, {}, []), Function: Function(name='HelloWorldFunction', functionname='HelloWorldFunction', runtime='python3.8', memory=None, timeout=3, handler='app.lambda_handler', imageuri=None, packagetype='Zip', imageconfig=None, codeuri='/Users/sedenfield/lambda-python3.8/hello_world', environment=None, rolearn=None, layers=[], events={'HelloWorld': {'Type': 'Api', 'Properties': {'Path': '/hello', 'Method': 'get', 'RestApiId': 'ServerlessRestApi'}}}, metadata=None, inlinecode=None, codesign_config_arn=None, architectures=['x86_64'], stack_path=''))
2021-10-08 12:46:06 [INFO]: 2021-10-08 12:46:06,207 | Building codeuri: /Users/sedenfield/lambda-python3.8/hello_world runtime: python3.8 metadata: {} architecture: x86_64 functions: ['HelloWorldFunction']
2021-10-08 12:46:06 [INFO]: 2021-10-08 12:46:06,207 | Building to following folder /private/tmp/aws-toolkit-vscode/vsctkNElW2P/output/HelloWorldFunction
2021-10-08 12:46:06 [INFO]: 2021-10-08 12:46:06,208 | Loading workflow module 'aws_lambda_builders.workflows'
2021-10-08 12:46:06 [INFO]: 2021-10-08 12:46:06,211 | Registering workflow 'PythonPipBuilder' with capability 'Capability(language='python', dependency_manager='pip', application_framework=None)'
2021-10-08 12:46:06 [INFO]: 2021-10-08 12:46:06,212 | Registering workflow 'NodejsNpmBuilder' with capability 'Capability(language='nodejs', dependency_manager='npm', application_framework=None)'
2021-10-08 12:46:06 [INFO]: 2021-10-08 12:46:06,213 | Registering workflow 'RubyBundlerBuilder' with capability 'Capability(language='ruby', dependency_manager='bundler', application_framework=None)'
2021-10-08 12:46:06 [INFO]: 2021-10-08 12:46:06,214 | Registering workflow 'GoDepBuilder' with capability 'Capability(language='go', dependency_manager='dep', application_framework=None)'
2021-10-08 12:46:06 [INFO]: 2021-10-08 12:46:06,215 | Registering workflow 'GoModulesBuilder' with capability 'Capability(language='go', dependency_manager='modules', application_framework=None)'
2021-10-08 12:46:06 [INFO]: 2021-10-08 12:46:06,217 | Registering workflow 'JavaGradleWorkflow' with capability 'Capability(language='java', dependency_manager='gradle', application_framework=None)'
2021-10-08 12:46:06 [INFO]: 2021-10-08 12:46:06,218 | Registering workflow 'JavaMavenWorkflow' with capability 'Capability(language='java', dependency_manager='maven', application_framework=None)'
2021-10-08 12:46:06 [INFO]: 2021-10-08 12:46:06,219 | Registering workflow 'DotnetCliPackageBuilder' with capability 'Capability(language='dotnet', dependency_manager='cli-package', application_framework=None)'
2021-10-08 12:46:06 [INFO]: 2021-10-08 12:46:06,220 | Registering workflow 'CustomMakeBuilder' with capability 'Capability(language='provided', dependency_manager=None, application_framework=None)'
2021-10-08 12:46:06 [INFO]: 2021-10-08 12:46:06,220 | Found workflow 'PythonPipBuilder' to support capabilities 'Capability(language='python', dependency_manager='pip', application_framework=None)'
2021-10-08 12:46:06 [INFO]: 2021-10-08 12:46:06,408 | Running workflow 'PythonPipBuilder'
2021-10-08 12:46:06 [INFO]: 2021-10-08 12:46:06,408 | Running PythonPipBuilder:ResolveDependencies
2021-10-08 12:46:06 [INFO]: 2021-10-08 12:46:06,586 | calling pip download -r /tmp/aws-toolkit-vscode/vsctkNElW2P/debug-requirements.txt --dest /var/folders/2n/xs6sz2s951ggjzbrfsv1m2n80000gp/T/tmp6qb_agm3 --exists-action i
2021-10-08 12:46:08 [INFO]: 2021-10-08 12:46:08,654 | Full dependency closure: {charset-normalizer==2.0.6(wheel), debugpy==1.5.0(wheel), pycparser==2.20(wheel), idna==3.2(wheel), simple-salesforce==1.11.4(wheel), cffi==1.14.6(wheel), certifi==2021.10.8(wheel), authlib==0.15.4(wheel), cryptography==35.0.0(wheel), requests==2.26.0(wheel), urllib3==1.26.7(wheel)}
2021-10-08 12:46:08 [INFO]: 2021-10-08 12:46:08,655 | initial compatible: {charset-normalizer==2.0.6(wheel), idna==3.2(wheel), pycparser==2.20(wheel), simple-salesforce==1.11.4(wheel), certifi==2021.10.8(wheel), authlib==0.15.4(wheel), requests==2.26.0(wheel), urllib3==1.26.7(wheel)}
2021-10-08 12:46:08,655 | initial incompatible: {cryptography==35.0.0(wheel), debugpy==1.5.0(wheel), cffi==1.14.6(wheel)}
2021-10-08 12:46:08,655 | Downloading missing wheels: {cryptography==35.0.0(wheel), debugpy==1.5.0(wheel), cffi==1.14.6(wheel)}
2021-10-08 12:46:08,655 | calling pip download --only-binary=:all: --no-deps --platform manylinux2014_x86_64 --implementation cp --abi cp38 --dest /var/folders/2n/xs6sz2s951ggjzbrfsv1m2n80000gp/T/tmp6qb_agm3 cryptography==35.0.0
2021-10-08 12:46:09 [INFO]: 2021-10-08 12:46:09,420 | calling pip download --only-binary=:all: --no-deps --platform manylinux2014_x86_64 --implementation cp --abi cp38 --dest /var/folders/2n/xs6sz2s951ggjzbrfsv1m2n80000gp/T/tmp6qb_agm3 debugpy==1.5.0
2021-10-08 12:46:10 [INFO]: 2021-10-08 12:46:10,089 | calling pip download --only-binary=:all: --no-deps --platform manylinux2014_x86_64 --implementation cp --abi cp38 --dest /var/folders/2n/xs6sz2s951ggjzbrfsv1m2n80000gp/T/tmp6qb_agm3 cffi==1.14.6
2021-10-08 12:46:10 [INFO]: 2021-10-08 12:46:10,766 | compatible wheels after second download pass: {charset-normalizer==2.0.6(wheel), debugpy==1.5.0(wheel), pycparser==2.20(wheel), idna==3.2(wheel), simple-salesforce==1.11.4(wheel), cffi==1.14.6(wheel), certifi==2021.10.8(wheel), authlib==0.15.4(wheel), cryptography==35.0.0(wheel), requests==2.26.0(wheel), urllib3==1.26.7(wheel)}
2021-10-08 12:46:10 [INFO]: 2021-10-08 12:46:10,766 | Build missing wheels from sdists (C compiling True): set()
2021-10-08 12:46:10,766 | compatible after building wheels (no C compiling): {charset-normalizer==2.0.6(wheel), debugpy==1.5.0(wheel), pycparser==2.20(wheel), idna==3.2(wheel), simple-salesforce==1.11.4(wheel), cffi==1.14.6(wheel), certifi==2021.10.8(wheel), authlib==0.15.4(wheel), cryptography==35.0.0(wheel), requests==2.26.0(wheel), urllib3==1.26.7(wheel)}
2021-10-08 12:46:10,766 | Build missing wheels from sdists (C compiling False): set()
2021-10-08 12:46:10,766 | compatible after building wheels (C compiling): {charset-normalizer==2.0.6(wheel), debugpy==1.5.0(wheel), pycparser==2.20(wheel), idna==3.2(wheel), simple-salesforce==1.11.4(wheel), cffi==1.14.6(wheel), certifi==2021.10.8(wheel), authlib==0.15.4(wheel), cryptography==35.0.0(wheel), requests==2.26.0(wheel), urllib3==1.26.7(wheel)}
2021-10-08 12:46:10 [INFO]: 2021-10-08 12:46:10,767 | Final compatible: {charset-normalizer==2.0.6(wheel), debugpy==1.5.0(wheel), pycparser==2.20(wheel), idna==3.2(wheel), simple-salesforce==1.11.4(wheel), cffi==1.14.6(wheel), certifi==2021.10.8(wheel), authlib==0.15.4(wheel), cryptography==35.0.0(wheel), requests==2.26.0(wheel), urllib3==1.26.7(wheel)}
2021-10-08 12:46:10,767 | Final incompatible: {cryptography==35.0.0(wheel), debugpy==1.5.0(wheel), cffi==1.14.6(wheel)}
2021-10-08 12:46:10,767 | Final missing wheels: set()
2021-10-08 12:46:10 [INFO]: 2021-10-08 12:46:10,939 | PythonPipBuilder:ResolveDependencies succeeded
2021-10-08 12:46:10 [INFO]: 2021-10-08 12:46:10,939 | Running PythonPipBuilder:CopySource
2021-10-08 12:46:10 [INFO]: 2021-10-08 12:46:10,941 | PythonPipBuilder:CopySource succeeded
2021-10-08 12:46:10 [INFO]:
Build Succeeded
2021-10-08 12:46:10 [INFO]:
Built Artifacts : private/tmp/aws-toolkit-vscode/vsctkNElW2P/output
Built Template : private/tmp/aws-toolkit-vscode/vsctkNElW2P/output/template.yaml
Commands you can use next
=========================
[*] Invoke Function: sam local invoke -t private/tmp/aws-toolkit-vscode/vsctkNElW2P/output/template.yaml
[*] Deploy: sam deploy --guided --template-file private/tmp/aws-toolkit-vscode/vsctkNElW2P/output/template.yaml
2021-10-08 12:46:10 [INFO]: 2021-10-08 12:46:10,946 | Sending Telemetry: {'metrics': [{'commandRun': {'requestId': '76dfcd79-5462-4836-ad9d-b3e1f335c2c3', 'installationId': 'cf2416da-1d02-4383-a7a0-ad98cbef5e60', 'sessionId': '81d37c99-8381-4b59-88c7-45965a25e6e0', 'executionEnvironment': 'CLI', 'ci': False, 'pyversion': '3.8.12', 'samcliVersion': '1.33.0', 'awsProfileProvided': False, 'debugFlagProvided': True, 'region': '', 'commandName': 'sam build', 'duration': 4854, 'exitReason': 'success', 'exitCode': 0}}]}
2021-10-08 12:46:11 [INFO]: 2021-10-08 12:46:11,045 | Telemetry response: 200
2021-10-08 12:46:11 [INFO]: Build complete.
2021-10-08 12:46:11 [INFO]: Starting SAM application locally
2021-10-08 12:46:11 [INFO]: AWS.running.command
2021-10-08 12:46:11,442 | Telemetry endpoint configured to be https://aws-serverless-tools-telemetry.us-west-2.amazonaws.com/metrics
2021-10-08 12:46:11,443 | Using config file: samconfig.toml, config environment: default
2021-10-08 12:46:11,443 | Expand command line arguments to:
2021-10-08 12:46:11,443 | --template_file=/tmp/aws-toolkit-vscode/vsctkNElW2P/output/template.yaml --debug_port=(5859,) --debugger_path=/Users/sedenfield/.vscode/extensions/amazonwebservices.aws-toolkit-vscode-1.30.0/resources/debugger --debug_args=/tmp/lambci_debug_files/py_debug_wrapper.py --listen 0.0.0.0:5859 --wait-for-client --log-to-stderr --debug --function_logical_id=HelloWorldFunction --no_event --layer_cache_basedir=/Users/sedenfield/.aws-sam/layers-pkg --container_host=localhost --container_host_interface=127.0.0.1
2021-10-08 12:46:11,443 | local invoke command is called
2021-10-08 12:46:11,447 | No Parameters detected in the template
2021-10-08 12:46:11,463 | 2 stacks found in the template
2021-10-08 12:46:11,463 | No Parameters detected in the template
2021-10-08 12:46:11,477 | 2 resources found in the stack
2021-10-08 12:46:11,477 | No Parameters detected in the template
2021-10-08 12:46:11,491 | Found Serverless function with name='HelloWorldFunction' and CodeUri='HelloWorldFunction'
2021-10-08 12:46:11,491 | --base-dir is not presented, adjusting uri HelloWorldFunction relative to /tmp/aws-toolkit-vscode/vsctkNElW2P/output/template.yaml
2021-10-08 12:46:11,502 | Found one Lambda function with name 'HelloWorldFunction'
2021-10-08 12:46:11,502 | Invoking app.lambda_handler (python3.8)
2021-10-08 12:46:11,502 | No environment variables found for function 'HelloWorldFunction'
2021-10-08 12:46:11,502 | Environment variables overrides data is standard format
2021-10-08 12:46:11,502 | Loading AWS credentials from session with profile 'None'
2021-10-08 12:46:11,510 | Resolving code path. Cwd=/tmp/aws-toolkit-vscode/vsctkNElW2P/output, CodeUri=/tmp/aws-toolkit-vscode/vsctkNElW2P/output/HelloWorldFunction
2021-10-08 12:46:11,510 | Resolved absolute path to code is /tmp/aws-toolkit-vscode/vsctkNElW2P/output/HelloWorldFunction
2021-10-08 12:46:11,510 | Code /tmp/aws-toolkit-vscode/vsctkNElW2P/output/HelloWorldFunction is not a zip/jar file
2021-10-08 12:46:11,532 | Skip pulling image and use local one: public.ecr.aws/sam/emulation-python3.8:rapid-1.33.0-x86_64.
2021-10-08 12:46:11,532 | Mounting /tmp/aws-toolkit-vscode/vsctkNElW2P/output/HelloWorldFunction as /var/task:ro,delegated inside runtime container
2021-10-08 12:46:11,847 | Setting up SIGTERM interrupt handler
START RequestId: e589e4ae-954e-40bb-b5bf-a50eeac781ea Version: $LATEST
Prepending Lambda task root to path: /var/task
Starting debugger...
I+00000.108: Initial environment:
System paths:
sys.prefix: /var/lang
sys.base_prefix: /var/lang
sys.real_prefix: <missing>
site.getsitepackages(): /var/lang/lib/python3.8/site-packages
site.getusersitepackages(): /root/.local/lib/python3.8/site-packages
sys.path (site-packages): /var/lang/lib/python3.8/site-packages
sysconfig.get_path('stdlib'): /var/lang/lib/python3.8
sysconfig.get_path('platstdlib'): /var/lang/lib/python3.8
sysconfig.get_path('purelib'): /var/lang/lib/python3.8/site-packages
sysconfig.get_path('platlib'): /var/lang/lib/python3.8/site-packages
sysconfig.get_path('include'): /var/lang/include/python3.8
sysconfig.get_path('scripts'): /var/lang/bin
sysconfig.get_path('data'): /var/lang
os.__file__: /var/lang/lib/python3.8/os.py
threading.__file__: /var/lang/lib/python3.8/threading.py
I+00000.111: sys.argv before parsing: ['/tmp/lambci_debug_files/py_debug_wrapper.py', '--listen', '0.0.0.0:5859', '--wait-for-client', '--log-to-stderr', '/var/runtime/bootstrap.py']
after parsing: ['/tmp/lambci_debug_files/py_debug_wrapper.py']
D+00000.112: sys.argv after patching: ['/var/runtime/bootstrap.py']
D+00000.113: configure({'qt': 'none', 'subProcess': True}, {})
D+00000.113: listen(('0.0.0.0', 5859), **{})
I+00000.114: Initial debug configuration: {
"qt": "none",
"subProcess": true,
"python": "/var/lang/bin/python3.8"
}
I+00000.118: Waiting for adapter endpoints on 127.0.0.1:33875...
I+00000.119: debugpy.listen() spawning adapter: [
"/var/lang/bin/python3.8",
"/var/task/debugpy/adapter",
"--for-server",
"33875",
"--host",
"0.0.0.0",
"--port",
"5859",
"--server-access-token",
"f4e6cf20408d6433d40ba69c13728c3b22170af0354e90aa7c225db4f6df4ce4"
]
I+00000.885: Endpoints received from adapter: {
"client": {
"host": "0.0.0.0",
"port": 5859
},
"server": {
"host": "127.0.0.1",
"port": 41763
}
}
I+00000.885: Adapter is accepting incoming client connections on 0.0.0.0:5859
D+00000.887: pydevd.settrace(*(), **{'host': '127.0.0.1', 'port': 41763, 'wait_for_ready_to_run': False, 'block_until_connected': True, 'access_token': 'f4e6cf20408d6433d40ba69c13728c3b22170af0354e90aa7c225db4f6df4ce4', 'suspend': False, 'patch_multiprocessing': True, 'dont_trace_start_patterns': ('/var/task/debugpy',), 'dont_trace_end_patterns': ('debugpy_launcher.py',)})
I+00001.109: pydevd is connected to adapter at 127.0.0.1:41763
Debugger waiting for client...
D+00001.111: wait_for_client()
2021-10-08 12:46:16 [INFO]: Attaching debugger to SAM application...
2021-10-08 12:46:16 [INFO]: Debugger attached
I+00001.333: Pre-launch environment:
System paths:
sys.prefix: /var/lang
sys.base_prefix: /var/lang
sys.real_prefix: <missing>
site.getsitepackages(): /var/lang/lib/python3.8/site-packages
site.getusersitepackages(): /root/.local/lib/python3.8/site-packages
sys.path (site-packages): /var/lang/lib/python3.8/site-packages
sysconfig.get_path('stdlib'): /var/lang/lib/python3.8
sysconfig.get_path('platstdlib'): /var/lang/lib/python3.8
sysconfig.get_path('purelib'): /var/lang/lib/python3.8/site-packages
sysconfig.get_path('platlib'): /var/lang/lib/python3.8/site-packages
sysconfig.get_path('include'): /var/lang/include/python3.8
sysconfig.get_path('scripts'): /var/lang/bin
sysconfig.get_path('data'): /var/lang
os.__file__: /var/lang/lib/python3.8/os.py
threading.__file__: /var/lang/lib/python3.8/threading.py
I+00001.337: Running file '/var/runtime/bootstrap.py'
Results in VS Code popup

Expected result:
To import modules without errors For VS Code to not expect urllib3 in my sam app project dir
Additional environment details (Ex: Windows, Mac, Amazon Linux etc)
- OS: macOS 11.6 (M1 Mac)
sam --version: 1.33- AWS region: local debugging in VS Code (but i have us-west-2, in my aws config file)
Thank you for your time
Issue Analytics
- State:
- Created 2 years ago
- Reactions:2
- Comments:8 (5 by maintainers)
Top Results From Across the Web
SAM python debug: tries to resolve non-existent files #2155
VSCode throws an error window in the bottom right with the following message: Unable to open 'socks.py': Unable to read file ...
Read more >VSCode Debugger unable to resolve non-existent files while ...
The lambda function has been tested locally using the aws-sam-cli 's sam build and sam ... Unable to open 'socks.py': Unable to read...
Read more >Running and debugging Lambda functions directly from code
Run and debug the serverless application directly without an AWS SAM ... It has a *.csproj file in its parent folder within the...
Read more >Create, test & debug AWS Lambda functions locally and ...
Create, test & debug AWS Lambda functions locally and deploy to AWS using VS Code, the AWS Toolkit and SAM CLI.
Read more >How to connect VSCode Node debugger to local SAM
I am trying to switch from the Serverless Framework to SAM for Lambda/API development. Serverless makes it very easy to debug locally using...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Alright so here’s the full answer:
debugpy 1.5.0now reports uncaught ‘User’ exceptionsurllib3package is used bysimple-salesforce, which throws ifPySocksis not installedsocksmodule now, however, the extension does not know that the file is from a package, not user codeSee https://github.com/microsoft/debugpy/issues/750
There’s a few ways to fix without downgrading:
PySocksas a dependencyUser Uncaught Exceptionspip install -r requirements.txt -t .(worst option)The AWS Toolkit might be able to fine-tune the path mappings to target the correct modules, but no guarantees. SAM CLI could also put all installed packages in their own directory, that way we can easily add path mappings to them.
@JadenSimon Thanks for the explanation and the workarounds!
@shareefer Can you please create the issue in aws-toolkit-vscode to track?
Closing the issue as there is a workaround and we can’t actually fix the issue from SAM CLI