create_env | Manage User Environments | Open Analytics Framework - create_env - Teradata Vantage

Teradata® VantageCloud Lake

Deployment
VantageCloud
Edition
Lake
Product
Teradata Vantage
Published
January 2023
Language
English (United States)
Last Update
2024-04-03
dita:mapPath
phg1621910019905.ditamap
dita:ditavalPath
pny1626732985837.ditaval
dita:id
phg1621910019905

Use the create_env function to create isolated remote user environment or environments in the Open Analytics Framework that includes a specific Python or R language interpreter version. Available base Python or R environments can be found using list_base_envs function.

When template argument is provided, files and libs are installed if specified in the template file.

You can create multiple environments based on the specifications in a single template file. If an environment creation fails, a failure message appears on console and the next environment creation starts.

Optional Arguments

Either env_name or template argument must be specified.
env_name
Specifies the name of the environment to be created.
This argument is required when template is not used. Otherwise, it is optional.
template
Specifies the path to template json file containing details of the user environment or environments to be created.
This argument is required when env_name is not used. Otherwise, it is optional.
By using the template file, you can create one or more user environments with same or different requirements. The template file can contain the following information about environments to be created:
  • Name of the environment. (Required)
  • Base Python version to be used. (Optional)
  • Description for the environment. (Optional)
  • Files or libraries to be installed in the environment. (Optional).
An example template file:
{
   "env_specs" : [
       {
           "env_name" : "<name of the user environment_MUST_BE_SPECIFIED>",
           "base_env" : "<OPTIONAL_base_env>",
           "desc": "<OPTIONAL_env_description>",
           "libs": ["<OPTIONAL>", "<List_of_libs_to_be_installed>"] OR "<location_of_requirements.txt>"
           "files": ["<OPTIONAL>", "<full_path_the_file>", "<full_path_to_dir>"]
       },
       {
           "env_name" : "   .",
           "base_env" : "   ",
           "desc": "..",
           "libs": ..
           "files":    
       },
       {
              
       },
       {
              
       }
              ]
}
  • Template file can contain details about single or multiple environments to be created. At least one is required.
  • Content of template file must adhere to the syntax mentioned here. Check the example for more details.
base_env
Specifies the name of the Python or R base environment to be used to create remote user environment, when env_name is provided.
This argument is ignored when template is provided.
When base_env is not provided, highest available Python base environment version listed in list_base_env() is used. To check available base environments, use list_base_envs.

The format of this argument is python_<version number> and r_<version_number>.

  • For Python or R user environment, the version number matches the version number listed in list_base_envs.
  • For conda environment, you can use any Python version supported by Anaconda, irrespective of base environments listed with list_base_envs.
desc
Specifies description about the remote environment when env_name is provided.
This argument is ignored when template is provided.
Default value of this argument is "This env 'env_name' is created with base env 'base_env'."
conda_env
Specifies whether the environment to be created is a conda environment.
When set to True, conda environment is created. Otherwise, non-conda environment is created.
Default value is False.
Currently, only Python conda environment is supported.

This function returns an object of class UserEnv representing the remote user environment.

When template file provided with template argument has specifications for multiple environments, an object of class UserEnv representing the last created environmentis returned.

Example 1: Create a Python 3.7.13 environment with given name and description in Vantage

>>> fraud_detection_env = create_env('Fraud_detection',
                                     'python_3.7.13',
                                     'Fraud detection through time matching')
    User environment 'Fraud_detection' created.

Example 2: Create a R 4.1.3 environment with given name and description in Vantage

>>> fraud_detection_env = create_env('Carbon_Credits',
                                     'r_4.1',
                                     'Prediction of carbon credits consumption')
    User environment 'Carbon_Credits' created.

Example 3: Create multiple environments, and install files and libraries

This example creats multiple environments and install files and libraries in these environments by providing specifications in template file.

  • Create a template json file.
    >>> import teradataml, os, json
    >>> tdml_data_path = os.path.join(os.path.dirname(teradataml.__file__), "data")
        python_base_env = "python_3.9.13"
        r_base_env = "r_4.1"
        env_specs = [
            {
                "env_name": "env_1",
                "base_env": python_base_env,
                "desc": "Desc for test env 1"
            },
            {
                "env_name": "env_2",
                "base_env": r_base_env,
                "libs": ["glm2", "stringi"]
                "files": [os.path.join(tdml_data_path, "load_example_data.py"),
                          os.path.join(tdml_data_path, "scripts")]
            }
        ]
        json_data = {"env_specs": env_specs}
        with open("template.json", "w") as json_file:
           json.dump(json_data, json_file)
  • Create environments.
    >>> create_env(template="template.json")
    Creating environment 'env_1'...
    User environment 'env_1' created.
    An empty environment 'env_1' is created.
    Created environment 'env_1' with specified requirements.
    Environment Name: env_1
    Base Environment: python_3.9.13
    Description: Desc for test env 1
     
    Creating environment 'env_2'...
    User environment 'env_2' created.
    An empty environment 'env_2' is created.
    Installing files in environment 'env_2'...
    File 'load_example_data.py' installed successfully in the remote user environment 'env_2'.
    File 'mapper.py' installed successfully in the remote user environment 'env_2'.
    File 'mapper.R' installed successfully in the remote user environment 'env_2'.
    File 'mapper_replace.py' installed successfully in the remote user environment 'env_2'.
    File installation in environment 'env_2' - Completed.
    Created environment 'env_2' with specified requirements.
    Environment Name: env_2
    Base Environment: r_4.1
    Description: This env 'env_2' is created with base env 'r_4.1'.
     
     
    ############ Files installed in User Environment ############
     
                       File   Size             Timestamp
    0             mapper.py    547  2023-11-07T10:14:06Z
    1              mapper.R    613  2023-11-07T10:14:09Z
    2  load_example_data.py  14158  2023-11-07T10:14:03Z
    3     mapper_replace.py    552  2023-11-07T10:14:12Z
     
     
    ############ Libraries installed in User Environment ############
     
              name  version
    0   KernSmooth  2.23-20
    1         MASS   7.3-55
    2       Matrix    1.4-0
    3         base    4.1.3
    4         boot   1.3-28
    5        class   7.3-20
    6      cluster    2.1.2
    7    codetools   0.2-18
    8     compiler    4.1.3
    9     datasets    4.1.3
    10     foreign   0.8-82
    11   grDevices    4.1.3
    12    graphics    4.1.3
    13        grid    4.1.3
    14     lattice  0.20-45
    15     methods    4.1.3
    16        mgcv   1.8-39
    17        nlme  3.1-155
    18        nnet   7.3-17
    19    parallel    4.1.3
    20     remotes    2.4.2
    21       rpart   4.1.16
    22     spatial   7.3-15
    23     splines    4.1.3
    24       stats    4.1.3
    25      stats4    4.1.3
    26    survival   3.2-13
    27       tcltk    4.1.3
    28       tools    4.1.3
    29       utils    4.1.3

Example 4: Create a conda Python 3.8 environment with given name and description in Vantage

>>> fraud_detection_env = create_env('Fraud_detection_conda',
                                     'python_3.8',
                                     'Fraud detection through time matching',
                                     conda_env=True)
Conda environment creation initiated
User environment 'Fraud_detection_conda' created.