def _load_local_environment(mlproject, use_conda): ''' Returns Environment object for project execution on local compute. If the MLproject contains a conda environment specification and use_conda is True from the backend_config object, we create a new environment from that specification for project training. If use_conda is False, we use the local, activated conda environment. :param mlproject: Project object loaded by Mlflow :param use_conda: bool :rtype environment: AzureML Environment object ''' if mlproject.docker_env: '''Docker support is WIP!''' raise ExecutionException( "Docker support is in progress, please specify a conda environment instead" ) elif use_conda is True and mlproject.conda_env_path: print( _CONSOLE_MSG.format( "Creating conda environment from Mlproject for local run")) try: environment = Environment.from_conda_specification( name="environment", file_path=mlproject.conda_env_path) environment.python.user_managed_dependencies = False # check if azureml-mlflow in pip dependencies if environment.python.conda_dependencies._get_pip_package_with_prefix( "azureml-mlflow") == []: _logger.warning( "WARNING: MLproject doesn't contain pip dependency azureml-mlflow. Adding it now..." ) environment.python.conda_dependencies.add_pip_package( "azureml-mlflow") except ExecutionException as e: raise ExecutionException(e) return environment else: conda_home = os.environ.get('CONDA_DEFAULT_ENV') if conda_home: try: if conda_home == "base": _logger.warning( "WARNING: Using inactive base conda environement for local run" ) environment = Environment.from_existing_conda_environment( "environment", conda_home) environment.python.user_managed_dependencies = True print( _CONSOLE_MSG.format( "Using local conda environment {} for local run". format(conda_home))) except ExecutionException as e: raise ExecutionException(e) return environment else: raise ExecutionException( "Local conda environment not found, check your path.")
def create_environment_from_conda_file(conda_path, name="amlenv"): """Creates environment from supplied conda file Args: conda_path (str): path to conda environment file name (str, optional): name of environment. Defaults to "amlenv". Returns: azureml.core.Environment """ return Environment.from_existing_conda_environment(name, conda_path)
def create_environment_from_local(name="amlenv", conda_env_name=None): """Creates environment from environment If no value is passed in to the conda_env_name it will simply select the currently running environment Args: name (str, optional): name of environment. Defaults to "amlenv". conda_env_name (str, optional): name of the environment to use. Defaults to None. Returns: azureml.core.Environment """ conda_env_name = os.getenv( "CONDA_DEFAULT_ENV") if conda_env_name is None else conda_env_name return Environment.from_existing_conda_environment(name, conda_env_name)
- pandas - scikit-learn - pip: - azureml-defaults #then use this to make ML enviro from azureml.core import Environment env = Environment.from_conda_specification(name='training_environment', file_path='./conda.yml') #make enviro from existing Conda from azureml.core import Environment env = Environment.from_existing_conda_environment(name='training_environment', conda_environment_name='py_env') #by specifying pkg from azureml.core import Environment from azureml.core.conda_dependencies import CondaDependencies env = Environment('training_environment') deps = CondaDependencies.create(conda_packages=['scikit-learn','pandas','numpy'], pip_packages=['azureml-defaults']) env.python.conda_dependencies = deps #configuring enviro containers env.docker.enabled = True deps = CondaDependencies.create(conda_packages=['scikit-learn','pandas','pip'],
def get_environment(workspace, name, pip_requirements=None, conda_specification=None, conda_env=None, docker_image=None, docker_file=None, override=False, inference_stack=None): """ Get an Azure ML environment from PIP or Conda. From: - pip_requirements - conda_specification - conda_env at most one can be provided. If none is provided, it is assumed that the requirements are taken care of by the user. From: - docker_image - docker_file at most one can be provided. If none is provided, the base Azure image is used. :params workspace: The Azure ML workspace to look for existing environments. :params name: Name for this environment :params pip_requirements: Path to the pip requirements file :params conda_specifidation: Path to the conda specification file :params conda_env: Name of the conda environment to use :params docker_image: Base the image off an existing docker image :params docker_file: Base the image off a Dockerfile. :params override: Create a new environment with this name, regardless of if one already exists. :params inference_stack: Add a stack that enables this environment for inference. "latest" is a valid option. Set to None to not add this. :returns: Azure ML environment or None in case of failure """ if not override: try: env = Environment.get(workspace, name) print("Existing environment found, using that") return env except: print("No environment with that name found, creating new one") # Validate at most one of pip_requirements, conda_specification, conda_env # is provided if sum([ 1 for x in [pip_requirements, conda_specification, conda_env] if x is not None ]) > 1: print("Provide at most 1 of pip_requirements, conda_specification, " "conda_env") return None # Validate that at most one of docker_image, docker_file is # provided if sum([1 for x in [docker_image, docker_file] if x is not None]) > 1: print("Provide at most 1 of docker_image, docker_file") return None if pip_requirements is not None: env = Environment.from_pip_requirements(name, pip_requirements) elif conda_specification is not None: env = Environment.from_conda_specification(name, conda_specification) elif conda_env is not None: env = Environment.from_existing_conda_environment(name, conda_env) else: env = Environment(name) env.python.user_managed_dependencies = True if docker_file is not None: env.docker.enabled = True env.docker.base_image = None env.docker.base_dockerfile = docker_file elif docker_image is not None: env.docker.enabled = True env.docker.base_image = docker_image if inference_stack is not None: env.inferencing_stack_version = inference_stack # Register environment env.register(workspace=workspace) return env