Skip to content

Invoke

Invoke is a Python library and command-line tool for defining reusable automation tasks as functions and running them from the shell. It’s designed for shell-oriented subprocess execution and task organization, and it draws inspiration from tools like Make and Rake.

Installation and Setup

Install it from Python Package Index (PyPI) into a virtual environment or directly on your system:

Language: Shell
$ python -m pip install invoke

Key Features

  • Defines tasks as regular Python functions decorated with @task.
  • Executes shell commands through .run() and supports configurable behavior driven by Invoke’s configuration system.
  • Organizes tasks into nested namespaces using Collection objects, exposing dot-separated names like docs.build.
  • Provides both CLI and library APIs, allowing you to reuse Invoke’s parser and runner building blocks in custom tooling.

Usage

In a typical workflow, you define a set of @task functions in tasks.py and call them from the command line. Here’s a toy example:

Language: Python Filename: tasks.py
from invoke import task

@task
def greet(c, name="World"):
    c.run(f"echo Hello, {name}!")

With this file in place you can run the greet task:

Language: Shell
$ invoke greet
Hello, World!
$ invoke greet --name="Real Python"
Hello, Real Python!

List available tasks

Language: Shell
$ invoke --list

Organize tasks into namespaces:

Language: Python Filename: tasks.py
from invoke import Collection, task

@task
def build_docs(c):
    c.run("echo 'Building documentation...'")
    # Build command here...

@task
def clean_docs(c):
    c.run("echo 'Cleaning documentation...'")
    # Cleanup command here...

ns = Collection()
docs = Collection("docs")
docs.add_task(build_docs, "build")
docs.add_task(clean_docs, "clean")
ns.add_collection(docs)

Then call:

Language: Shell
$ invoke docs.build
Building documentation...
$ invoke docs.clean
Cleaning documentation...
The subprocess Module: Wrapping Program With Python

Tutorial

The subprocess Module: Wrapping Programs With Python

In this tutorial, you'll learn how to leverage other apps and programs that aren't Python, wrapping them or launching them from your Python scripts using the subprocess module. You'll learn about processes all the way up to interacting with a process as it executes.

intermediate devops stdlib

For additional information on related topics, take a look at the following resources:


By Leodanis Pozo Ramos • Updated Sept. 16, 2026