4.10 Use the Conda Interpreter in a Notebook Paragraph

Oracle Machine Learning Notebooks provides a Conda interpreter to enable administrators to create conda environments with custom third-party Python and R libraries. Once created, you can download and activate Conda environments inside a notebook session also using the Conda interpreter.

An Oracle Machine Learning notebook supports multiple languages. For this, you must create a notebook with some paragraphs to run SQL queries, and other paragraphs to run PL/SQL scripts. To run a notebook in different scripting languages, you must first connect the notebook paragraphs with the respective interpreters such as SQL, PL/SQL, R, Python, or Conda.

This topic shows how to start working in the Conda environment:
  • Connect to the Conda interpreter
  • Download and activate the Conda environment
  • View the list of packages in the Conda environment
  • Run a Python function to import the Iris dataset, and use the seaborn package for visualization
  1. Type %conda at the beginning of the paragraph to connect to the Conda interpreter, and press Enter.
    %conda
  2. Next, download and activate the Conda environment. Type:
    download sbenv
    activate sbenv
    In this example, the Conda environment is downloaded and activated. The name of the Conda environment in this example is sbenv.Download and Activate the Conda Environment
  3. You can view all the packages that are present in the Conda environment. To view the list of packages, type list.
    List of packages in the seaborn library
  4. Here's an example that demonstrates how to use the seaborn library package for visualization. Seaborn is a Python visualization library based on matplotlib. It provides a high-level interface for drawing attractive statistical graphics. This example
    • Imports Pandas and seaborn
    • Loads the Iris dataset
    • Plots the datapoints, that is, the three different species of the Iris flower - Setosa, Virginica, and Versicolor based on its dimensions. It creates a scatter plot
    Type:
    %python
    
    def sb_plot():
        import pandas as pd
        import seaborn as sb
        from matplotlib import pyplot as plt
        df = sb.load_dataset('iris')
        sb.set_style("ticks")
        sb.pairplot(df,hue = 'species',diag_kind = "kde",kind = "scatter",palette = "husl")
        plt.show()
    Commands
  5. Run the function in a Python paragraph.
    Type:
    %python 
    sb_plot()
    Visualization using Seaborn Python library

4.10.1 About the Conda Environment and Conda Interpreter

Conda is an open-source package and environment management system that allows the use of environments containing third-party Python and R libraries. Oracle Machine Learning User Interface (UI) provide the conda interpreter to install third-party Python and R libraries inside a notebook session.

Third-party libraries installed in Oracle Machine Learning Notebooks can be used in:
  • Standard Python
  • Standard R
  • Oracle Machine Learning for Python embedded Python execution from the Python, SQL and REST APIs
  • Oracle Machine Learning for R embedded R execution from the R, SQL, and REST APIs

To start working in the Conda environment:

  1. Ensure that a Conda environment is saved to Object Storage, or update an existing package by installing a new version.

    Note:

    You must be signed in as ADMIN user to create the Conda environment. The administrator manages the lifecycle of an environment, including adding or deleting packages from it, and removing environments. The Conda environments are stored in an Object Storage bucket associated with the Autonomous AI Database.
  2. Sign into Oracle Machine Learning UI and download the Conda environment. To download the Conda environment, type:
    %conda 
    download myenv
  3. Activate the Conda environment. To activate the Conda environment, type:
    activate myenv

    Note:

    There is only one active Conda environment at a given point in time.
  4. Create a notebook, use the Conda interpreter to use third-party libraries in the Object Storage. To use the Conda interpreter, type %conda at the beginning of the paragraph to connect to the Conda environment, and work with third-party libraries for Python. You can switch between the preinstalled Conda environments. For example, you can have an environment for working with Graph analysis, and another environment for Oracle Machine Learning analysis.
  5. Deactivate the Conda environment. As a best practice, deactivate the Conda environment after you have finished working on your machine learning analysis. To deactivate the environment, type:
    deactivate

4.10.2 Conda Interpreter Commands

This table lists the commands for the Conda interpreter.

Conda Interpreter Commands

Table 4-1 Conda Interpreter Commands

Tasks Commands Role
Create a Conda environment. create -n <env_name> <python_version>
  • ADMIN
Remove a list of packages from a specified conda environment. It is also an alias for conda uninstall. remove -n <env_name> --all

Note:

The Conda environment is deleted from the user session.
  • ADMIN
  • OML user
List user-created local environment. env list
  • ADMIN
  • OML user
Remove user-created local environment. env remove -n <env_name>
  • ADMIN
  • OML user
List all packages and versions installed in active environment. list
  • ADMIN
  • OML user
Activate a user-created local environment. activate -n <env_name>
  • ADMIN
  • OML user
Deactivate the current environment. deactivate
  • ADMIN
  • OML user
