Skip to content

Commit b74e8ff

Browse files
nadiayayuanzhua
authored andcommitted
fix: move sagemaker config loading to LocalSession since it is only used for local code support. (aws#1137)
* Move sagemaker config loading to local session since it is only used for local code support. * Add a common error message function. * FIx imports order. * Improve function doc string.
1 parent 0cb2b22 commit b74e8ff

File tree

5 files changed

+51
-18
lines changed

5 files changed

+51
-18
lines changed

setup.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,17 @@ def read_version():
3939
"protobuf>=3.1",
4040
"scipy>=0.19.0",
4141
"protobuf3-to-dict>=0.1.5",
42-
"PyYAML>=3.10, <5", # required for session
4342
"requests>=2.20.0, <2.21",
4443
]
4544

4645
# Specific use case dependencies
4746
extras = {
4847
"analytics": ["pandas"],
49-
"local": ["urllib3>=1.21, <1.25", "docker-compose>=1.23.0"],
48+
"local": [
49+
"urllib3>=1.21, <1.25",
50+
"docker-compose>=1.23.0",
51+
"PyYAML>=3.10, <5", # PyYAML version has to match docker-compose requirements
52+
],
5053
"tensorflow": ["tensorflow>=1.3.0"],
5154
}
5255
# Meta dependency groups

src/sagemaker/local/image.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,6 @@
3333
from threading import Thread
3434
from six.moves.urllib.parse import urlparse
3535

36-
import yaml
37-
3836
import sagemaker
3937
import sagemaker.local.data
4038
import sagemaker.local.utils
@@ -461,6 +459,13 @@ def _generate_compose_file(self, command, additional_volumes=None, additional_en
461459
}
462460

463461
docker_compose_path = os.path.join(self.container_root, DOCKER_COMPOSE_FILENAME)
462+
463+
try:
464+
import yaml
465+
except ImportError as e:
466+
logging.error(sagemaker.utils._module_import_error("yaml", "Local mode", "local"))
467+
raise e
468+
464469
yaml_content = yaml.dump(content, default_flow_style=False)
465470
logger.info("docker compose file: \n%s", yaml_content)
466471
with open(docker_compose_path, "w") as f:

src/sagemaker/local/local_session.py

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from __future__ import absolute_import
1515

1616
import logging
17+
import os
1718
import platform
1819

1920
import boto3
@@ -28,14 +29,7 @@
2829
_LocalTransformJob,
2930
)
3031
from sagemaker.session import Session
31-
from sagemaker.utils import DeferredError, get_config_value
32-
33-
try:
34-
import urllib3
35-
except ImportError as e:
36-
logging.warning("urllib3 failed to import. Local mode features will be impaired or broken.")
37-
# Any subsequent attempt to use urllib3 will raise the ImportError
38-
urllib3 = DeferredError(e)
32+
from sagemaker.utils import get_config_value, _module_import_error
3933

4034
logger = logging.getLogger(__name__)
4135

@@ -329,6 +323,12 @@ def __init__(self, config=None):
329323
config (dict): Optional configuration for this client. In particular only
330324
the local port is read.
331325
"""
326+
try:
327+
import urllib3
328+
except ImportError as e:
329+
logging.error(_module_import_error("urllib3", "Local mode", "local"))
330+
raise e
331+
332332
self.http = urllib3.PoolManager()
333333
self.serving_port = 8080
334334
self.config = config
@@ -403,6 +403,16 @@ def _initialize(self, boto_session, sagemaker_client, sagemaker_runtime_client):
403403
self.sagemaker_runtime_client = LocalSagemakerRuntimeClient(self.config)
404404
self.local_mode = True
405405

406+
sagemaker_config_file = os.path.join(os.path.expanduser("~"), ".sagemaker", "config.yaml")
407+
if os.path.exists(sagemaker_config_file):
408+
try:
409+
import yaml
410+
except ImportError as e:
411+
logging.error(_module_import_error("yaml", "Local mode", "local"))
412+
raise e
413+
414+
self.config = yaml.load(open(sagemaker_config_file, "r"))
415+
406416
def logs_for_job(self, job_name, wait=False, poll=5):
407417
"""
408418

src/sagemaker/session.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import boto3
2626
import botocore.config
2727
from botocore.exceptions import ClientError
28-
import yaml
2928

3029
import sagemaker.logs
3130
from sagemaker import vpc_utils
@@ -95,11 +94,8 @@ def __init__(self, boto_session=None, sagemaker_client=None, sagemaker_runtime_c
9594
"""
9695
self._default_bucket = None
9796

98-
sagemaker_config_file = os.path.join(os.path.expanduser("~"), ".sagemaker", "config.yaml")
99-
if os.path.exists(sagemaker_config_file):
100-
self.config = yaml.load(open(sagemaker_config_file, "r"))
101-
else:
102-
self.config = None
97+
# currently is used for local_code in local mode
98+
self.config = None
10399

104100
self._initialize(boto_session, sagemaker_client, sagemaker_runtime_client)
105101

src/sagemaker/utils.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -608,3 +608,22 @@ def __getattr__(self, name):
608608
name:
609609
"""
610610
raise self.exc
611+
612+
613+
def _module_import_error(py_module, feature, extras):
614+
"""Return error message for module import errors, provide
615+
installation details.
616+
617+
Args:
618+
py_module (str): Module that failed to be imported
619+
feature (str): Affected sagemaker feature
620+
extras (str): Name of the extra_requirements to install all of the dependencies
621+
Returns:
622+
str: Error message with installation instructions.
623+
"""
624+
error_msg = (
625+
"Failed to import {}. {} features will be impaired or broken."
626+
"Please run \"pip install 'sagemaker[{}]'\" "
627+
"to install all required dependencies."
628+
)
629+
return error_msg.format(py_module, feature, extras)

0 commit comments

Comments
 (0)