Skip to content

Open-source Python library for AI browser agents, plus Browser Use Cloud: stealth browsers and a hosted agent API.

Make init command work without browser-use[cli] extras

Reagan Hsudd1fca43d771
diff --git a/browser_use/cli.py b/browser_use/cli.py
index 1058caa98..528970c1d 100644
--- a/browser_use/cli.py
+++ b/browser_use/cli.py
@@ -35,6 +35,15 @@ if len(sys.argv) > 1 and sys.argv[1] == 'install':
sys.exit(1)
sys.exit(0)
+# Check for init subcommand early to avoid loading TUI dependencies
+if 'init' in sys.argv:
+ from browser_use.init_cmd import main as init_main
+
+ # Remove 'init' from sys.argv so click doesn't see it as an unexpected argument
+ sys.argv.remove('init')
+ init_main()
+ sys.exit(0)
+
import asyncio
import json
import logging
diff --git a/browser_use/init_cmd.py b/browser_use/init_cmd.py
new file mode 100644
index 000000000..a57504189
--- /dev/null
+++ b/browser_use/init_cmd.py
@@ -0,0 +1,160 @@
+"""
+Standalone init command for browser-use template generation.
+
+This module provides a minimal command-line interface for generating
+browser-use templates without requiring heavy TUI dependencies.
+"""
+
+import sys
+from pathlib import Path
+
+import click
+
+# Template metadata
+INIT_TEMPLATES = {
+ 'default': {
+ 'file': 'default_template.py',
+ 'description': 'Simplest setup - capable of any web task with minimal configuration',
+ },
+ 'advanced': {
+ 'file': 'advanced_template.py',
+ 'description': 'All configuration options shown with defaults',
+ },
+ 'tools': {
+ 'file': 'tools_template.py',
+ 'description': 'Custom action examples - extend the agent with your own functions',
+ },
+}
+
+
+def _write_init_file(output_path: Path, content: str, force: bool = False) -> bool:
+ """Write content to a file, with safety checks."""
+ # Check if file already exists
+ if output_path.exists() and not force:
+ click.echo(f'⚠️ File already exists: {output_path}')
+ if not click.confirm('Overwrite?', default=False):
+ click.echo('❌ Cancelled')
+ return False
+
+ # Ensure parent directory exists
+ output_path.parent.mkdir(parents=True, exist_ok=True)
+
+ # Write file
+ try:
+ output_path.write_text(content, encoding='utf-8')
+ return True
+ except Exception as e:
+ click.echo(f'❌ Error writing file: {e}', err=True)
+ return False
+
+
[email protected]('browser-use-init')
+ '--template',
+ '-t',
+ type=click.Choice(['default', 'advanced', 'tools'], case_sensitive=False),
+ help='Template to use',
+)
+ '--output',
+ '-o',
+ type=click.Path(),
+ help='Output file path (default: browser_use_<template>.py)',
+)
+ '--force',
+ '-f',
+ is_flag=True,
+ help='Overwrite existing files without asking',
+)
+ '--list',
+ '-l',
+ 'list_templates',
+ is_flag=True,
+ help='List available templates',
+)
+def main(
+ template: str | None,
+ output: str | None,
+ force: bool,
+ list_templates: bool,
+):
+ """
+ Generate a browser-use template file to get started quickly.
+
+ Examples:
+
+ \b
+ # Interactive mode - prompts for template selection
+ uvx browser-use init
+
+ \b
+ # Generate default template
+ uvx browser-use init --template default
+
+ \b
+ # Generate advanced template with custom filename
+ uvx browser-use init --template advanced --output my_script.py
+
+ \b
+ # List available templates
+ uvx browser-use init --list
+ """
+
+ # Handle --list flag
+ if list_templates:
+ click.echo('Available templates:\n')
+ for name, info in INIT_TEMPLATES.items():
+ click.echo(f' {name:12} - {info["description"]}')
+ return
+
+ # Interactive template selection if not provided
+ if not template:
+ click.echo('Available templates:\n')
+ for name, info in INIT_TEMPLATES.items():
+ click.echo(f' {name:12} - {info["description"]}')
+ click.echo()
+
+ template = click.prompt(
+ 'Which template would you like to use?',
+ type=click.Choice(['default', 'advanced', 'tools'], case_sensitive=False),
+ default='default',
+ )
+
+ # Template is guaranteed to be set at this point (either from option or prompt)
+ assert template is not None
+
+ # Determine output path
+ if output:
+ output_path = Path(output)
+ else:
+ output_path = Path.cwd() / f'browser_use_{template}.py'
+
+ # Read template file
+ try:
+ templates_dir = Path(__file__).parent / 'cli_templates'
+ template_file = INIT_TEMPLATES[template]['file']
+ template_path = templates_dir / template_file
+ content = template_path.read_text(encoding='utf-8')
+ except Exception as e:
+ click.echo(f'❌ Error reading template: {e}', err=True)
+ sys.exit(1)
+
+ # Write file
+ if _write_init_file(output_path, content, force):
+ click.echo(f'✅ Created {output_path}')
+ click.echo('\nNext steps:')
+ click.echo(' 1. Install browser-use:')
+ click.echo(' uv pip install browser-use')
+ click.echo(' 2. Set up your API key in .env file or environment:')
+ click.echo(' BROWSER_USE_API_KEY=your-key')
+ click.echo(' (Get your key at https://cloud.browser-use.com/dashboard/api)')
+ click.echo(' 3. Run your script:')
+ click.echo(f' python {output_path.name}')
+ else:
+ sys.exit(1)
+
+
+if __name__ == '__main__':
+ main()
diff --git a/pyproject.toml b/pyproject.toml
index f4ccf118f..76c2cd111 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -14,6 +14,7 @@ dependencies = [
"aiohttp==3.12.15",
"anyio>=4.9.0",
"bubus>=1.5.6",
+ "click>=8.1.8",
"google-api-core>=2.25.0",
"httpx>=0.28.1",
"portalocker>=2.7.0,<3.0.0",
@@ -58,7 +59,6 @@ dependencies = [
[project.optional-dependencies]
cli = [
"rich>=14.0.0",
- "click>=8.1.8",
"textual>=3.2.0",
]
code = [