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
已提問 1 年前檢視次數 560 次
1 個回答
1
已接受的答案

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
已回答 1 年前
  • Wow, I sure do. That was a silly mistake. Thank you for the fast response, Duraga_S!

您尚未登入。 登入 去張貼答案。

一個好的回答可以清楚地回答問題並提供建設性的意見回饋,同時有助於提問者的專業成長。

回答問題指南