modal.Mount

class Mount(modal.object.Object)

Create a mount for a local directory or file that can be attached to one or more Modal functions.

Usage

import modal
import os
stub = modal.Stub()

@stub.function(mounts=[modal.Mount.from_local_dir("~/foo", remote_path="/root/foo")])
def f():
    # `/root/foo` has the contents of `~/foo`.
    print(os.listdir("/root/foo/"))

Modal syncs the contents of the local directory every time the app runs, but uses the hash of the file’s contents to skip uploading files that have been uploaded before.

def __init__(self, *args, **kwargs):

from_id

@classmethod
def from_id(cls: Type[O], object_id: str, client: Optional[_Client] = None) -> O:

Retrieve an object from its unique ID (accessed through obj.object_id).

deps

@property
def deps(self) -> Callable[..., List["_Object"]]:

persist

def persist(
    self, label: str, namespace=api_pb2.DEPLOYMENT_NAMESPACE_WORKSPACE, environment_name: Optional[str] = None
):

Object.persist is deprecated for generic objects. See NetworkFileSystem.persisted or Dict.persisted.

from_name

@classmethod
def from_name(
    cls: Type[O],
    app_name: str,
    tag: Optional[str] = None,
    namespace=api_pb2.DEPLOYMENT_NAMESPACE_WORKSPACE,
    environment_name: Optional[str] = None,
) -> O:

Retrieve an object with a given name and tag.

Useful for referencing secrets, as well as calling a function from a different app. Use this when attaching the object to a stub or function.

Examples

# Retrieve a secret
stub.my_secret = Secret.from_name("my-secret")

# Retrieve a function from a different app
stub.other_function = Function.from_name("other-app", "function")

# Retrieve a persisted Volume, Queue, or Dict
stub.my_volume = Volume.from_name("my-volume")
stub.my_queue = Queue.from_name("my-queue")
stub.my_dict = Dict.from_name("my-dict")

lookup

@classmethod
def lookup(
    cls: Type[O],
    app_name: str,
    tag: Optional[str] = None,
    namespace=api_pb2.DEPLOYMENT_NAMESPACE_WORKSPACE,
    client: Optional[_Client] = None,
    environment_name: Optional[str] = None,
) -> O:

Lookup an object with a given name and tag.

This is a general-purpose method for objects like functions, network file systems, and secrets. It gives a reference to the object in a running app.

Examples

# Lookup a secret
my_secret = Secret.lookup("my-secret")

# Lookup a function from a different app
other_function = Function.lookup("other-app", "function")

# Lookup a persisted Volume, Queue, or Dict
my_volume = Volume.lookup("my-volume")
my_queue = Queue.lookup("my-queue")
my_dict = Dict.lookup("my-dict")

add_local_dir

@typechecked
def add_local_dir(
    self,
    local_path: Union[str, Path],
    *,
    # Where the directory is placed within in the mount
    remote_path: Union[str, PurePosixPath, None] = None,
    # Filter function for file selection; defaults to including all files
    condition: Optional[Callable[[str], bool]] = None,
    # add files from subdirectories as well
    recursive: bool = True,
) -> "_Mount":

Add a local directory to the Mount object.

from_local_dir

@staticmethod
@typechecked
def from_local_dir(
    local_path: Union[str, Path],
    *,
    # Where the directory is placed within in the mount
    remote_path: Union[str, PurePosixPath, None] = None,
    # Filter function for file selection - default all files
    condition: Optional[Callable[[str], bool]] = None,
    # add files from subdirectories as well
    recursive: bool = True,
) -> "_Mount":

Create a Mount from a local directory.

Usage

assets = modal.Mount.from_local_dir(
    "~/assets",
    condition=lambda pth: not ".venv" in pth,
    remote_path="/assets",
)

add_local_file

@typechecked
def add_local_file(
    self, local_path: Union[str, Path], remote_path: Union[str, PurePosixPath, None] = None
) -> "_Mount":

Add a local file to the Mount object.

from_local_file

@staticmethod
@typechecked
def from_local_file(local_path: Union[str, Path], remote_path: Union[str, PurePosixPath, None] = None) -> "_Mount":

Create a Mount mounting a single local file.

Usage

# Mount the DBT profile in user's home directory into container.
dbt_profiles = modal.Mount.from_local_file(
    local_path="~/profiles.yml",
    remote_path="/root/dbt_profile/profiles.yml"),
)

from_local_python_packages

@staticmethod
def from_local_python_packages(*module_names: str) -> "_Mount":

Returns a modal.Mount that makes local modules listed in module_names available inside the container. This works by mounting the local path of each module’s package to a directory inside the container that’s on PYTHONPATH.

Usage

import modal
import my_local_module

stub = modal.Stub()

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