{"library":"cookiecutter","code":"from cookiecutter.main import cookiecutter\nimport os\nimport shutil\n\n# Define a temporary output directory\noutput_dir = './my_new_project_temp'\n\n# Define a simple local template for demonstration\ntemplate_path = './my_template'\n\n# Create a simple template for demonstration purposes\n# In a real scenario, you'd clone from a Git URL or use a local template.\nif not os.path.exists(template_path):\n    os.makedirs(os.path.join(template_path, '{{ cookiecutter.repo_name }}'))\n    with open(os.path.join(template_path, 'cookiecutter.json'), 'w') as f:\n        f.write('{\"repo_name\": \"my-project\"}')\n    with open(os.path.join(template_path, '{{ cookiecutter.repo_name }}', 'README.md'), 'w') as f:\n        f.write('Hello, {{ cookiecutter.repo_name }}!')\n\nprint(f\"Generating project from template: {template_path}\")\n# Run cookiecutter programmatically\n# This will prompt for 'repo_name' unless `no_input=True` is used\n# or default_context is provided.\n# For a runnable quickstart, we'll provide default_context and no_input.\n\ntry:\n    cookiecutter(\n        template_path,\n        no_input=True, # Set to False to be prompted\n        extra_context={'repo_name': 'my-generated-project'},\n        output_dir=output_dir,\n        overwrite_if_exists=True\n    )\n    print(f\"Project successfully generated in {os.path.join(output_dir, 'my-generated-project')}\")\n    # Verify a file exists\n    generated_file = os.path.join(output_dir, 'my-generated-project', 'README.md')\n    if os.path.exists(generated_file):\n        with open(generated_file, 'r') as f:\n            print(f\"Content of {generated_file}:\\n{f.read()}\")\n    else:\n        print(\"Error: Generated file not found.\")\nexcept Exception as e:\n    print(f\"An error occurred: {e}\")\nfinally:\n    # Clean up the temporary template and generated project\n    if os.path.exists(template_path):\n        shutil.rmtree(template_path)\n    if os.path.exists(output_dir):\n        shutil.rmtree(output_dir)\n    print(\"Cleanup complete.\")\n","lang":"python","description":"This example demonstrates how to use `cookiecutter` programmatically. It first creates a minimal local template, then generates a project from it, providing `extra_context` to avoid interactive prompts, and finally cleans up the generated files and template.","tag":null,"tag_description":null,"last_tested":"2026-04-24","results":[{"runtime":"python:3.10-alpine","exit_code":0},{"runtime":"python:3.10-slim","exit_code":0},{"runtime":"python:3.11-alpine","exit_code":0},{"runtime":"python:3.11-slim","exit_code":0},{"runtime":"python:3.12-alpine","exit_code":0},{"runtime":"python:3.12-slim","exit_code":0},{"runtime":"python:3.13-alpine","exit_code":0},{"runtime":"python:3.13-slim","exit_code":0},{"runtime":"python:3.9-alpine","exit_code":0},{"runtime":"python:3.9-slim","exit_code":0}]}