Passing local data

If you have a function that needs access to some data not present in your Python files themselves you have a few options for bundling that data with your Modal app.

Passing function arguments

The simplest and most straight-forward way is to read the data from your local script and pass the data to the outermost Modal function call:

import json


@app.function()
def foo(a):
    print(sum(a["numbers"]))


@app.local_entrypoint()
def main():
    data_structure = json.load(open("blob.json"))
    foo.remote(data_structure)

Any data of reasonable size that is serializable through cloudpickle is passable as an argument to Modal functions.

Refer to the section on global variables for how to work with objects in global scope that can only be initialized locally.

Mounting directories

If you want to forward files from your local system, you can do that through modal.Mount objects and the mounts function decorator option:

@app.function(mounts=[modal.Mount.from_local_dir("/user/john/.aws", remote_path="/root/.aws")])
def aws_stuff():
    ...

Note: the mounted directory will not be shared between worker instances, so modifying files or writing new files to a mount will not be reflected in other functions calls with the same mount. For this reason, you should typically treat the Mount as read-only.

Mounting local packages

For the special case of mounting a local package so it’s also available within your Python environment inside the container, Modal provides a Mount.from_local_python_packages helper function:

import modal
import my_local_module

app = modal.App()  # Note: prior to April 2024, "app" was called "stub"

@app.function(mounts=modal.Mount.from_local_python_packages("my_local_module", "my_other_module"))
def f():
    my_local_module.do_stuff()