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

Preparations

  1. Create a code directory for testing. In this example, mycode is used.
    • Linux or 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 file consists of the following 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. Compress all files in the mycode directory.
    • Linux or 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 package.

  3. In the Function Compute console, find the desired function. In the upper-right corner of the function details page, click Upload Code to upload the ZIP package obtained in the previous step.
Important Function Compute runs in Linux. If binary files are contained when you install the emoji dependency library on your on-premises machine that runs Windows or macOS, the function fails to be executed after you upload the code package to Function Compute. We recommend that you use WebIDE or Serverless Devs to install the emoji dependency library. For more information, see Use WebIDE to package third-party dependencies of a function and Use Serverless Devs to install dependencies and deploy projects.

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. Add the s.yaml file.
    The following sample code provides an example on how to edit the file:
    edition: 1.0.0          # The version of the YAML specification. The version complies with the semantic versioning specification.
    name: fcDeployApp       # The name of the project.
    access: "default"       # The alias of the key.
    
    services:
      fc-deploy-test: # The name of the service.
        component: fc  # The name of the component.
        props: # The property value of the component.
          region: cn-shanghai
          service:
            name: fctest
            description: 'test'
            internetAccess: true
          function:
            name: emoji
            description: this is a emoji
            runtime: python3
            codeUri: ./
            handler: index.handler
            memorySize: 128
            timeout: 6
  3. Add the requirements.txt file.
    Edit the file according to the following content:
    emoji==2.0.0
  4. Run the s build --use-docker command to install dependencies.
    After the execution, the .s directory is generated in the mycode directory, and the dependencies are installed in the .s/build/artifacts/{serviceName}/{functionName} directory.
  5. Run the s deploy command to deploy the project.
    After you run the command, you can deploy a function to Function Compute.

References

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