Creating Python Code-only Functions
Find out how to package and create Python code-only functions in OCI Functions.
Use a Python code-only function when you want to deploy Python source code and dependencies without building a container image.
Python source code must be packaged under a root-level function/ directory.
Before creating a Python code-only function, package the function code and any dependencies using the archive layout required for Python code-only functions. For general code-only function prerequisites, see Creating Code-only Functions. For supported Python runtime versions and runtime names, see Languages supported for code-only functions.
In addition, for packaging and creating Python code-only functions, you need:
- A Python development environment.
- A Python function module and handler function.
- Any required Python dependencies installed into a package directory.
- A valid Python handler value.
Python archive requirements
For Python code-only functions:
- The archive must contain a root-level
function/directory. - The
function/directory contains the handler module and application source code. - The root-level
python/directory is optional and contains Python dependencies. - The root-level
resources/directory is optional and contains runtime resources. - The root-level
native/directory is optional only for multi-architecture native dependency use cases. - For single-architecture Python functions, root-level
native/is not allowed. - For multi-architecture Python functions, if
native/exists, place architecture-specific native dependency files only undernative/fn-arch-x86/andnative/fn-arch-arm/. - A handler is required.
- The handler must use the format
<file>.<function>. - The handler must refer to a Python function in the
function/directory.
Python handler
The handler identifies the Python function to invoke. The handler must refer to a Python function in a Python file in the function/ directory.
Use the format:
<file>.<function>
For example, if the archive contains function/func.py and the handler function is named handler, use:
func.handler
Single-architecture Python archive
archive-root/
├── function/
└── func.py
├── python/ # optional
└── resources/ # optional
Rules:
function/is required.python/is optional.resources/is optional.native/is not allowed for single-architecture Python functions.- The handler must refer to a Python function in the
function/directory.
Package Python source without dependencies
mkdir -p build/archive/function
cp func.py build/archive/function/
cd build/archive
zip -r ../function.zip function/
Handler:
func.handler
Package Python source with dependencies
Install dependencies into the python/ directory before creating the ZIP.
mkdir -p build/archive/function
mkdir -p build/archive/python
cp func.py build/archive/function/
python3 -m pip install -r requirements.txt -t build/archive/python
cd build/archive
zip -r ../function.zip function/ python/
If using resources, include resources/ at the archive root.
Dependency packaging
OCI Functions does not download Python dependencies during function creation. Package required dependencies in the archive before creating or updating the function.
The Python FDK is included in the managed Python runtime for code-only functions. You do not have to package the FDK in the archive. If you package a different FDK version in python/, your packaged version overrides the FDK included in the runtime. Validate compatibility before deploying to production.
Python archive with native dependencies
Python functions can depend on packages that include compiled native code, such as .so files or Python wheels built for a specific Linux architecture.
For single-architecture Python functions:
- Do not include root-level
native/. - Package dependencies directly under
python/. - Make sure dependencies are built for the correct Linux platform and architecture.
For a single-architecture application, package Python dependencies, including dependencies with native code, under python/.
archive-root/
├── function/
└── func.py
├── python/
└── <python dependencies, including native dependencies for the application architecture>
└── resources/ # optional
For multi-architecture Python functions:
- Package shared source under
function/. - Package pure-Python dependencies under
python/, if needed. - Package architecture-specific native dependencies under:
native/fn-arch-x86/native/fn-arch-arm/
archive-root/
├── function/
└── func.py
├── python/ # optional pure-Python dependencies
├── resources/ # optional
└── native/
├── fn-arch-x86/
└── fn-arch-arm/
Rules:
native/is required only if the function uses architecture-specific native dependencies.- If
native/exists for a multi-architecture application, it must contain bothfn-arch-x86/andfn-arch-arm/. - Place native dependency files only under the matching architecture directory.
function/is always required.
Python runtime behavior
The Python FDK is included in the managed Python runtime for code-only functions. Package the handler module, application code, and any required dependencies in the archive.
Creating a Python code-only function
To create a Python code-only function, follow the instructions in Creating Code-only Functions. For example, to create a Python code-only function using the Console:
- On the Applications list page, select the application in which you want to create the Python code-only function. If you need help finding the list page or the application, see Listing Applications.
-
Select the Functions tab.
The Functions list page opens. All functions in the selected application are displayed in a table.
- Select Create from archive and specify details for the Python code-only function, including the file source, the Python archive, a supported Python runtime, and a Python handler.
For Python code-only functions, you must specify a handler when you create the function.
- Specify other details for the function as required (see Creating Code-only Functions).
- Select Create to create the function.
The Python code-only function is created and can be invoked like any other function.
Troubleshooting
| Error Code | Problem | Likely cause | Recommended fix |
|---|---|---|---|
400 |
Archive validation fails because function/ is missing |
Source files are at the ZIP root or in the wrong folder | Put handler and application code under function/. |
400 |
Handler validation fails | Handler is not in <file>.<function> format |
Use a handler such as func.handler. |
400 |
Handler cannot be found | Handler file or function name does not match the archive | Confirm that the module and function exist under function/. |
400 |
Function fails because dependency is missing | Dependency was not packaged under python/ |
Install dependencies into python/ before creating the ZIP. |
400 |
Function fails because native dependency is incompatible | Dependency was built for macOS, Windows, or the wrong Linux architecture | Build or download dependencies for the correct Linux architecture. |
400 |
Single-architecture archive fails validation | Root-level native/ directory is present |
Remove native/; package single-architecture dependencies under python/. |
400 |
Multi-architecture native archive fails validation | One architecture directory is missing | Include both native/fn-arch-x86/ and native/fn-arch-arm/. |
400 |
Native archive validation fails | Files were placed directly under native/ |
Move files under native/fn-arch-x86/ or native/fn-arch-arm/. |