Invoke
Invoke is a Python library for managing shell-oriented subprocesses and organizing executable Python code into CLI-invokable tasks. It provides a clean, high-level API for running shell commands and defining/organizing task functions from a `tasks.py` file, drawing inspiration from tools like Make and Rake. The current version is 2.2.1, with releases occurring periodically to introduce new features and address issues.
Warnings
- breaking Invoke 2.0.0 removed support for Python versions less than 3.6. Code targeting older Python 2.x or early Python 3.x (e.g., 3.4, 3.5) will break.
- breaking The `Task.argspec` method's return value changed in 2.0.0 to return an `inspect.Signature` object instead of a tuple. This affects users directly inspecting task function signatures programmatically.
- breaking The default value for `MockContext.repeat_init` changed from `False` to `True` in Invoke 2.0.0. This can affect testing setups using `MockContext`.
- deprecated The `invoke.util.encode_output` function was effectively removed in Invoke 2.0.0 as it became a no-op under Python 3.
- gotcha All functions decorated with `@task` must accept at least one positional argument, conventionally named `c`, `ctx`, or `context`, which represents the `invoke.Context` object. Forgetting this argument will cause the task to fail at runtime.
Install
-
pip install invoke
Imports
- task
from invoke import task
- Collection
from invoke import Collection
- run
from invoke import run
Quickstart
from invoke import task
@task
def greet(c, name='World'):
"""Says hello to the specified name."""
print(f"Hello, {name}!")
@task
def clean(c):
"""Removes temporary build files."""
# Example of running a shell command
c.run("echo 'Cleaning build directory...'", warn=True)
# For a real scenario, you might do:
# c.run("rm -rf build/")