All Products
Search
Document Center

E-MapReduce:Use third-party Python libraries in a notebook

Last Updated:Jun 20, 2026

When you run interactive PySpark jobs in a notebook, you can use third-party Python libraries to enhance data processing and analysis. This topic describes three methods for installing these libraries.

Background information

During interactive PySpark development, you can use third-party Python libraries for more flexible and user-friendly data processing and analysis. This topic describes three available methods. Choose the one that best suits your needs.

Method

Use cases

Method 1: Install Python libraries using pip

Processing variables unrelated to Spark in a notebook, such as return values from Spark computations or custom variables.

Important

Libraries installed with this method do not persist across sessions and must be reinstalled after each restart.

Method 2: Configure a custom Python environment using Environments

For PySpark jobs that require third-party libraries to be pre-installed in each notebook session.

Method 3: Configure a custom Python environment using Spark parameters

For distributed PySpark computing where third-party libraries must be available to all executors.

Prerequisites

Procedure

Method 1: Install libraries using pip

  1. Go to the notebook development page.

    1. Log in to the E-MapReduce console.

    2. In the left navigation pane, choose EMR Serverless > Spark.

    3. On the Spark page, click the name of your target workspace.

    4. On the EMR Serverless Spark page, click Development in the left navigation pane.

    5. Double-click the notebook that you created.

  2. In a Python cell of the notebook, enter the following command to install the scikit-learn library, and then click the Run Cell icon image.

    pip install scikit-learn
  3. In another Python cell, enter the following code, and then click the Run Cell icon image.

    # Import the library and prepare the dataset.
    from sklearn import datasets
    # Load a built-in dataset, such as the Iris dataset.
    iris = datasets.load_iris()
    X = iris.data  # Feature data
    y = iris.target  # Label
    # Split the dataset.
    from sklearn.model_selection import train_test_split
    # Split the data into training and test sets.
    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)
    # Train a support vector machine (SVM) model.
    from sklearn.svm import SVC
    # Create a classifier instance.
    clf = SVC(kernel='linear')  # Use a linear kernel.
    # Train the model.
    clf.fit(X_train, y_train)
    # Make predictions with the trained model.
    y_pred = clf.predict(X_test)
    # Evaluate model performance.
    from sklearn.metrics import classification_report, accuracy_score
    print(classification_report(y_test, y_pred))
    print("Accuracy:", accuracy_score(y_test, y_pred))
    

    The output is similar to the following:

                  precision    recall  f1-score   support
               0       1.00      1.00      1.00        19
               1       1.00      1.00      1.00        13
               2       1.00      1.00      1.00        13
        accuracy                           1.00        45
       macro avg       1.00      1.00      1.00        45
    weighted avg       1.00      1.00      1.00        45
    Accuracy: 1.0

Method 2: Configure an environment with Environments

Step 1: Create a runtime environment

  1. Go to the Environments page.

    1. Log in to the E-MapReduce console.

    2. In the left navigation pane, choose EMR Serverless > Spark.

    3. On the Spark page, click the name of your target workspace.

    4. On the EMR Serverless Spark page, click Environment in the left navigation pane.

  2. Click Create Environment.

  3. On the Create Environment page, click Add Library.

    For parameter details, see Manage runtime environments.

  4. In the New Library dialog box, set Source Type to PyPI, specify the PyPI Package, and then click OK.

    In the PyPI Package field, enter the name and version of the library. If you do not specify a version, the system installs the latest version. Example: scikit-learn.

  5. Click create.

    After the environment is created, the system starts to initialize it.

Step 2: Use the runtime environment

Note

Stop the session before you edit it.

  1. Go to the Notebook Session tab.

    1. On the EMR Serverless Spark page, choose O&M Center > Sessions in the left navigation pane.

    2. Click the Notebook Session tab.

  2. In the Actions column of the target notebook session, click Edit.

  3. From the Environment drop-down list, select the runtime environment that you created in the preceding step and click Save Changes.

  4. In the upper-right corner, click START.

