Developing and Debugging

Modal makes it easy to run apps in the cloud, try code changes in the cloud, and debug remotely executing code as if it were right there on your laptop. To speed boost your inner dev loop, this guide provides a rundown of tools and techniques for developing and debugging software in Modal.

Interactivity

You can launch a Modal Function interactively and have it drop you right into the middle of the action, at an interesting callsite or the site of a runtime detonation.

Interactive functions

It is possible to start the interactive Python debugger or start an IPython REPL right in the middle of a Modal function.

The Python debugger is initiated with the language built-in breakpoint() function:

@stub.function(interactive=True)
def f():
    breakpoint()
    print("step1")
    print("step2")

An IPython REPL is started like so:

@stub.function(interactive=True)
def f():
    model = expensive_function()
    # play around with `model`
    import IPython
    IPython.embed()

Don’t worry about installing IPython in your containers, that’s not necessary. It is provided by Modal’s execution environment.

Interactive shell

Modal lets you run interactive commands on your container images from the terminal. This is handy for debugging issues with your image, interactively refining build commands, and exploring the contents of SharedVolumes and Mounts.

The primary interface for accessing this feature is the modal app shell CLI command, which accepts a function name in your app (or prompts you to select one, if none is provided), and runs an interactive command on the same image as the function, with the same Secrets, SharedVolumes and Mounts attached as the selected function.

The default command is /bin/bash, but you can override this with any other command of your choice using the --cmd flag.

Note that this command does not attach a shell to an existing instance of the function, but instead creates a fresh instance of the underlying image. We might support the former soon - please reach out to us if that would be useful to you.

stub.interactive_shell()

Alternatively, you can also spawn an interactive shell from a Python script, by running the function stub.interactive_shell(). This is useful if you want to run the shell with custom arguments that don’t correspond to any of your existing functions.

Live updating

Developing with stub.serve()

Modal Stubs come with the handy serve function, which live updates an app when any of the supporting files change.

Live updating works with webhooks, syncing your changes as you make them, and it also works well with cron schedules and job queues.

import modal
stub = modal.Stub()

@stub.webhook()
def f():
    return "I update on file edit!"

@stub.function(schedule=modal.Period(seconds=5))
def run_me():
    print("I also update on file edit!")

if __name__ == "__main__":
    stub.serve()

Observability

Each running Modal app, including all ephemeral apps, streams logs and resource metrics back to you for viewing.

On start, an app will log a dashboard link that will take you its app page.

$ python3 main.py
✓ Initialized. View app page at https://modal.com/apps/ap-XYZ1234.
...

From this page you can access the following:

  • logs, both from your application and system-level logs from Modal
  • compute resource metrics (CPU, RAM, GPU)
  • function call history, including historical success/failure counts