All Products
Search
Document Center

Function Compute:Deploy a code package

Last Updated:Jul 11, 2025

This topic describes how to install dependencies for your Python code, and how to package and deploy code in Function Compute. In this topic, the third-party dependency emoji is used as an example.

Preparations

  1. Create a code directory for tests and specify a name for the directory. In this example, mycode is used.

    • Linux and macOS

      Run the mkdir -p /tmp/mycode command to create the directory.

    • Windows

      Create a folder and name it mycode.

  2. In the mycode directory, create the index.py file.

    The following code describes the file content.

    from emoji import emojize
    def handler(event, context):
        return emojize(":thumbs_up:")

Use pip to install dependencies and deploy code

Prerequisites

Procedure

  1. Run the pip3 install emoji -t . command in the mycode directory to install the emoji dependency library to the current directory.

  2. Package all files in the mycode directory.

    • Linux and macOS

      Go to the mycode directory and run the zip code.zip -r ./* command.

      Note

      Make sure that you have the read and write permissions on the directory.

    • Windows

      Go to the mycode directory, select all files, right-click the files, and then compress the files into a .zip file.

  3. In the Function Compute console, find the function that you want to manage. On the Code tab of the function details page, click Upload Code > Upload ZIP Package in the upper-right corner. In the dialog box that appears, upload the .zip file that you prepared, and then click Save And Deploy.

Important

Because Function Compute runs in a Linux environment, if you install the emoji dependency library that contains a binary file on a Windows or macOS system, your code package will fail to run after it is uploaded to Function Compute. Therefore, we recommend that you use WebIDE to package third-party dependencies for the function or use Serverless Devs to install dependencies and deploy the project.

Use Serverless Devs to install dependencies and deploy projects

Prerequisites

Procedure

  1. Run the cd /tmp/mycode command to go to the mycode directory.

  2. Create the s.yaml file.

    The following sample code provides an example of the file:

    edition: 3.0.0
    name: fcDeployApp
    access: "default"
    
    vars: # Global variables
      region: "cn-hangzhou"
    
    resources:
      hello_world:
        component: fc3 # The component name
        props:
          region: ${vars.region}             
          functionName: "emojipy"
          description: 'this is emoji'
          runtime: "python3"
          code: ./
          handler: index.handler
          memorySize: 128
          timeout: 30
          environmentVariables:
            PYTHONUSERBASE: /code/python         # Add environment variables to obtain dependencies
  3. Add the requirements.txt file.

    Edit the file based on the following code snippet:

    emoji==2.0.0
  4. Run sudo s build --use-docker to install dependencies.

    After the execution is complete, the dependencies are installed to the ./python directory.

  5. Run sudo s deploy to deploy the project.

    After the execution, you can deploy your function to Function Compute.

More information

You can also use layers of Function Compute to install dependencies. We recommend that you use a public layer or build a dependency layer online. For more information, see the following topics:

close