Step 3: Classify data with Scikit-learn

  1. Go to the notebook development page.

    1. On the EMR Serverless Spark page, click Development in the left navigation pane.

    2. Double-click the notebook that you created.

  2. In a Python cell of the notebook, enter the following code, and then click the Run Cell icon image.

    # Import the library and prepare the dataset.
    from sklearn import datasets
    # Load a built-in dataset, such as the Iris dataset.
    iris = datasets.load_iris()
    X = iris.data  # Feature data
    y = iris.target  # Label
    # Split the dataset.
    from sklearn.model_selection import train_test_split
    # Split the data into training and test sets.
    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)
    # Train a support vector machine (SVM) model.
    from sklearn.svm import SVC
    # Create a classifier instance.
    clf = SVC(kernel='linear')  # Use a linear kernel.
    # Train the model.
    clf.fit(X_train, y_train)
    # Make predictions with the trained model.
    y_pred = clf.predict(X_test)
    # Evaluate model performance.
    from sklearn.metrics import classification_report, accuracy_score
    print(classification_report(y_test, y_pred))
    print("Accuracy:", accuracy_score(y_test, y_pred))
                  

Method 3: Configure an environment with Spark parameters

This method requires ipykernel 6.29 or later, jupyter_client 8.6 or later, and Python 3.8 or later. Additionally, you must package the environment on a Linux system with an x86 architecture.

Step 1: Build and deploy a conda environment

  1. Run the following commands to install Miniconda:

    wget https://repo.continuum.io/miniconda/Miniconda3-latest-Linux-x86_64.sh
    chmod +x Miniconda3-latest-Linux-x86_64.sh
    ./Miniconda3-latest-Linux-x86_64.sh -b
    source miniconda3/bin/activate
  2. Build a conda environment that uses Python 3.8 and numpy.

    # Create and activate the conda environment.
    conda create -y -n pyspark_conda_env python=3.8
    conda activate pyspark_conda_env
    # Install third-party libraries.
    pip install numpy \
    ipykernel~=6.29 \
    jupyter_client~=8.6 \
    jieba \
    conda-pack
    # Pack the environment.
    conda pack -f -o pyspark_conda_env.tar.gz

Step 2: Upload resource file to OSS

Upload the packaged pyspark_conda_env.tar.gz to Alibaba Cloud OSS and record the complete OSS path. For more information, see Simple Upload.

Step 3: Configure and start the notebook session

Note

Stop the session before you edit it.

  1. Go to the Notebook Session tab.

    1. On the EMR Serverless Spark page, choose O&M Center > Sessions in the left navigation pane.

    2. Click the Notebook Session tab.

  2. In the Actions column of the target notebook session, click Edit.

  3. In the Spark Configuration section, add the following configurations and click Save Changes.

    spark.archives  oss://<yourBucket>/path/to/pyspark_conda_env.tar.gz#env
    spark.pyspark.python ./env/bin/python
    Note

    In the configuration, replace <yourBucket>/path/to with your actual OSS upload path.

  4. In the upper-right corner, click START.

Step 4: Process text data with Jieba

Note

Jieba is a third-party Python library for Chinese text segmentation. For information about its open-source license, see LICENSE.

  1. Go to the notebook development page.

    1. On the EMR Serverless Spark page, click Development in the left navigation pane.

    2. Double-click the notebook that you created.

  2. In a new Python cell, enter the following command to perform Chinese word segmentation using Jieba, and then click the Run Cell icon image.

    import jieba
    strs = ["EMRServerlessSpark是一款专为大规模数据处理与分析而打造的全托管Serverless产品", "为您提供了高效的任务开发调试调度以及运维等一站式服务", "EMRServerlessSpark实现了资源调度和根据任务负载进行动态扩展"]
    sc.parallelize(strs).flatMap(lambda s: jieba.cut(s, use_paddle=True)).collect()

    The command returns the following result:

    import jieba
    strs = ["EMRServerlessSpark是一款专为大规模数据处理与分析而打造的全托管Serverless产品", "为您提供了高效的任务开发调试调度以及运维等一站式服务", "EMRServerlessSpark实现了资源调度和根据任务..."]
    sc.parallelize(strs).flatMap(lambda s: jieba.cut(s, use_paddle=True)).collect()
    ['EMRServerlessSpark',
     '是',
     '一款',
     '专',
     '为',
     '大规模',
     '数据处理',
     '与',
     '分析',
     '而',
     '打造',
     '的',
     '全',
     '托管',
     'Serverless',
     '产品',
     '为',
     '您',
     '提供',
     '了',
     '高效',
     '的',
     '任务',
     '开发',
     '调试',
     '调度',
     '以及',
     '运维',
     '等',
     '一站式',
     '服务',
     'EMRServerlessSpark',
     '实现',
     '了',
     '资源',
     '调度',
     '和',
     '根据',
     '任务',
     '负载',
     '进行',
     '动态',
     '扩展']