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
app = modal.App() # Note: "app" was called "stub" up until April 2024
@app.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):
add_local_dir
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
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
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
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,
remote_dir: Union[str, PurePosixPath] = ROOT_DIR.as_posix(),
condition: Optional[Callable[[str], bool]] = None,
) -> "_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
app = modal.App() # Note: "app" was called "stub" up until April 2024
@app.function(mounts=[
modal.Mount.from_local_python_packages("my_local_module", "my_other_module"),
])
def f():
my_local_module.do_stuff()
from_name
@staticmethod
def from_name(
label: str,
namespace=api_pb2.DEPLOYMENT_NAMESPACE_WORKSPACE,
environment_name: Optional[str] = None,
) -> "_Mount":
lookup
@classmethod
def lookup(
cls: Type["_Mount"],
label: str,
namespace=api_pb2.DEPLOYMENT_NAMESPACE_WORKSPACE,
client: Optional[_Client] = None,
environment_name: Optional[str] = None,
) -> "_Mount":