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
- 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
.
- Linux or macOS
- In the
mycode
directory, create theindex.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
- Python 3 is installed on your on-premises device, and you have the required permissions on pip3.
- A Python function is created in the Function Compute console. For more information, see Create a function.
Procedure
- Run the
pip3 install emoji -t
command in themycode
directory to install the emoji dependency library to the current directory. - Compress all files in the
mycode
directory.- Linux or macOS
Go to the
mycode
directory and run thezip 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.
- Linux or macOS
- 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
- Run the
cd /tmp/mycode
command to go to themycode
directory. - 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
- Add the
requirements.txt
file.Edit the file according to the following content:emoji==2.0.0
- Run the
s build --use-docker
command to install dependencies.After the execution, the.s
directory is generated in themycode
directory, and the dependencies are installed in the.s/build/artifacts/{serviceName}/{functionName}
directory. - 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: