/tmp
directory, and other directories are read-only. Therefore, you need to install, package and upload the local dependent library for use. The Python dependency package can be uploaded with function codes to the cloud, or uploaded to the layer that will be bound to the required function.pip
with pip3
or pip2
according to the environment configurations.requirements.txt
.pip install -r requirements.txt -t .
command under the code directory to install the dependency package. You can use the -t
parameter to specify the installation directory, or directly run -t .
under the project’s code directory to install the dependency package in the current directory.pip freeze > requirements.txt
command to generate a requirements.txt
file that contains all dependencies of the current environment.index.py
code file shown below to install the requests
dependency locally.# -*- coding: utf8 -*-import requestsdef main_handler(event, context):addr = "www.qq.com"resp = requests.get(addr)print(resp)return resp
pip3 install requests -t .
command to install the requests
dependency under the current directory of the project. The code file is as follows:$ pip3 install requests -t .Collecting requestsUsing cached requests-2.25.1-py2.py3-none-any.whl (61 kB)Collecting certifi>=2017.4.17Using cached certifi-2020.12.5-py2.py3-none-any.whl (147 kB)Collecting chardet<5,>=3.0.2Using cached chardet-4.0.0-py2.py3-none-any.whl (178 kB)Collecting idna<3,>=2.5Using cached idna-2.10-py2.py3-none-any.whl (58 kB)Collecting urllib3<1.27,>=1.21.1Using cached urllib3-1.26.4-py2.py3-none-any.whl (153 kB)Installing collected packages: urllib3, idna, chardet, certifi, requestsSuccessfully installed certifi-2020.12.5 chardet-4.0.0 idna-2.10 requests-2.25.1 urllib3-1.26.4$ ls -ltotal 8drwxr-xr-x 3 xxx 111 96 4 29 16:45 bindrwxr-xr-x 7 xxx 111 224 4 29 16:45 certifidrwxr-xr-x 8 xxx 111 256 4 29 16:45 certifi-2020.12.5.dist-infodrwxr-xr-x 44 xxx 111 1408 4 29 16:45 chardetdrwxr-xr-x 9 xxx 111 288 4 29 16:45 chardet-4.0.0.dist-infodrwxr-xr-x 11 xxx 111 352 4 29 16:45 idnadrwxr-xr-x 8 xxx 111 256 4 29 16:45 idna-2.10.dist-info-rw-r--r--@ 1 xxx 111 177 4 29 16:33 index.pydrwxr-xr-x 21 xxx 111 672 4 29 16:45 requestsdrwxr-xr-x 9 xxx 111 288 4 29 16:45 requests-2.25.1.dist-infodrwxr-xr-x 17 xxx 111 544 4 29 16:45 urllib3drwxr-xr-x 10 xxx 111 320 4 29 16:45 urllib3-1.26.4.dist-info
import
statement in function codes. You can also package and deploy dependencies to a layer, and bind the layer to a function being created to reuse them.pycryptodome
dependency need to be compiled for installation. Because the compilation varies with the operating system, the dependent library, dynamic library, and other programs compiled on Windows or Mac may be unable to run in the SCF environment. The following solutions are recommended.
Was this page helpful?