Invoke (pyinvoke)

raw JSON →
1.0.4 verified Tue May 12 auth: no python install: verified quickstart: verified

Invoke, often referred to as pyinvoke to avoid name collision, is a Python task execution tool and library, drawing inspiration from Make and Rake. It enables users to define shell commands within Python functions and execute them via a command-line interface. The current stable version is 1.0.4, with releases occurring periodically to maintain compatibility and add features.

pip install invoke
error ModuleNotFoundError: No module named 'invoke'
cause The `invoke` package is not installed in the current Python environment.
fix
Run pip install invoke to install the library.
error NameError: name 'task' is not defined
cause The `@task` decorator was used in `tasks.py` without being imported from the `invoke` package.
fix
Add from invoke import task at the top of your tasks.py file.
error TypeError: my_task() missing 1 required positional argument: 'c'
cause A task function was defined without its required `Context` object as the first argument.
fix
Modify the task function signature to accept a Context object, e.g., change def my_task(): to def my_task(c):.
error CommandNotFoundError: The command 'your-command-here' was not found. (or FileNotFoundError)
cause The shell command specified within `c.run()` does not exist or is not in the system's PATH where `invoke` is executed.
fix
Ensure the command is correctly installed and accessible in your shell's PATH, or provide the full absolute path to the executable in c.run().
gotcha The PyPI package name is `invoke`, not `pyinvoke`. Always use `pip install invoke` for installation.
fix Use `pip install invoke` to install the library.
breaking Task functions now explicitly require a `Context` object as their first argument. Older examples or codebases might omit this, leading to `TypeError`.
fix Update task definitions to accept a `Context` object, e.g., `@task def my_task(c): ...`.
gotcha Invoke 1.x supports both Python 2.7+ and 3.4+. However, future major versions (e.g., Invoke 2.x) are expected to drop Python 2 support. Plan for migration if still using Python 2.
fix Ensure your development environment uses Python 3.4+ for forward compatibility. Regularly check Invoke's documentation for upcoming breaking changes related to Python version support.
gotcha The way task arguments are defined and parsed (e.g., short/long flags, default values, types) has evolved. Complex argument parsing might require specific syntax or aliases.
fix Refer to the official documentation on task arguments (`docs.pyinvoke.org/en/stable/concepts/tasks.html#argument-parsing`) for the latest and recommended patterns, especially for optional or typed arguments.
python os / libc status wheel install import disk
3.10 alpine (musl) - - 0.67s 19.0M
3.10 slim (glibc) - - 0.40s 19M
3.11 alpine (musl) - - 1.05s 21.1M
3.11 slim (glibc) - - 0.68s 22M
3.12 alpine (musl) - - 1.32s 12.9M
3.12 slim (glibc) - - 0.98s 13M
3.13 alpine (musl) - - 1.31s 12.5M
3.13 slim (glibc) - - 0.84s 13M
3.9 alpine (musl) - - 0.46s 18.5M
3.9 slim (glibc) - - 0.48s 19M

Create a `tasks.py` file in your project directory. Define tasks as Python functions decorated with `@task`, ensuring they accept a `Context` object (conventionally named `c`) as their first argument. Execute tasks from the command line using `invoke <task_name>`.

from invoke import task

@task
def clean(c):
    """Removes build artifacts."""
    print("Cleaning up build/ directory...")
    # Use '|| true' for robustness in quickstart if 'rm' might fail or not exist
    c.run("rm -rf build/ || true")
    print("Clean complete.")

@task
def greeting(c, name="World"):
    """Says hello to the given NAME."""
    print(f"Hello, {name}!")

@task(aliases=['deploy'])
def push(c):
    """Pushes code to a remote repository (example)."""
    print("Simulating deployment...")
    # For quickstart, a simple echo is safer than an actual deployment command
    c.run("echo 'Deployment simulated!'")
    print("Push complete.")

# To run these tasks:
# 1. Save this code as `tasks.py` in your project root.
# 2. Open your terminal in the same directory.
# 3. Run tasks: `invoke clean`, `invoke greeting --name Pythonista`, `invoke deploy`