SageMaker Notebook Instance: cannot import name 'get_execution_role' from 'sagemaker'

0

I am trying to run a pytorch estimator on a local instance. I have a basic script from a SageMaker Studio Notebook which loads the session, region, and role. Because Studio notebooks do not support local instance types, I migrated to Notebook instances. Immediately when I run the first code block, I receive an error.
Environment:

  • SageMaker Notebook Instance.
  • Kernel: conda_pytorch_p39
import sagemaker
from sagemaker import get_execution_role
from sagemaker.pytorch import PyTorch

# Get the necessary information
sagemaker_session = sagemaker.Session()
role = get_execution_role()

Cannot import name 'get_execution_role' from 'sagemaker' (/home/ec2-user/SageMaker/AI/sagemaker.py)

I also tried the following:

import sagemaker

sagemaker_session = sagemaker.Session()
bucket = sagemaker_session.default_bucket()
role = sagemaker.get_execution_role()

module 'sagemaker' has no attribute 'Session'

I have tried multiple variations of this to no avail. Please help.

Samuel
asked a year ago587 views
1 Answer
1
Accepted Answer

You seem to have a sagemaker.py in your working directory that the cell is importing. get_execution_role, session etc. are part of the sagemaker python SDK (https://sagemaker.readthedocs.io/en/stable/overview.html).

Not sure why you have the sagemaker.py file, if it's a helper function, try renaming it and trying the cell again. SageMaker notebooks should have sagemaker already installed, you can also install/upgrade it using pip.

AWS
Durga_S
answered a year ago
  • Wow, I sure do. That was a silly mistake. Thank you for the fast response, Duraga_S!

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions