aws/aws-sam-cli

Bug: SAM CLI tries to import packages from system Python on Windows

Opened this issue · 3 comments

Description:

When I run sam local or any other SAM command, they all fail:

sam local
2024-09-21 22:45:29 Command 'local' is not configured correctly. Unable to import 'samcli.commands.local.local'
Traceback (most recent call last):
  File "C:\Program Files\Amazon\AWSSAMCLI\runtime\Lib\site-packages\samcli\cli\command.py", line 264, in get_command
    mod = importlib.import_module(pkg_name)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "importlib\__init__.py", line 90, in import_module
  File "<frozen importlib._bootstrap>", line 1387, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1360, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1331, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 935, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 995, in exec_module
  File "<frozen importlib._bootstrap>", line 488, in _call_with_frames_removed
  File "C:\Program Files\Amazon\AWSSAMCLI\runtime\Lib\site-packages\samcli\commands\local\local.py", line 8, in <module>
    from .generate_event.cli import cli as generate_event_cli
  File "C:\Program Files\Amazon\AWSSAMCLI\runtime\Lib\site-packages\samcli\commands\local\generate_event\cli.py", line 8, in <module>
    from samcli.commands.local.generate_event.core.command import CoreGenerateEventCommand
  File "C:\Program Files\Amazon\AWSSAMCLI\runtime\Lib\site-packages\samcli\commands\local\generate_event\core\command.py", line 10, in <module>
    from samcli.commands.local.generate_event.event_generation import GenerateEventCommand
  File "C:\Program Files\Amazon\AWSSAMCLI\runtime\Lib\site-packages\samcli\commands\local\generate_event\event_generation.py", line 12, in <module>
    from samcli.lib.telemetry.metric import track_command
  File "C:\Program Files\Amazon\AWSSAMCLI\runtime\Lib\site-packages\samcli\lib\telemetry\metric.py", line 22, in <module>
    from samcli.lib.hook.hook_config import HookPackageConfig
  File "C:\Program Files\Amazon\AWSSAMCLI\runtime\Lib\site-packages\samcli\lib\hook\hook_config.py", line 7, in <module>
    import jsonschema
  File "C:\Program Files\Amazon\AWSSAMCLI\runtime\Lib\site-packages\jsonschema\__init__.py", line 13, in <module>
    from jsonschema._format import FormatChecker
  File "C:\Program Files\Amazon\AWSSAMCLI\runtime\Lib\site-packages\jsonschema\_format.py", line 11, in <module>
    from jsonschema.exceptions import FormatError
  File "C:\Program Files\Amazon\AWSSAMCLI\runtime\Lib\site-packages\jsonschema\exceptions.py", line 14, in <module>
    from attrs import define
  File "C:\Program Files\Amazon\AWSSAMCLI\runtime\Lib\site-packages\attrs\__init__.py", line 3, in <module>
    from attr import (
ImportError: cannot import name 'AttrsInstance' from 'attr' (C:\Users\myuser\AppData\Roaming\Python\Python312\site-packages\attr\__init__.py)
Usage: sam [OPTIONS] COMMAND [ARGS]...
Try 'sam -h' for help.

Error: No such command 'local'.

It seems that the attr package is supposed to be read from C:\Program Files\Amazon\AWSSAMCLI\runtime\Lib\site-packages but instead it is read from the user packages folder at C:\Users\<username>\AppData\Roaming\Python\Python312\site-packages.

Steps to reproduce:

  1. Install Python 3.12
  2. Add it to user's PATH environment variable
  3. Install SAM CLI
  4. Install the attrs package: pip install --user attrs==18.1.0

Note: C:\Program Files\Amazon\AWSSAMCLI\bin comes before C:\Program Files\Python312 in PATH - I double checked.

Observed result:

SAM commands don't work.

Expected result:

Expected them to not use my Python installation but use the one that ships with the installer.

Additional environment details (Ex: Windows, Mac, Amazon Linux etc)

  1. OS: Windows 10
  2. sam --version: SAM CLI, version 1.124.0
  3. AWS region: -
{
  "version": "1.124.0",
  "system": {
    "python": "3.12.6",
    "os": "Windows-10-10.0.19045-SP0"
  },
  "additional_dependencies": {
    "docker_engine": "Not available",
    "aws_cdk": "Not available",
    "terraform": "Not available"
  },
  "available_beta_feature_env_vars": [
    "SAM_CLI_BETA_FEATURES",
    "SAM_CLI_BETA_BUILD_PERFORMANCE",
    "SAM_CLI_BETA_TERRAFORM_SUPPORT",
    "SAM_CLI_BETA_RUST_CARGO_LAMBDA"
  ]
}

Python version: 3.12.6

Adding set PYTHONNOUSERSITE=1 right before the python.exe line in C:\Program Files\Amazon\AWSSAMCLI\bin\sam.cmd fixed it for me.

set PYTHONNOUSERSITE=1
"%~dp0/../runtime/python.exe" -m samcli %*

Hey @sryze, thanks for opening this. I don't appear to be able to reproduce the same issue.

I have both Python 3.9 and 3.12 installed for testing reasons, and also moved SAM CLI to the top of the list. I was at the bottom for me initially. Running sam init works fine on my end, whether the entry for SAM CLI is at the top or bottom.

Screenshot 2024-09-26 at 11 11 23

This is what my sam.cmd looks like:

@rem
@echo off

setlocal

"%~dp0/../runtime/python.exe" -m samcli %*�

Is this new behaviour with 1.124.0? ie. have you used older versions of SAM CLI before this that didn't break this way?

This might sound trivial, but can you try reinstalling SAM CLI?

@lucashuy Sorry, I forgot to mention that to reproduce this bug you must have the package attrs installed, specifically version 18.1.0. In my case it's a dependency of another package (coursera-dl).

pip install --user attrs==18.1.0