Install third-party dependencies for your Python function, package the code, and deploy it to Function Compute. This guide uses the emoji library as a working example.
Choose a deployment method
| Method | Best for | Requires |
|---|---|---|
| pip + console upload | Pure-Python dependencies (no compiled binaries) | Python 3, pip3 |
| Serverless Devs + Docker | Dependencies with binary extensions (C/C++, cryptography, numpy, etc.) | Serverless Devs CLI, Docker |
If your dependencies include compiled binary files, use the Serverless Devs method. Binary files built on macOS or Windows are incompatible with the Linux environment that Function Compute runs in.
Preparations
Set up a local code directory and a sample function before following either method.
Create a directory named
mycode.Linux and macOS:
mkdir -p /tmp/mycodeWindows: Create a folder named
mycode
In the
mycodedirectory, createindex.pywith the following content:from emoji import emojize def handler(event, context): return emojize(":thumbs_up:")
Method 1: Use pip and the console
Prerequisites
Before you begin, make sure you have:
Python 3 installed on your local machine, with permission to run
pip3A Python function created in the Function Compute console. For setup instructions, see Create an event function
Install dependencies and upload
Install the
emojidependency into themycodedirectory:pip3 install emoji -t .Run this command from inside the
mycodedirectory. The-t .flag installs the package into the current directory instead of the global site-packages.Package all files in
mycodeinto a ZIP file.Linux and macOS: Go to the
mycodedirectory and run:zip code.zip -r ./*NoteNote: Make sure you have read and write permissions on the directory.
Windows: Select all files in the
mycodedirectory, right-click, and compress them into a.zipfile.
Upload the ZIP file in the Function Compute console:
Locate the target function and go to its Function Details page.
Click the Code tab.
Click Upload Code > Upload ZIP Package, then select
code.zip.Click Save and Deploy.
Function Compute runs in a Linux environment. If the emoji library (or any other dependency) contains compiled binary files, the package may fail to run when installed on macOS or Windows. In that case, we recommend that you use WebIDE to package third-party dependencies or use Method 2 (Serverless Devs + Docker).
Method 2: Use Serverless Devs and Docker
Serverless Devs builds dependencies inside a Docker container that matches the Function Compute Linux runtime, ensuring binary compatibility.
Prerequisites
Before you begin, make sure you have:
Build and deploy
Go to the
mycodedirectory:cd /tmp/mycodeCreate
s.yamlwith the following content: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.The following table describes the key files in this project:
File Purpose Required s.yamlServerless Devs project configuration Yes index.pyFunction handler code Yes requirements.txtPython dependencies to install Yes Create
requirements.txtwith the following content:emoji==2.0.0Build the dependencies inside a Docker container:
sudo s build --use-dockerAfter the build completes, dependencies are installed to the
./pythondirectory.Deploy the function:
sudo s deployAfter deployment, the function is available in Function Compute.
What's next
Use Function Compute layers to share dependencies across multiple functions without bundling them in each code package: