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
Create a code directory for tests and specify a name for the directory. In this example,
mycodeis used.Linux and macOS
Run the
mkdir -p /tmp/mycodecommand to create the directory.Windows
Create a folder and name it
mycode.
In the
mycodedirectory, create theindex.pyfile.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
Python 3 is installed on your on-premises machine, and you are granted the required permissions on pip3.
A Python function is created in the Function Compute console. For more information, see Create an event function.
Procedure
Run the
pip3 install emoji -t .command in themycodedirectory to install the emoji dependency library to the current directory.Package all files in the
mycodedirectory.Linux and macOS
Go to the
mycodedirectory and run thezip code.zip -r ./*command.NoteMake sure that you have the read and write permissions on the directory.
Windows
Go to the
mycodedirectory, select all files, right-click the files, and then compress the files into a .zip file.
In the Function Compute console, locate the target function and navigate to the Code tab on its Function Details page.
Click , and then select and upload the ZIP package that you prepared earlier before clicking Save and Deploy.
Function Compute runs in a Linux environment. If a binary file is included when you install the emoji dependency library on a Windows or macOS device, the code package fails to run. Therefore, we recommend that you use WebIDE to package third-party dependencies or use Serverless Devs to install dependencies and deploy projects.
Use Serverless Devs to install dependencies and deploy projects
Before you start
Procedure
Run the
cd /tmp/mycodecommand to go to themycodedirectory.Create the
s.yamlfile.The following sample code provides an example of the file:
edition: 3.0.0 name: fcDeployApp access: "default" vars: # The global variables. region: "cn-hangzhou" resources: hello_world: component: fc3 # The name of the component. 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.code.Add the
requirements.txtfile.Edit the file based on the following code snippet:
emoji==2.0.0Run
sudo s build --use-dockerto install dependencies.After the execution is complete, the dependencies are installed to the
./pythondirectory.Run
sudo s deployto 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: