Registry / devops / knack
library0.14.0pypypi✓ verified 25d ago

Knack is a lightweight Python framework for building command-line interfaces (CLIs) with a focus on ease of use and extensibility. It provides common CLI features like command grouping, argument parsing, help generation, and output formatting. The current version is 0.13.0, with new releases typically tied to Python version support and bug fixes, occurring a few times a year.

pip install knack
INSTALL
IMPORT
SIG · KNACK
K
knack
devopspythonv0.14.0
Install
2.6s avg
Import
100ms
Disk
30MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v0.14.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.104s · 30.8MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 2.6s · import 0.096s · 32MB
30MB installed
● package 30MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

CLI
from knack.cli import CLI
CLICommandsLoader
from knack.commands import CLICommandsLoader
from knack.cli import CLICommandsLoader
CLICommandsLoader is part of the 'commands' module, not 'cli'.

This quickstart demonstrates how to set up a basic Knack CLI with a single command group and command. It defines a command loader, registers a simple 'hello' command, and then invokes the CLI programmatically. Save this as a Python file and run it directly to see the output.

import sys from knack.cli import CLI from knack.commands import CLICommandsLoader # Define a simple command handler def hello_command_handler(): """Says hello.""" print("Hello from Knack CLI!") return 0 # Define a custom commands loader class MyCommandsLoader(CLICommandsLoader): def load_command_table(self, args): # Define a command group 'greet' and a command 'hello' within it with self.command_group('greet', operations_tmpl='{}#{{}}'.format(__name__)) as g: g.command('hello', 'hello_command_handler') return self.command_table # Create the CLI instance mycli = CLI(cli_name='mycli', commands_loader_cls=MyCommandsLoader) if __name__ == '__main__': # Example of invoking the CLI programmatically. # For actual command-line use, you'd typically pass sys.argv[1:]. # To run this example: python your_script.py print("\n--- Invoking 'mycli greet hello' ---") exit_code = mycli.invoke(['greet', 'hello']) print(f"CLI exited with code: {exit_code}") print("\n--- Invoking 'mycli --help' ---") exit_code = mycli.invoke(['--help']) print(f"CLI exited with code: {exit_code}")
knack --version
Debug
Known issues
breakingKnack frequently drops support for older Python versions. Ensure your environment uses a currently supported Python version.
fix
Upgrade to a supported Python version. For example, v0.13.0 dropped Python 3.8 support; v0.11.0 dropped 3.7; v0.10.0 dropped 3.6. Always check the release notes for the target Knack version for exact Python compatibility.
affects: v0.10.0 and above
breakingThe internal handling of `bool` default values for arguments changed, which might affect argument parsing logic.
fix
Prior to v0.11.0, `bool` default values were implicitly converted to `DefaultInt`. This conversion was stopped. If your code relied on this previous, potentially incorrect, behavior for argument default values, verify your argument parsing logic, especially for boolean flags.
affects: v0.11.0
gotchaThe `colorama` dependency, used for ANSI escape sequences, became conditionally installed only on Windows.
fix
If your cross-platform project relies on `colorama` being available on non-Windows systems (e.g., for direct import and usage), you will need to explicitly add `colorama` to your project's dependencies for those platforms, as Knack no longer installs it universally.
affects: v0.9.0 and above
breakingThe `command_group` context manager, previously used for defining command groups within `CommandsLoader`, was removed.
fix
Update your `CommandsLoader` implementation to remove usage of the `command_group` context manager. Instead, register commands directly by populating `self.command_table` or `self.command_groups` within `load_command_table`, or utilize the `command_group_from_module` pattern if using module-based command loading. Consult Knack's release notes or updated examples for the specific version you are targeting.
affects: v0.9.0 and above
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'knack'
The 'knack' library is not installed in your current Python environment or the environment where the script is being executed.
fix
Install the library using pip: `pip install knack`
ModuleNotFoundError: No module named 'knack.experimental'
This error typically occurs when there's an incompatibility or a missing submodule, often after an upgrade of a tool (like azdev) that uses `knack` internally, or if the `knack` installation is corrupted or a specific version with this module is not active.
fix
Try reinstalling the `knack` library or the dependent package (e.g., `azdev`) to ensure all submodules and dependencies are correctly installed and compatible. For `azdev`, specifically, a common fix is to manually uninstall older `knack` versions and reinstall the correct one (e.g., `pip install knack=={version}`).
TypeError: args should be a list or tuple.
This error arises when programmatically invoking a `knack` command using `cli.invoke()` and the `args` parameter is not provided as a list or a tuple.
fix
Ensure the arguments passed to `cli.invoke()` are enclosed in a list or tuple. For example, `cli.invoke(['my', 'command', '--option', 'value'])` instead of `cli.invoke('my command --option value')`.
TypeError: ScenarioTest.__init__() missing 1 required positional argument: 'method_name'
This error indicates an incorrect invocation of the `ScenarioTest` class constructor, likely within a test setup for a CLI built with Knack, where the `method_name` argument is expected but not provided.
fix
When initializing `ScenarioTest`, ensure you pass the `method_name` argument. This usually happens automatically when using standard Python `unittest` framework with `knack`'s testing utilities, so review your test class and inheritance. For example: `class MyTests(ScenarioTest): def test_my_command(self): ...` (where `method_name` would be 'test_my_command').
Upgrade
Version history
0.14.0latest on PyPI · released May 14, 2026
Audit
Dependencies
coloramaoptionalUsed for ANSI escape sequence support on Windows terminals. Conditionally installed only on Windows.
Agent activity
12 hits · last 30 days
node
10
Amazon
1
Resources