Skip to content
Latchkey

Pulumi Python "ModuleNotFoundError" in CI

The Python Pulumi program imported a module that is not installed in the interpreter Pulumi used. In CI this is usually a virtualenv that was not created or whose requirements were never installed before pulumi up.

What this error means

A Pulumi step fails with "ModuleNotFoundError: No module named 'pulumi_aws'" (or pulumi), wrapped by "Running program ... failed".

pulumi
error: Running program '/home/runner/work/app/app' failed with an unhandled exception:
    Traceback (most recent call last):
      File "./__main__.py", line 2, in <module>
        import pulumi_aws as aws
    ModuleNotFoundError: No module named 'pulumi_aws'

Common causes

Requirements not installed into the venv

Pulumi created or used a virtualenv but the job never ran pip install -r requirements.txt into it, so the SDK packages are missing.

Program ran against the wrong interpreter

The runtime points at a system Python without the dependencies instead of the venv that has them.

How to fix it

Install requirements into the Pulumi venv

  1. Create the virtualenv Pulumi expects (or let it manage one).
  2. Install requirements.txt into that interpreter.
  3. Run the Pulumi command.
Terminal
python3 -m venv venv
./venv/bin/pip install -r requirements.txt
pulumi up --yes

Let Pulumi manage the virtualenv

Set the virtualenv runtime option so Pulumi creates and uses a venv with your requirements.

Pulumi.yaml
runtime:
  name: python
  options:
    virtualenv: venv

How to prevent it

  • Install Python requirements before any Pulumi command.
  • Use the virtualenv runtime option so the interpreter is consistent.
  • Cache the pip download directory keyed on requirements.

Frequently asked questions

What causes ""ModuleNotFoundError" (Python)"?
Pulumi created or used a virtualenv but the job never ran pip install -r requirements.txt into it, so the SDK packages are missing.
How do I fix "ModuleNotFoundError" (Python)?
Install requirements into the Pulumi venv

Related guides

References

Latchkey auto-heals failures like this one - detected, fixed, and retried without you. Start free → 30-day trial · No credit card