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:
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, 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:
Python
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:
Shell
$ invoke greet
Hello, World!
$ invoke greet --name="Real Python"
Hello, Real Python!
List available tasks
Shell
$ invoke --list
Organize tasks into namespaces:
Python
tasks.py
from invoke import Collection, task
@task
def build_docs(c):
c.run("echo 'Building documentation...'")
# Buid 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:
Shell
$ invoke docs.build
Building documentation...
$ invoke docs.clean
Cleaning documentation...
By Leodanis Pozo Ramos • Updated Dec. 17, 2025