简体   繁体   中英

Why is env different in an Azure ML notbook and an Azure ML terminal?

I'm using MS Azure ML and have found that when I start a Notebook (from the Azure ML Studio) it is executing in aa different environment than if I create a Python script and run it from the studio. I want to be able to create a specific environment and have the Notebook use that. The environment that the Notebook seems to run does not contain the packages I need and I want to preserve different environments.

First open a terminal, using the same compute target as you want to use with your Notebook afterwards, and type:

conda env list

here you will the list of environments which you can use with

conda activate <myenv>

However, to create a new environment and use it in you AzureML Notebook, you have to do the following commands:

 conda create --name azureml_py38 python=3.8 conda activate new_env conda install pip conda install ipykernel python -m ipykernel install --user --name new_env --display-name "Python 3.8 - New Environment"

And then last, but not least, you have to edit the Jupyter Kernel display names.

IMPORTANT Please ensure you are comfortable running all these steps:

 jupyter kernelspec list cd <folder-that-matches-the-kernel-of-your-environment> sudo nano kernel.json <edit display-name> <save file>

Using Pip, Conda, Docker image & Dockerfile we can create ML environments for your own scripts.

I want to be able to create a specific environment

To manually create an environment,

from azureml.core.environment import Environment
Environment(name="myenv")

Use Conda dependencies or pip requirement files for creating an environment using below code:

#### From a Conda specification file
myenv = Environment.from_conda_specification(name = "myenv",
                                             file_path = "path-to-conda-specification-file")

#### From a pip requirements file
myenv = Environment.from_pip_requirements(name = "myenv",
                                          file_path = "path-to-pip-requirements-file")

The environment that the Notebook seems to run does not contain the packages I need and I want to preserve different environments.

To add the packages to your environment, can use the Conda, pip or private wheel files but it is recommended to use CondaDependency class.

from azureml.core.environment import Environment
from azureml.core.conda_dependencies import CondaDependencies

myenv = Environment(name="myenv")
conda_dep = CondaDependencies()

#### Installs numpy version 1.17.0 conda package
conda_dep.add_conda_package("numpy==1.17.0")

#### Installs pillow package
conda_dep.add_pip_package("pillow")

#### Adds dependencies to PythonSection of myenv
myenv.python.conda_dependencies=conda_dep

Register your environment in workspace using the command myenv.register(workspace=ws)

list(workspace) the list the environments in the workspace

To run your specific environment:

from azureml.core import Run
Run.get_environment()

To install a Conda environment as a kernel in a notebook, see add a new Jupyter kernel and the code should be followed in a same sequence:

conda create --name newenv
conda activate newenv
conda install pip
conda install ipykernel
python -m ipykernel install --user --name newenv --display-name "Python (newenv)"

To add, change or remove the Jupyter Kernels, please refer this article .

Refer this Document for more details

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM