WebIDE can provide a consistent environment between the terminal and the execution environment of Function Compute. You can use a WebIDE instance terminal to package third-party dependencies. The packaging is the same as the packaging in a runtime environment of Function Compute. This topic describes how to package third-party dependencies for Python, Node.js, and PHP runtimes and custom runtimes by using a WebIDE terminal.

Python

Specific third-party libraries are built in Python runtime environments. For more information, see Built-in python modules. If you need to install other third-party libraries, run the following command in the /code directory of the WebIDE terminal:

pip install -t .  flask
# or
pip install -t . -r requirements.txt

If you use the preceding method, your code directory may become cluttered and you may not be able to easily find the index.py file in the resource manager. You can define a directory, such as /python, and install third-party dependencies to the directory. To allow the function code to import the dependencies, you must add the PYTHONPATH=/code/python environment variable for the function. Run the following command:

mkdir python
cd python
pip install -t . flask
If the size of the third-party dependency package that you want to install is large and it takes a long time to deploy, you can package the /python directory as a layer, and then reference the layer in the function. After that, delete the /python directory in the code directory. Run the following command:
#1. Publish the python directory as a layer.
zip -ry python.zip python
s cli fc layer publish --layer-name myPythonLibLayer --code /code/python.zip --compatible-runtime python3.10,python3.9,python3.6,custom, custom.debian10 --region cn-hangzhou -a default
...
395da10bf789aa49dd035db01bab****#myPythonLibLayer#1

#2. Update the function and reference the layer in the function.

#3. Delete the python directory to reduce the size of the function code package.
rm -rf python python.zip

#4. Deploy the updated function.
Note We recommend that you use the /python directory for a Python runtime environment. You do not need to configure the PYTHONPATH=/opt/python environment variable to allow third-party packages to be imported into the function.

For more information about how to create and reference custom layers, see Create a custom layer and Manage layers.

Node.js

Specific third-party libraries are built in Node.js runtime environments. For more information, see Node.js built-in modules. If you need to install other third-party libraries, run the following command in the /code directory of the WebIDE terminal:

# The package.json file already exists.
npm install
If the size of the third-party dependency package that you want to install is large and it takes a long time to deploy the function code, you can customize the /node_modules directory, package the directory as a layer, and then reference the layer in the function. After that, delete the /node_modules directory in the code directory. Sample code:
#1. Publish the node_modules directory as a layer.
mkdir nodejs
mv node_modules ./nodejs
zip -ry nodejs.zip nodejs
s cli fc layer publish --layer-name myNodeLibLayer --code /code/nodejs.zip --compatible-runtime nodejs16,nodejs14,nodejs12,nodejs10,custom,custom.debian10 --region cn-hangzhou -a default
...
395da10bf789aa49dd035db01bab****#myNodeLibLayer#1

#2. Update the function and reference the layer in the function.

#3. Delete the node_modules directory to reduce the size of the function code package.
rm -rf nodejs nodejs.zip

#4. Deploy the updated function.

For more information about how to create and reference custom layers, see Create a custom layer and Manage layers.

PHP

For PHP, the method to install a third-party dependency package is the same as that in Python and Node.js. The difference is that the directory that is used to build layers in a PHP runtime environment is /opt/php. For more information, see the "Directories that can be added in each runtime" section in Create a custom layer.

Custom Runtime

If you use a custom runtime and the language is Python, Node.js, or PHP, you can use the same method to package third-party dependencies.

If you use a custom runtime and the language is a mainstream compiled language, such as Java or Golang, you must use the SDK provided by WebIDE to perform compilation and packaging.