Fabric3
Fabric3 is a Python library and command-line tool designed for remote execution and deployment over SSH, forked from the original Fabric 1.x to provide compatibility with Python 3.4+. While it achieved Python 3 support, the official Fabric project (versions 2.x and above, and eventually 1.15.0) subsequently gained Python 3 compatibility and a redesigned API, rendering Fabric3 obsolete. It is now considered an unauthorized and deprecated fork.
Common errors
-
Command 'fab' not found
cause Fabric3's command-line tool 'fab' is not in your system's PATH, or Fabric3 was not installed correctly for your active Python environment.fixEnsure Fabric3 is installed for your current Python interpreter (`pip show fabric3`). Verify that the Python Scripts directory (e.g., ~/.local/bin on Linux, or C:\Python\Scripts on Windows) is included in your system's PATH. If using virtual environments, activate the environment before running `fab`. -
ImportError: No module named 'fabric.api'
cause Fabric3 is either not installed, or you are trying to import `fabric.api` in an environment where the official `fabric` library (2.x+) is installed, which changed its import paths.fixIf you intend to use Fabric3, ensure it's installed (`pip install fabric3`). If you are migrating to the official Fabric 2.x+, the main import is `from fabric import Connection, task` and `fabric.api` no longer exists. -
UnicodeDecodeError: 'ascii' codec can't decode byte 0x__ in position __: ordinal not in range(128)
cause This often occurs when remote output or file content contains non-ASCII characters and the terminal or Python environment defaults to an 'ascii' encoding.fixEnsure your local and remote environments are configured to use UTF-8. For example, set `LANG=en_US.UTF-8` and `LC_ALL=en_US.UTF-8` in your shell environments. Fabric3 (and Fabric 1.15.0) addressed some `UnicodeDecodeError` issues, but environment misconfiguration can still cause problems.
Warnings
- breaking Fabric3 is an unauthorized and deprecated fork of Fabric 1.x. The official Fabric library (versions 2.x and above) now natively supports Python 3 and has a significantly different, more modern API. Continued use of Fabric3 is strongly discouraged.
- deprecated The primary maintainers of the original Fabric project explicitly state that they do not control the `fabric3` PyPI entry and there will be no official `fabric3` or `fabric4` releases. The 1.x branch of mainline Fabric itself gained Python 3 support in version 1.15.0.
- gotcha Fabric 1.x (and thus Fabric3) relies heavily on a global `env` object for configuration, which can lead to issues in concurrent or complex environments. Fabric 2.x+ uses explicit `Connection` objects.
- gotcha The `fabric.utils.RingBuffer` utility was removed in Fabric 1.x and should not be used. It was commonly replaced by `collections.deque`.
Install
-
pip install fabric3
Imports
- run
from fabric.api import run
- env
from fabric.api import env
- local
from fabric.api import local
- cd
from fabric.api import cd
- sudo
from fabric.api import sudo
- put
from fabric.api import put
- get
from fabric.api import get
Quickstart
# fabfile.py
from fabric.api import run, env, sudo, put
# Configure hosts (replace with actual SSH connection details)
env.hosts = ['user@your_remote_host_ip']
env.password = 'your_ssh_password' # Or use SSH agent/key
env.warn_only = True # Don't abort on non-zero exit codes
def deploy():
print(f"Deploying to {env.host_string}...")
# Example: Update package lists and install nginx
sudo('apt-get update && apt-get install -y nginx')
run('service nginx start')
print("Nginx installed and started.")
# Example: Upload a local file
local_path = '/tmp/test_file.txt'
remote_path = '/tmp/remote_test.txt'
with open(local_path, 'w') as f:
f.write('Hello from Fabric3!')
put(local_path, remote_path)
run(f'cat {remote_path}')
print("File uploaded and content verified.")
# To run this from your terminal:
# fab deploy