Install an external package from a public Conda channel. install -n <env_name> <package_name>
  • ADMIN
Uninstall a specific package from a Conda environment. It is also an alias for remove. uninstall -n <env_name> <package_name>
  • ADMIN
Display information about current conda install. info
  • ADMIN
  • OML user
View the command line help. COMMANDNAME --help
  • ADMIN
  • OML user
Upload a Conda environment to object storage.

Note:

This is an Oracle Autonomous AI Database specific command.
upload --overwrite <env_name> --description 'some description' -t <name> <value>

Note:

You can provide many tags. For example: -t <name1> <value1> -t <name2> <value2> ..
  • ADMIN
Download and unpack a specific Conda environment from the object storage.

Note:

This is an Oracle Autonomous AI Database specific command.
download --overwrite <env_name>
  • ADMIN
  • OML user
List local environments available to the user. list-local-envs
  • ADMIN
  • OML user
List all Conda environments in the object storage.

Note:

This is an Oracle Autonomous AI Database specific command.
list-saved-envs --installed-packages -e <env_name>
  • ADMIN
  • OML user
Delete a Conda environment.

Note:

This is an Oracle Autonomous AI Database specific command.
delete <env_name>

Note:

The Conda environment is deleted from the Object Storage.
  • ADMIN
Update conda packages to the latest compatible version. update
  • ADMIN
Upgrade the current conda package. It is also an alias for conda update. upgrade
  • ADMIN
Search for packages and display associated information. The input is a MatchSpec, a query language for conda packages. search
  • ADMIN
  • OML user

4.10.3 Create a Conda Environment for Python, Install a Python Package and Upload it to Object Storage

This topic demonstrates how to create a Conda environment mypyenv for Python packages, with Python 3.13.5 that is compatible with OML4Py. Here, you will also install the Python package seaborn from the conda-forge channel, and upload the Conda environment mypyenv to object storage.

Conda channels are the locations where packages are stored. They serve as the base for hosting and managing packages. Conda packages are downloaded from remote channels, which are URLs to directories containing conda packages. The conda command searches a set of channels. By default, packages are automatically downloaded and updated from the default channel. The conda-forge channel is a community channel made up of thousands of contributors, and is free for all to use. You can modify what remote channels are automatically searched. You might want to do this to maintain a private or internal channel.

Note:

You must be signed in as ADMIN user to create the Conda environment.

Note:

To avoid version conflicts, please do not install packages that are already included in OML4Py. For a complete list of pre-installed packages and their versions, see Python Libraries in OML4Py.
To create a Conda environment:
  1. Run the following command to list the environments that are available by default. Conda contains default environments with some core system libraries and conda dependencies. Run the following command to list the available environments.
    %conda
    
    env list

    Note:

    The active environment is marked with an asterisk (*).
    The command returns the list of following environments:

    Figure 4-81 Command to list Conda environments



  2. Now, run the following command to create an environment by the name mypyenv from the conda-forge channel, install the Python package seaborn, and upload the conda environment mypyenv to Object Storage:
    %conda
    
    create -n mypyenv -c conda-forge --override-channels --strict-channel-priority python=3.13.5 seaborn
    
    upload mypyenv --overwrite -t application "OML4PY"

    Note:

    This example uses Python version 3.13.5. You must ensure compatibility between the third-party package version and the Python version that OML4Py uses.
    To determine the Python version being used, run the following command:
    import sys
    sys.version
    In this command:
    • -n: This is the name of the environment. In this example, it is mypyenv.
    • -c: This is the channel name. In this example, the conda-forgechannel is used to install the Python package.
    • --override-channels: This argument ensures that the system does not search default, and requires a channel to be mentioned.
    • --strict-channel-priority: This argument ensures that packages in lower priority channels are not considered if a package with the same name appears in a higher priority channel. In this example, the priority is given to Python 3.13.5 and seaborn.
    • upload: This argument uploads the Conda environment named mypyenv to a target location.
    • --overwrite: This argument ensures that if a file or directory with the same name already exists at the target location, it should be overwritten.
    • -t application: This argument specifies the type of the target as an application.
    • "OML4PY": This is the name or path of the target application where the environment will be uploaded.
    The command returns the following message once it creates the environment and installs the listed package. Scroll down the paragraph to view the complete details.

    Figure 4-82 Command to create a Conda environment



  3. Now, verify the environment that you created. Run the following command once again to view the list of environments.
    %conda
    
    env list

    Note:

    The mypyenv environment is not yet active. The active environment is marked with an asterisk (*), and in this list, it is present against the base environment usr.
    Note that the environment mypyenv is listed in the output.

    Figure 4-83 mypyenv environment created



  4. To activate the environment mypyenv, run the following command:
    %conda
    
    activate mypyenv

    Figure 4-84 Command to activate Conda environment



  5. Now, run the command to list the environment once again to view the list of active environments. Note that the asterisk (*) is now present against the mypyenv environment.

    Figure 4-85 Activated environment mypyenv listed



