{
  "source_file": "0930",
  "source_platform": "Dev.to",
  "author": "Bala Priya C",
  "title_listed": "Python Fire: How To Create CLIs from ANY Python Object",
  "keywords": "",
  "domain": "数据处理",
  "content_type": "深度技术解析",
  "is_paid_flag": "否",
  "url": "https://dev.to/balapriya/python-fire-67i",
  "publish_date": "2021-04-23",
  "corpus_stage": "",
  "notes": "",
  "id": "9e10420d7e39",
  "collected_at": "2026-10-02T14:01:40",
  "title": "Python Fire: How To Create CLIs from ANY Python Object - DEV Community",
  "body_markdown": "This blog post summarizes the use cases of Python Fire, explained by\nDavid Bieber\n, a Software Engineer at Google Brain in a webinar organized by Women Who Code Python Track.\nHello, Python Fire!\nFire is a Python library that can create a Command-line Interface from absolutely any Python object, created for the purpose of unifying scripting, testing, and operations for complex systems. It’s named “Fire” as it instantly fires off (executes) our command. It can be of great help in the following workflows:\nCreating Command-line interfaces\nDeveloping and Debugging Python code\nExploring third-party Python projects\nLet’s install fire and look at the generic pattern to use fire on the Python programs/functions that we create.\npip\ninstall\nfire\nimport\nfire\nfire\n.\nFire\n(\nfunction_name\n)\nEnter fullscreen mode\nExit fullscreen mode\nCreating CLIs\nLet’s call fire on our very own “Hello World!” program.\nimport\nfire\ndef\nhello\n(\nname\n=\n\"\nworld\n\"\n):\nreturn\n\"\nHello\n\"\n+\nname\n+\n\"\n!\n\"\nif\n__name__\n==\n'\n__main__\n'\n:\nfire\n.\nFire\n(\nhello\n)\n# Fire now creates a CLI for our program as shown below.\n>\nhello\n# Hello world!\n>\nhello\n@My_Name\n# Hello @My_Name!\n>\nhello\n--\nname\n=\nWWC\n# Hello WWC!\nEnter fullscreen mode\nExit fullscreen mode\nWhat does calling Fire do?\nFire consumes the arguments and uses them to\nInstantiate a class\nCall a function\nGet a member or property\nAnd, it repeats the process until all the arguments have been exhausted. After completion, the resulting component is serialized in a human-readable way.\nTo reiterate, we can call fire.Fire with any Python class, object, function, module, dict and so on!\nUsing Multiple Commands\nLet’s see a simple Calculator example, that contains methods add and multiply which each takes two arguments and computes sum and product respectively.\nimport\nfire\nclass\nCalculator\n(\nobject\n):\ndef\nadd\n(\nself\n,\nx\n,\ny\n):\nreturn\nx\n+\ny\ndef\nmultiply\n(\nself\n,\nx\n,\ny\n):\nreturn\nx\n*\ny\nif\n__name__\n==\n'\n__main__\n'\n:\nfire\n.\nFire\n(\nCalculator\n)\nEnter fullscreen mode\nExit fullscreen mode\nAs expected Fire creates a CLI from which we should be able to run multiple commands including inspecting the class, add and multiply functions.\n> calculator add 1 2 # 3\n> calculator multiply 3 4 # 12\n> calculator\nUsage: calculator add\n       calculator multiply\nEnter fullscreen mode\nExit fullscreen mode\nUsing Hierarchical Commands\nIn the following code snippet, we have a Tools class, which when instantiated would in turn instantiate the Calculator and Widget classes.\nimport\nfire\nclass\nTools\n(\nobject\n):\ndef\n__init__\n(\nself\n):\nself\n.\ncalculator\n=\nCalculator\n()\nself\n.\nwidget\n=\nWidget\n()\nif\n__name__\n==\n'\n__main__\n'\n:\nfire\n.\nFire\n(\nTools\n)\n# The CLI that Fire generates looks allows us to access the respective classes and class methods\n>\ntools\ncalculator\nadd\n1\n2\n# 3\n>\ntools\ncalculator\nmultiply\n3\n4\n# 12\n>\ntools\nUsage\n:\ntools\ncalculator\ntools\nwidget\nEnter fullscreen mode\nExit fullscreen mode\nDebugging and Development\nPython Fire library is indeed very helpful in debugging and development where we can just go ahead and call\nfire.Fire()\non any Python script without any argument and use in the command-line.\nThere’s also an option to use the ‘\ninteractive\n’ flag which as shown below, imports useful objects and allows us to directly use them from the IPython shell without having to import modules, function calls etc.\n>\ncalc\n--\n--\ninteractive\nFire\nis\nstarting\nPython\nREPL\nwith\nthe\nfollowing\nobjects\n:\nModules\n:\nfire\nObjects\n:\nCalculator\n,\ncalc\n,\ncomponent\n,\nresult\n,\ntrace\nIPython\n--\nAn\nenhanced\nInteractive\nPython\n[\n1\n]:\ncalc\n.\nadd\n(\n1\n,\n2\n)\n[\n2\n]:\ncalc\n.\nmultiply\n(\n3\n,\n4\n)\nEnter fullscreen mode\nExit fullscreen mode\nExploring Third-Party Python Projects\nAs with above-explained examples, we might as well call\nfire.Fire()\non third party modules whose source code we may not necessarily have access to. In the latest release of fire, we can directly call\npython -m fire\nfrom the command-line\nLet’s walk through an example with the image processing library PIL. Suppose we have a Python script\nimagefire.py\nwith the following lines and we want to open a\ngiraffe.png\nimage and perform some operations on the image.\nfrom\nPIL\nimport\nImage\nimport\nfire\nfire\n.\nFire\n(\nImage\n)\n>\nimagefire\n.\npy\nopen\ngiraffe\n.\npng\n-\nconvert\nL\n-\nresize\n100\n,\n100\n-\nsave\ngiraffe_out\n.\npng\nEnter fullscreen mode\nExit fullscreen mode\nThe above code opens the image, converts it to grayscale, resizes it to the specified size, and then saves the resulting image under the name\ngiraffe_out.png\n. The recent release of fire allows us to do the same all the more easily!\n# NEW release of Fire\n>\npython\n-\nm\nfire\nPIL\n.\nImage\nopen\ngiraffe\n.\npng\n-\nconvert\nL\n-\nresize\n100\n,\n100\n-\nsave\ngiraffe_out\n.\npng\nEnter fullscreen mode\nExit fullscreen mode\nReferences\nIf you’re looking to delve deeper into Python Fire, the following are some of the resources\n[1]\nhttps://google.github.io/python-fire/guide/\n[2]\nhttps://github.com/google/python-fire\nCover Image: Photo by\nRoland Larsson\non\nUnsplash",
  "metadata": {
    "title": "Python Fire: How To Create CLIs from ANY Python Object - DEV Community",
    "description": "This blog post summarizes the use cases of Python Fire, explained by David Bieber, a Software... Tagged with python.",
    "keywords": "python, software, coding, development, engineering, inclusive, community",
    "author": null,
    "og:type": "article",
    "og:url": "https://dev.to/balapriya/python-fire-67i",
    "og:title": "Python Fire: How To Create CLIs from ANY Python Object",
    "og:description": "This blog post summarizes the use cases of Python Fire, explained by David Bieber, a Software...",
    "og:site_name": "DEV Community",
    "og:image": "https://media2.dev.to/dynamic/image/width=1000,height=500,fit=cover,gravity=auto,format=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fs58jscthwzikuo6i4ujl.jpg",
    "twitter:site": "@thepracticaldev",
    "twitter:creator": "@",
    "twitter:title": "Python Fire: How To Create CLIs from ANY Python Object",
    "twitter:description": "This blog post summarizes the use cases of Python Fire, explained by David Bieber, a Software...",
    "twitter:card": "summary_large_image",
    "twitter:widgets:new-embed-design": "on",
    "twitter:image:src": "https://media2.dev.to/dynamic/image/width=1000,height=500,fit=cover,gravity=auto,format=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fs58jscthwzikuo6i4ujl.jpg"
  },
  "paywall": {
    "listed_paid": false,
    "detected": false,
    "marker": "",
    "public_only": false,
    "policy": "付费/会员内容只保留页面公开可见部分，不绕过付费墙"
  },
  "crawl": {
    "success": true,
    "status_code": 200,
    "error": ""
  },
  "code_blocks": [
    {
      "file": "attachments/9e10420d7e39/code/body-000.txt",
      "chars": 54,
      "language": "text"
    },
    {
      "file": "attachments/9e10420d7e39/code/body-001.txt",
      "chars": 278,
      "language": "text"
    },
    {
      "file": "attachments/9e10420d7e39/code/body-002.txt",
      "chars": 189,
      "language": "text"
    },
    {
      "file": "attachments/9e10420d7e39/code/body-003.txt",
      "chars": 118,
      "language": "text"
    },
    {
      "file": "attachments/9e10420d7e39/code/body-004.txt",
      "chars": 400,
      "language": "text"
    },
    {
      "file": "attachments/9e10420d7e39/code/body-005.txt",
      "chars": 232,
      "language": "text"
    },
    {
      "file": "attachments/9e10420d7e39/code/body-006.txt",
      "chars": 135,
      "language": "text"
    },
    {
      "file": "attachments/9e10420d7e39/code/body-007.txt",
      "chars": 118,
      "language": "text"
    }
  ],
  "images": [],
  "audio": [],
  "videos": [],
  "links": [
    {
      "href": "https://www.linkedin.com/in/davidjbieber",
      "label": "David Bieber",
      "type": "a"
    },
    {
      "href": "https://google.github.io/python-fire/guide/",
      "label": "https://google.github.io/python-fire/guide/",
      "type": "a"
    },
    {
      "href": "https://github.com/google/python-fire",
      "label": "https://github.com/google/python-fire",
      "type": "a"
    },
    {
      "href": "https://unsplash.com/@becomingsilence?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText",
      "label": "Roland Larsson",
      "type": "a"
    },
    {
      "href": "https://unsplash.com/s/photos/fire-abstract?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText",
      "label": "Unsplash",
      "type": "a"
    }
  ],
  "repair_version": 2,
  "repaired_at": "2026-10-14T10:49:12Z",
  "source_url": "https://dev.to/balapriya/python-fire-67i",
  "papers": [],
  "actual_video_files": [],
  "body_chars": 4968,
  "expected_body_images": 0,
  "discovery_status": "ok",
  "delivery_status": "ok",
  "missing_reason": "",
  "raw_html": "attachments/9e10420d7e39/pages/source.html",
  "local_html": "attachments/9e10420d7e39/pages/article.html",
  "body_base_path": "attachments/9e10420d7e39/pages/",
  "body_markdown_path": "attachments/9e10420d7e39/pages/article.md"
}