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".
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
- Create the virtualenv Pulumi expects (or let it manage one).
- Install
requirements.txtinto that interpreter. - Run the Pulumi command.
python3 -m venv venv
./venv/bin/pip install -r requirements.txt
pulumi up --yesLet Pulumi manage the virtualenv
Set the virtualenv runtime option so Pulumi creates and uses a venv with your requirements.
runtime:
name: python
options:
virtualenv: venvHow to prevent it
- Install Python requirements before any Pulumi command.
- Use the
virtualenvruntime option so the interpreter is consistent. - Cache the pip download directory keyed on requirements.