Skip to content

Migrate from textfsm

textfsm2 ships the textfsm2 Python package so it can coexist with upstream google/textfsm in the same environment (just like httpx2 does for httpx). You do not need to uninstall textfsm or pass --no-deps when installing packages like ntc-templates.

Switch direct textfsm imports

Replace import textfsm with an aliased import in your own modules:

import textfsm2 as textfsm
from textfsm2 import clitable

Every public class, method, attribute, and exception stays identical to upstream textfsm.

For libraries or gradual rollouts that need to support environments where only textfsm is installed, fall back cleanly:

try:
    import textfsm2 as textfsm
except ModuleNotFoundError:
    import textfsm

Accelerate third-party libraries (alias_textfsm)

Libraries such as ntc-templates and netmiko import textfsm directly. Call textfsm2.alias_textfsm() at the top of your application entrypoint so every subsequent import textfsm resolves to textfsm2 process-wide:

import textfsm2

textfsm2.alias_textfsm()

from ntc_templates.parse import parse_output  # runs on textfsm2

Follow two rules when using alias_textfsm():

  • Call it first: Execute alias_textfsm() before any module imports textfsm. If another textfsm module is already loaded in sys.modules, it raises RuntimeError.
  • Use it in applications only: Libraries should import textfsm2 directly (or alias it locally) instead of mutating process-wide imports for their callers.

Use CliTable with an index file

CliTable maps command names and device attributes (such as Platform) to TextFSM template files using an index CSV file. See the upstream CliTable wiki for the full specification.

Create templates/index with Template first, optional metadata columns in the middle, and Command last:

Template, Platform, Command
cisco_ios_show_version.textfsm, cisco_ios, sh[[ow]] ver[[sion]]
cisco_ios_show_interfaces.textfsm, cisco_ios, sh[[ow]] int[[erfaces]]

Square brackets like sh[[ow]] expand into optional abbreviation matches so sh ver and show version both match the same row.

Query the index and parse CLI output with textfsm2.clitable:

from textfsm2 import clitable

cli_table = clitable.CliTable("index", "templates")
cli_table.ParseCmd(raw_output, {"Platform": "cisco_ios", "Command": "sh ver"})

print(cli_table.header)
for row in cli_table:
    print(dict(row))