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 NetworkFileSystems and Mounts.

The primary interface for accessing this feature is the modal 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, NetworkFileSystems 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.

Live updating

Hot reloading with modal serve

Modal has the command modal serve <filename.py>, which creates a loop that live updates an app when any of the supporting files change.

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

from modal import Stub, web_endpoint

stub = Stub()

@stub.function()
@web_endpoint()
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 you edit this file, the modal serve command will detect the change and update the code, without having to restart the command.

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