OpenFaaS: How to Add Python Requirements and Dependencies

This guide will show you how to add requirements and dependencies for a Python project using OpenFaaS.

Python dependencies are software components that your project needs for it to work. You can manually use PyPI (the Python Package Index) to provide packages that you need, but OpenFaaS can automate this for you.

OpenFaaS: Serverless platform with no strings attached? - Andreas Mosti - NDC Oslo 2020

Let’s Get Started!

First, let’s use the faas-cli new command to create a new python3 function from the templates.

See below:

faas-cli new – lang python3 my-function


2021/10/31 00:10:12 No templates found in current directory.
2021/10/31 00:10:12 Attempting to expand templates from
2021/10/31 00:10:13 Fetched 14 template(s) : [csharp dockerfile go java11 java11-vert-x node node12 node12-debian node14 php7 python python3 python3-debian ruby] from
Folder: my-function created.
  ___                   _____           ____
 / _ \ _ __   ___ _ __ |  ___|_ _  __ _/ ___|
| | | | '_ \ / _ \ '_ \| |_ / _` |/ _` \___ \
| |_| | |_) |  __/ | | |  _| (_| | (_| |___) |
 \___/| .__/ \___|_| |_|_|  \__,_|\__,_|____/

Function created in folder: my-function
Stack file written: my-function.yml

You have created a Python3 function using the Classic Watchdog.

To include third-party dependencies create a requirements.txt file.

For high-throughput applications, we recommend using the python3-flask
or python3-http templates.

This command will then create a few files in the folder in which you ran the command.

See below:

├── my-function
│   ├──
│   ├──
│   └── requirements.txt
├── my-function.yml
└── template

This guide will focus on the requirements.txt and files. You use the requirements.txt file to set up the python-pip modules that you need. And the file has the code you are writing.

Add Your OpenFaaS Python Requirements

This guide will show you how to add the “scanless” module for Python. You can, of course, add any module that you need. This module is a Python 3 command-line utility for using websites to perform port scans on your behalf.

To add the logging module, add the following line to your requirements.txt file:


Next, edit your file and replace the contents with the following:

import scanless
import logging

def handle(req):
    logging.warning(' – Start of Code – ')

    sl = scanless.Scanless()
    output = sl.scan('', scanner='hackertarget')

    return output['raw']

The first import is “scanless”, and the faas-cli will install to your image once you start the build.

The second import is “logging”, and you did not add it to the requirements.txt. And you don’t need to since it’s part of the Python standard library. Therefore, it is available as soon as you install Python.

Now you must build, push and deploy your function using the faas-cli up command:

faas-cli up -f my-function.yml

Finally, you can run your function using the following command:


You should see the following result with the warning message as well as the port scan results:

WARNING:root: – Start of Code – 
Starting Nmap 7.40 ( ) at 2021-11-05 07:37 UTC
Nmap scan report for (
Host is up (0.069s latency).
Other addresses for (not scanned): 2600:3c01::f03c:91ff:fe18:bb2f
21/tcp   closed ftp
22/tcp   open   ssh
23/tcp   closed telnet
80/tcp   open   http
110/tcp  closed pop3
143/tcp  closed imap
443/tcp  closed https
3389/tcp closed ms-wbt-server

Nmap done: 1 IP address (1 host up) scanned in 0.30 seconds

Wrapping Up

As you can see, it is easy to add your Python dependencies to an OpenFaaS Project. In addition, Python provides some of the dependencies out-of-the-box by Python.

You May also be Interested in


About the Authors

Anto's editorial team loves the cloud as much as you! Each member of Anto's editorial team is a Cloud expert in their own right. Anto Online takes great pride in helping fellow Cloud enthusiasts. Let us know if you have an excellent idea for the next topic! Contact Anto Online if you want to contribute.

Support the Cause

Support Anto Online and buy us a coffee. Anything is possible with coffee and code.

Buy me a coffee

About Anto Online

Having started his career in 1999 as a Desktop Support Engineer, Anto soon changed paths and became a developer. After several years of development experience, he transitioned into a consultant. As an enterprise application consultant for a leading SaaS software provider, Anto specializes in AWS's serverless technologies. By day, Anto focuses on helping customers leverage the power of serverless technologies. By night, he indulges his passion for cloud computing by playing with Python and trying out things that are currently beyond the scope of his work. Sometimes Anto needs help as there are not enough hours at night. So Anto relies on a team of fellow Cloud enthusiasts to help him out. Each one is a Cloud expert in their own right, and Anto takes great pride in helping them learn and grow.

View all posts by Anto Online →

Leave a Reply

Your email address will not be published.