Fabric (v2/v3)
raw JSON → 3.2.3 verified Fri May 01 auth: no python
High-level Python library for SSH command execution and system administration, built on top of Invoke. The current version is 3.2.3 (2024-08). Fabric v2+ is a ground-up rewrite; it is incompatible with the original Fabric 1.x. The library uses an active, though slow, release cadence.
pip install fabric Common errors
error KeyError: 'Connection' ↓
cause Trying to import `Connection` from the wrong submodule (e.g., `fabric.connection` instead of `fabric`).
fix
Use
from fabric import Connection (not from fabric.connection import Connection). error ModuleNotFoundError: No module named 'fabric.decorators' ↓
cause Using old Fabric 1.x import pattern in Fabric v2+.
fix
Replace with
from invoke import task for decorators, and use from fabric import Connection for connections. Warnings
breaking Fabric v2/v3 is a complete rewrite of Fabric 1.x. No backward compatibility. Do not import `fabric.api`, `fabric.decorators`, or use `fab` command. ↓
fix Use `from fabric import Connection`, `from invoke import task`. Run tasks with `invoke` CLI.
deprecated The original Fabric 1.x (fabric<2) is unmaintained. The PyPI package name is now 'fabric', but legacy users might install 'fabric2' (which redirects). ↓
fix Upgrade to fabric >=2 and rewrite code as per new API.
gotcha The `run()` method returns a `Result` object, not a string. Many newcomers try to use the result as a string directly. ↓
fix Access stdout via `result.stdout` or use str cast: `str(result)`. Also check `result.exited` for exit code.
Imports
- Connection wrong
from fabric import Connectioncorrectfrom fabric import Connection - task wrong
from fabric.decorators import taskcorrectfrom invoke import task
Quickstart
from fabric import Connection
# Replace with your host/user/password or use SSH key
c = Connection(host='example.com', user='myuser', connect_kwargs={'password': os.environ.get('PASSWORD', '')})
result = c.run('whoami')
print(result.stdout.strip())