Why Modal gives ModuleNotFoundError for a package you installed

By Christophe Paka · Facts checked on 7 November 2026

You add pandas to your Image, the cloud environment for your Modal function. You type modal run report.py. Within a second, your own terminal prints ModuleNotFoundError, naming pandas. This modal ModuleNotFoundError was the message readers of the book's first edition reported more than any other.

The package is installed and the code is right. The import ran in the wrong place, so you can reinstall all day and nothing changes.

The figure below draws the line between the two places your file runs.

One script, two environments: your laptop has Python and the modal client but no pandas and runs the top of the file; the Modal container, booted from your Image with pandas and numpy, runs the function body.
Figure 4.1 The local and remote split: the file loads on your laptop, where pandas is missing, and the function body runs in the container, where the Image installed it.
Use this diagram on your site

Free to share in your blog post, docs, course or slides, as long as the credit link stays. Paste the code below where the figure should go.

How to read this figure

  1. Read the dashed line first. The line splits one script into two places. On the left is your laptop, with Python and the modal client but no pandas. On the right is a Modal container, booted from your Image, with pandas and numpy installed.

  2. See what runs on your laptop. When modal run starts, Python runs your whole file on your laptop, because that is the only way to find the App. So import modal, the App and the Image chain all run there. An import pandas at the top would crash here.

  3. Follow the arrow across. The arrow labeled modal run report.py crosses the boundary. Only the inside of a decorated function goes to the cloud, and it runs there once per input.

  4. See where the import works. Inside my_function, import pandas as pd runs in the container, where the Image put pandas. It is the same line of code on the other side of the boundary, and it works.

  5. Apply the rule of thumb. Keep modal and standard library imports, such as sys and os, at the top, since they exist on both sides. Import every package that lives only in the Image inside the function body. The order of the Image chain matters too: see the Image layer cake.

Key takeaways

  • Import packages that live only in the Image inside the function body, where the container runs them.
  • Keep only modal and standard library imports, such as sys and os, at the top of the file.
  • When your own terminal shows ModuleNotFoundError, move the import; do not reinstall the package.

Worked example: the Morning Brief report job

Morning Brief, the book's running project, counts its 200 morning items with pandas. Your laptop has the modal client and no pandas. The Image installs pandas and numpy.

  1. The first try puts import pandas as pd on line 2, at the top of the file.
  2. You type modal run report.py, and Python reads the file on your laptop.
  3. Line 2 runs on your laptop, where pandas does not exist. Python stops with ModuleNotFoundError. Nothing reached the cloud, so the Image was never used.
  4. The fix moves the import inside my_function. Or keep it at the top under with pandas_image.imports():, which runs it only inside the container.

Result: The laptop now reads the file without touching pandas, and the container runs the import and the code. Once you know the boundary, this error is a 30 second fix.

Questions readers ask

Do I need pandas installed on my laptop too?

No. With the import inside the function, your laptop never runs it. Your laptop needs Python and the modal client. The container gets pandas from the Image, which Modal builds in its cloud the first time you run.

What does the container have if I attach no Image?

Modal uses a default Image: Debian Linux with Python at your laptop's minor version. It has none of your packages. List what the container needs in an Image, and pin the Python version with python_version so two teammates get the same container.

Do I need Docker to deploy a model on Modal?

No. A Modal Image is your environment written as Python: a chain of method calls, no Dockerfile. Modal builds it in its cloud, and you attach it to a function with image=.

Sources: Images guide (Modal docs).

This figure is from Modal Made Crystal Clear, chapter 4.

The chapter builds the idea step by step, with recipes you can follow, a Try it to check yourself, and every source.

Get the book

All diagrams