Python

Prev Next

Available in Classic and VPC

This document describes how to create and use python actions in a variety of ways, and includes application examples.

Create action

A code written in python may contain multiple functions, but the main function must be declared as the starting point of a program. With this taken into account, the following is a simple example code hello in Python, printing Hello World along with a name and location.

def main(args):
    name = args.get("name", "World")
    place = args.get("place", "Naver")
    greeting = "Hello " + name + " in " + place + "!"
    print(greeting)
    return {"payload": greeting}

The following is the process of an action called "hello" in the console, using the code written above.

cloudfunctions-example-python_v2_01_ko

Create action by packaging with dependency files

When writing codes, there are cases where dependency modules need to be packaged along with one action file. In this case, you can package related files into a single module and create an action using the packaged file.
The main function, which is the entry point to access the function, must be defined within the __main__.py file. For example, to create an action by packaging together with the helper.py file where functions used by the main action are written, use the following command to compress the action-related files to helloPython.zip.

zip -r helloPython.zip __main__.py helper.py

You can then use the compressed file created to create the action.

Create action by packaging with the virtual environment

How to create actions by packaging with the virtual environment is explained for each OS environment.

Linux & Mac

To use additional libraries other than libraries provided by Cloud Functions by default, you can use the virtual environment (virtualenv) and package them together. The main function must be defined in the __main__.py file in the same way as the packaging method supported by default, and virtualenv's folder name must be set as "virtualenv".
The following is an example of building a virtual environment, and creating an action returning joke using the pyjokes library which is not provided by Cloud Functions.

  1. Build a virtualenv environment with the name virtualenv.

    virtualenv virtualenv
    source virtualenv/bin/activate
    pip install pyjokes
    
  2. Write code that calls the get_joke() method from __main__.py to return the joke.

    import pyjokes
    
    def main(args):
        return {"joke": pyjokes.get_joke()}
    
  3. Compress the vendor folder and the __main__.py file together.

    $ zip -r jokes.zip virtualenv __main__.py
    
  4. Use the compressed file created to create an action.

Note

The maximum size of an action source code of Cloud Functions is limited to 38 MB. Accordingly, deployment may be restricted if the file size exceeds the limited size due to the large number of libraries used. For the actions not frequently used, their container status may be switched to cold, leading to the increased initial action runtime.

Windows

Since in a Windows environment, it's difficult to support it in the same format as virtualenv in Linux or Mac environments, two packaging methods by building a virtual environment are explained. However, Python or pip must be installed in advance.

  • Packaging method using Docker: if the development environment allows the use of Docker, then you can create a package by simply configuring a development environment virtually. The packaging sequence is as follows:
    1. Install the Docker for Windows(Community Edition) or the Docker Toolbox falling under for the operating system used.
      (See: https://www.docker.com/docker-windows)
    2. Write the source code __main__.py.
      import pyjokes
      
      def main(args):
          return {"joke": pyjokes.get_joke()}
      
    3. Write the requirements.txt file.
      pyjokes==0.5.0
      
    4. Use the Docker command to create a ZIP file.
      • The working path is assumed to be c:\WorkingDir in the following file structure.
        powershell
        c:\WorkingDir>dir
        XXXX-XX-XX 09:07 PM

        .
        XXXX-XX-XX 09:07 PM ..
        XXXX-XX-XX 09:07 PM 200 main.py
        XXXX-XX-XX 09:07 PM 100 requirements.txt

        
        
      • Running the Docker command: it includes the process of creating a virtual development environment using docker, compressing the environment, and then creating it as a ZIP file.

        c:\WorkingDir> docker run --rm -v "\\C:\WorkingDir:/tmp" python:3.7.4-slim-buster /bin/sh -c 'mkdir workdir && cp -R /tmp/* /workdir/ && rm -rf /workdir/virtualenv && pip install virtualenv && apt -y update && apt -y install zip && cd /workdir && virtualenv virtualenv && . virtualenv/bin/activate && pip install -r requirements.txt && zip -r /tmp/project.zip ./*'
        
    5. Upload the compressed file and create an action.
Note

requirements.txt can be written manually by seeing https://pip.pypa.io/en/stable/, if you know the version of the dependency library or how to write it. If you don't know how to write it, you can install virtualenv for Windows, and write it using pip freeze.

c:\WorkingDir>pip install virtualenv
c:\WorkingDir>virtualenv virtualenv
c:\WorkingDir>c:\WorkingDir\virtualenv\Scripts\activate
(virtualenv) c:\WorkingDir>pip freeze > requirements.txt
  • Packaging method for dependency libraries only: if you have difficulty building an additional environment such as Docker, then you can separate your own code and dependency libraries from python virtualenv for Windows, and compress them to a package. The packaging sequence is as follows:
    1. Write the source code __main__.py.
      import pyjokes
      
      def main(args):
          return {"joke": pyjokes.get_joke()}
      
    2. Create virtualenv, proceed with installation of the dependency package, and copy it.
      c:\WorkingDir>pip install virtualenv
      c:\WorkingDir>virtualenv virtualenv
      c:\WorkingDir>c:\WorkingDir\virtualenv\Scripts\activate
      (virtualenv) c:\WorkingDir>pip install pyjokes
      (virtualenv) c:\WorkingDir>xcopy .\virtualenv\Lib\site-packages\*.* .\ /e /h /k
      
    3. Compress all the files to a ZIP file, except the virtualenv folder under c:\WorkingDir of the working path.
      compute-15-2-202_ko
    4. Upload the compressed file and create an action.

Create action by packaging with the requirements.txt

You can create actions by packaging __main__.py along with requirements.txt with the required dependency libraries clarified needed for running the actions without packaging by directly configuring the virtual environment.
For actions created by such a method, when the container is run in a cold status, a virtual environment is configured and then dependency libraries are installed.

Note
  • Available only in the runtime of the python:3.11 version or later.
  • The file name of the required dependency library list packaged must be requirements.txt.
  • Internet communication is required to install dependency libraries. For Internet communication in the Private Subnet integrated with actions, NAT Gateway settings are required. For how to set, see NAT Gateway user guide.
  1. Write the dependency library list with the name of requirements.txt.
    pyjokes==0.6.0
    
  2. Write code that calls the get_joke() method from __main__.py to return the joke.
    import pyjokes
    
    def main(args):
        return {"joke": pyjokes.get_joke()}
    
  3. Compress the requirements.txtx and the __main__.py file together.
    $ zip -r jokes-with-dependencies.zip requirements.txt __main__.py
    
  4. Use the compressed file created to create an action.
Caution
  • The process of configuring virtual environment in the action container and installing required dependency libraries requires a great use of memory. Accordingly, the action can fail to run if there are too many libraries to install or the capacity is large because the memory limits of the action container can be exceeded.
  • In addition, the action may fail to run since the installation of specific dependency in the virtual environment configured in the container is limited because the required pip version for installation is not proper or because of similar reasons.
  • If required dependency library installation is limited due to memory limits or pip versions, use the method to create actions by configuring and packaging the virtual environment directly.