GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Guide
  • Examples
  • Log in
  • Sign up

This view

  • Original page
  • New address

GeezerVison is showing modal.com — Project structure

Guide Introduction to Modal Getting started Functions Functions Invocation methods Function lookups Container lifecycle management Parametrized Functions Dynamic configuration Sandboxes Sandboxes Running commands Networking and security File access Snapshots Exit Snapshots BetaPricing and resources Sidecars BetaSecret injection BetaModel APIs Model APIs Models and pricing Agent integrations Endpoints Endpoints Supported models Candidates Training Quickstart Tinker-compatible API BetaMulti-node training Images Defining Images Using existing container images Named images Fast pull from registry GPUs and other resources GPU acceleration Using CUDA on Modal Configuring CPU, memory, and disk Scaling out Scaling out Input concurrency Batch processing Job queues Dynamic batching Multi-node Clusters Deployment Apps, Functions, and entrypoints Managing deployments Continuous deployment Scheduled Functions Observability HTTP Applications Servers Sticky Sessions Web Functions Streaming endpoints Web Function URLs Request timeouts Proxy Tokens Networking Tunnels Proxies BetaCluster networking Data sharing and storage Passing local data Volumes Storing model weights Cloud bucket mounts Dicts Queues Dataset ingestion Secrets and environment variables Secrets Environment variables Performance Cold start performance Memory Snapshots High-performance LLM inference Reliability and robustness Failures and retries Preemption Timeouts GPU health Troubleshooting Security and privacy Security and privacy Data residency Customer-supplied encryption keys AlphaAudit logs Integrations Using OIDC to authenticate with external services Connecting Modal to your Datadog account Connecting Modal to your OpenTelemetry provider Slack notifications Workspace & account settings Workspaces Environments Service users User groups Role-Based Access Control (RBAC) Billing Network egress billing Budgets Identity and provisioning Okta SSO Microsoft Entra SSO Google Workspace SSO Custom SAML SSO SCIM Integration Other topics Feature maturity Restricted Functions JavaScript/Go SDKs BetaModal 1.0 migration guide File and project structure Developing and debugging Jupyter notebooks Asynchronous API usage Global variables Region selection Localized placement GPU Metrics Search syntax

Project structure

Apps spanning multiple files 

When your project spans multiple files, more care is required to package the full structure for running or deploying on Modal.

There are two main considerations: (1) ensuring that all of your Functions get registered to the App, and (2) ensuring that any local dependencies get included in the Modal container.

Say that you have a simple project that’s distributed across three files:

src/
├── app.py  # Defines the `modal.App` as a variable named `app`
├── llm.py  # Imports `app` and decorates some functions
└── web.py  # Imports `app` and decorates other functions

With this structure, if you deploy using modal deploy src/app.py, Modal won’t discover the Functions defined in the other two modules, because they never get imported.

If you instead run modal deploy src/llm.py, Modal will deploy the App with just the Functions defined in that module.

One option would be to ensure that one module in the project transitively imports all of the other modules and to point the modal deploy CLI at it, but this approach can lead to an awkward project structure.

Defining your project as a Python package 

A better approach would be to define your project as a Python package and to use the Modal CLI’s “module mode” invocation pattern.

In Python, a package is a directory containing an __init__.py file (and usually some other Python modules). If you have a src/__init__.py that imports all of the member modules, it will ensure that any decorated Functions contained within them get registered to the App:

# Contents of __init__.py
import .app
import .llm
import .web

Important: use relative imports (import .app) between member modules.

Unfortunately, it’s not enough just to set this up and make your deploy command modal deploy src/app.py. Instead, you need to invoke Modal in module mode: modal deploy -m src.app. Note the use of the -m flag and the module path (src.app instead of src/app.py). Akin to python -m ..., this incantation treats the target as a package rather than just a single script.

App composition 

As your project grows in scope, it may become helpful to organize it into multiple component Apps, rather than having the project defined as one large monolith. That way, as you iterate during development, you can target a specific component, which will build faster and avoid any conflicts with concurrent work on other parts of the project.

Projects set up this way can still be deployed as one unit by using App.include. Say our project from above defines separate Apps in llm.py and web.py and then adds a new deploy.py file:

# Contents of deploy.py
import modal

from .llm import llm_app
from .web import web_app

app = modal.App("full-app").include(llm_app).include(web_app)

This lets you run modal deploy -m src.deploy to package everything in one step.

Note: Since the multi-file App still has a single namespace for all Functions, it’s important to name your Modal Functions uniquely across the project even when splitting it up across files: otherwise you risk some Functions “shadowing” others with the same name.

Including local dependencies 

Another factor to consider is whether Modal will package all of the local dependencies that your App requires.

Even if your Modal App itself can be contained to a single file, any local modules that file imports (like, say, a helpers.py) also need to be available in the Modal container.

By default, Modal will automatically include the module or package where a Function is defined in all containers that run that Function. So if the project is set up as a package and the helper modules are part of that package, you should be all set. If you’re not using a package setup, or if the local dependencies are external to your project’s package, you’ll need to explicitly include them in the Image, i.e. with modal.Image.add_local_python_source.

Note: This behavior changed in Modal 1.0. Previously, Modal would “automount” any local dependencies that were imported by your App source into a container. This was changed to be more selective to avoid unnecessary inclusion of large local packages.

Project structureApps spanning multiple filesDefining your project as a Python packageApp compositionIncluding local dependencies
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.