The asterisk (*) next to the environment name confirms the activation. This completes the task of creating, activating and verifying a Conda environment.

4.10.4 Create a Conda Environment for R and Install R Package and Upload it to Object Storage

This topic shows how to create a Conda environment myrenv for R packages, with R-4.0.5 for OML4R compatibility, install the forecast package, and upload the Conda environment to object storage.

Note:

You must be signed in as ADMIN user to create the Conda environment.

Note:

To avoid version conflicts, please do not install packages that are already included in OML4R. For a complete list of pre-installed packages and their versions, see About the OML4R Supporting Packages
To create a Conda environment named myrenv with R-4.0.5 for OML4R compatibility, install the package forecast, and upload the Conda environment myrenv to object storage:
  1. Run the following command to create the environment for R by the name myrenv, install the package forecast and upload the Conda environment myrenv to object storage:
    %conda
    
    create -n myrenv -c conda-forge --override-channels --strict-channel-priority r-base=4.0.5 r-forecast
    
    upload myrenv --overwrite -t application "OML4R"

    Note:

    In this example, we are using the conda-forge channel to install the R packages.
    In this command:
    • -n: This is the name of the environment. In this example, it is myrenv.
    • -c: This is the channel name. In this example, it is conda-forge.
    • --override-channels: This argument ensures that the system does not search default, and requires a channel to be mentioned.
    • --strict-channel-priority: This argument ensures that packages in lower priority channels are not considered if a package with the same name appears in a higher priority channel. In this example, the priority is given to R-4.0.5 and forecast.
    • upload: This argument uploads the Conda environment named myrenv to a target location.
    • --overwrite: This argument ensures that if a file or directory with the same name already exists at the target location, it should be overwritten.
    • -t application: This argument specifies the type of the target as an application.
    • "OML4R": This is the name or path of the target application where the environment will be uploaded.
    The command returns the following:

    Figure 4-86 Command to create Conda environment for R and install R Packages



  2. You may now verify the Conda environment for R that you just created. Run the following command in a %conda paragraph to view the list of environments.
    env list
    Note that the myrenv environment is now listed along with the mypyenv in the output.

    Figure 4-87 myrenv environment created and listed



    Note that the asterisk (*) is against the mypyenv, indicating that this environment is active currently.

4.10.5 Upload the Conda environments to an Object Storage bucket associated with the Autonomous AI Database

There is one Object Storage bucket for each data center region. The Conda environment is saved to a folder in Object Storage corresponding to the tenancy and database. The folder is managed by Autonomous AI Database and is only available to users through Oracle Machine Learning Notebooks. There is an 8G maximum size for a single Conda environment, and no size limit on Object Storage.

This topic shows how to upload a Conda environment to the Object Storage bucket associated with the Autonomous AI Database instance using the upload command. You will provide environment descriptions and tags, one for the user name and one for the application name. You will also overwrite any environment with the same name if it exists. The application tag is required for use with embedded execution. For example, OML4Py embedded Python execution works with Conda environments containing the OML4PY tag.
To upload a Conda environment to the Object Storage bucket:
  1. In a Conda paragraph in your notebook, run the following command to upload the mypyenv to the Object Storage bucket associated with the Autonomous AI Database.
    upload mypyenv --overwrite --description 'Install Python seaborn and tensorflow packages' -t user 'OMLUSER' -t application OML4PY
    In this command, you use the upload command and provide the following:
    • --description: a description of your task.
    • -t: A tag for the user OMLUSER and for the application OML4PY.

    Figure 4-88 Command to load the Conda environment for Py


    Command to load the Conda environment for Py

  2. Now, run the following command to view the list of saved environments in mypyenv:
    list-saved-envs -e mypyenv
    The command returns the following:

    Figure 4-89 View saved environment mypyenv


    View saved environment mypyenv

  3. Now upload the myrenv to the object storage by running the following command in a Conda paragraph:
    %conda
    
    upload myrenv --overwrite --description 'Install R forecast and ggplot2 packages' -t user 'OMLUSER' -t application 'OML4R
    The command returns the following:

    Figure 4-90 Command to upload Conda environment for R


    Command to upload Conda environment for R to Object Storage

  4. Again, run the following command to view the list of saved environments in myrenv:
    %conda
    
    list-saved-envs -e myrenv
    The command returns the following:

    Figure 4-91 View saved environment myrenv


    View saved environment myrenv

This completes the task of uploading the Python and R environments mypyenv and myrenv to the Object Storage associated with the Autonomous AI Database. The environments are now available for you to download and use. The environments will remain in the Object Storage until it is deleted.