code: def import_service(
      svc_import_path: str,
      *,
      working_dir: t.Optional[str] = None,
      standalone_load: bool = False,
      model_store: ModelStore = Provide[BentoMLContainer.model_store],
  ) -> Service:
      """Import a Service instance from source code, by providing the svc_import_path
      which represents the module where the Service instance is created and optionally
      what attribute can be used to access this Service instance in that module
  
      Example usage:
          # When multiple service defined in the same module
          import_service("fraud_detector:svc_a")
          import_service("fraud_detector:svc_b")
  
          # Find svc by Python module name or file path
          import_service("fraud_detector:svc")
          import_service("fraud_detector.py:svc")
          import_service("foo.bar.fraud_detector:svc")
          import_service("./def/abc/fraud_detector.py:svc")
  
          # When there's only one Service instance in the target module, the attributes
          # part in the svc_import_path can be omitted
          import_service("fraud_detector.py")
          import_service("fraud_detector")
      """
      from bentoml import Service
  
      prev_cwd = None
      sys_path_modified = False
      prev_cwd = os.getcwd()
      global_model_store = BentoMLContainer.model_store.get()
  
      def recover_standalone_env_change():
          # Reset to previous cwd
          os.chdir(prev_cwd)
          BentoMLContainer.model_store.set(global_model_store)
  
      try:
          if working_dir is not None:
              working_dir = os.path.realpath(
                  os.path.expanduser(working_dir)
              )
              # Set cwd(current working directory) to the Bento's project directory,
              # which allows user code to read files using relative path
              os.chdir(working_dir)
          else:
              working_dir = os.getcwd()
  
          if working_dir not in sys.path:
              sys.path.insert(0, working_dir)
              sys_path_modified = True
  
          if model_store is not global_model_store:
              BentoMLContainer.model_store.set(model_store)
  
          logger.debug(
              'Importing service "%s" from working dir: "%s"',
              svc_import_path,
              working_dir,
          )
  
          import_path, _, attrs_str = svc_import_path.partition(":")
          if not import_path:
              raise ImportServiceError(
                  f'Invalid import target "{svc_import_path}", must format as '
                  '"<module>:<attribute>" or "<module>'
              )
  
          if os.path.isfile(import_path):
              import_path = os.path.realpath(import_path)
              # Importing from a module file path:
              if not import_path.startswith(working_dir):
                  raise ImportServiceError(
                      f'Module "{import_path}" not found in working directory "{working_dir}"'
                  )
  
              file_name, ext = os.path.splitext(import_path)
              if ext != ".py":
                  raise ImportServiceError(
                      f'Invalid module extension "{ext}" in target "{svc_import_path}",'
                      ' the only extension acceptable here is ".py"'
                  )
  
              # move up until no longer in a python package or in the working dir
              module_name_parts: t.List[str] = []
              path = file_name
              while True:
                  path, name = os.path.split(path)
                  module_name_parts.append(name)
                  if (
                      not os.path.exists(
                          os.path.join(path, "__init__.py")
                      )
                      or path == working_dir
                  ):
                      break
              module_name = ".".join(module_name_parts[::-1])
          else:
              # Importing by module name:
              module_name = import_path
  
          # Import the service using the Bento's own model store
          try:
              module = importlib.import_module(
                  module_name, package=working_dir
              )
          except ImportError as e:
              raise ImportServiceError(
                  f'Failed to import module "{module_name}": {e}'
              )
          if not standalone_load:
              recover_standalone_env_change()
  
          if attrs_str:
              instance = module
              try:
                  for attr_str in attrs_str.split("."):
                      instance = getattr(instance, attr_str)
              except AttributeError:
                  raise ImportServiceError(
                      f'Attribute "{attrs_str}" not found in module "{module_name}".'
                  )
          else:
              instances = [
                  (k, v)
                  for k, v in module.__dict__.items()
                  if isinstance(v, Service)
              ]
  
              if len(instances) == 1:
                  attrs_str = instances[0][0]
                  instance = instances[0][1]
              else:
                  raise ImportServiceError(
                      f'Multiple Service instances found in module "{module_name}", use'
                      '"<module>:<svc_variable_name>" to specify the service instance or'
                      "define only service instance per python module/file"
                  )
  
          assert isinstance(
              instance, Service
          ), f'import target "{module_name}:{attrs_str}" is not a bentoml.Service instance'
  
          # set import_str for retrieving the service import origin
          object.__setattr__(
              instance, "_import_str", f"{module_name}:{attrs_str}"
          )
          return instance
      except ImportServiceError:
          if sys_path_modified and working_dir:
              # Undo changes to sys.path
              sys.path.remove(working_dir)
  
          recover_standalone_env_change()
          raise
  
imports: from __future__ import annotations
  import importlib
  import logging
  import os
  import sys
  import typing
  from typing import TYPE_CHECKING
  import fs
  from simple_di import Provide
  from simple_di import inject
  from ...exceptions import BentoMLException
  from ...exceptions import ImportServiceError
  from ...exceptions import NotFound
  from ..bento import Bento
  from ..bento.bento import BENTO_PROJECT_DIR_NAME
  from ..bento.bento import BENTO_YAML_FILENAME
  from ..bento.bento import DEFAULT_BENTO_BUILD_FILE
  from ..bento.build_config import BentoBuildConfig
  from ..configuration import BENTOML_VERSION
  from ..configuration.containers import BentoMLContainer
  from ..models import ModelStore
  from ..tag import Tag
  from .service import on_load_bento
label_desc: Use image node_id 1 for calling a global function with example usage: import_service(svc_import_path) and returns: instance