{"id":13501439,"url":"https://github.com/andy-landy/traceback_with_variables","last_synced_at":"2025-05-14T00:06:02.523Z","repository":{"id":44196182,"uuid":"247317137","full_name":"andy-landy/traceback_with_variables","owner":"andy-landy","description":"Adds variables to python traceback. Simple, lightweight, controllable. Debug reasons of exceptions by logging or pretty printing colorful variable contexts for each frame in a stacktrace, showing every value. Dump locals environments after errors to console, files, and loggers. Works in Jupyter and IPython. Install with pip or conda.","archived":false,"fork":false,"pushed_at":"2025-02-21T02:00:19.000Z","size":620,"stargazers_count":695,"open_issues_count":8,"forks_count":26,"subscribers_count":5,"default_branch":"master","last_synced_at":"2025-04-10T02:16:39.221Z","etag":null,"topics":["arguments","colors","debugging","dump","error-handling","errors","exception-handling","exceptions","frame","jupyter","locals","logging","pretty","pretty-print","print","python","python3","stacktrace","traceback","variables"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/andy-landy.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-03-14T16:57:34.000Z","updated_at":"2025-04-04T15:55:23.000Z","dependencies_parsed_at":"2024-11-19T01:19:22.745Z","dependency_job_id":"9a1c8b7a-abb6-4427-ad0f-d6b3c592c761","html_url":"https://github.com/andy-landy/traceback_with_variables","commit_stats":{"total_commits":68,"total_committers":4,"mean_commits":17.0,"dds":"0.13235294117647056","last_synced_commit":"ee9890726960c24c2d77a5d2a18c9759b657bd76"},"previous_names":[],"tags_count":9,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/andy-landy%2Ftraceback_with_variables","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/andy-landy%2Ftraceback_with_variables/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/andy-landy%2Ftraceback_with_variables/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/andy-landy%2Ftraceback_with_variables/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/andy-landy","download_url":"https://codeload.github.com/andy-landy/traceback_with_variables/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254043456,"owners_count":22004956,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["arguments","colors","debugging","dump","error-handling","errors","exception-handling","exceptions","frame","jupyter","locals","logging","pretty","pretty-print","print","python","python3","stacktrace","traceback","variables"],"created_at":"2024-07-31T22:01:37.567Z","updated_at":"2025-05-14T00:06:02.488Z","avatar_url":"https://github.com/andy-landy.png","language":"Python","readme":"![Example](https://raw.githubusercontent.com/andy-landy/traceback_with_variables/master/header.png)\n\n\u003ch2 align=\"center\"\u003ePython Traceback (Error Message) Printing Variables\u003c/h2\u003e\n\u003cp align=\"center\"\u003eVery simple to use and fast, but versatile when needed. Try for debug and keep for small-scale production.\u003c/p\u003e\n\n\u003cp align=\"center\"\u003e\n\u003ca href=\"https://github.com/andy-landy/traceback_with_variables/actions\"\u003e\u003cimg alt=\"Actions Status\" src=\"https://github.com/andy-landy/traceback_with_variables/workflows/tests/badge.svg\"\u003e\u003c/a\u003e\n\u003ca href=\"https://github.com/andy-landy/traceback_with_variables/blob/master/.github/workflows/master-test.yml#L59\"\u003e\u003cimg title=\"code tests coverage is 100%\" alt=\"code tests coverage is 100%\" src=\"https://img.shields.io/badge/coverage-100%25-brightgreen.svg\"\u003e\u003c/a\u003e\n\u003ca href=\"https://github.com/andy-landy/traceback_with_variables/tree/master/LICENSE\"\u003e\u003cimg alt=\"License: MIT\" src=\"https://img.shields.io/github/license/andy-landy/traceback_with_variables?color=informational\"\u003e\u003c/a\u003e\n\u003ca href=\"https://pepy.tech/project/traceback-with-variables\"\u003e\u003cimg alt=\"Downloads\" src=\"https://static.pepy.tech/badge/traceback-with-variables\"\u003e\u003c/a\u003e\n\u003ca href=\"https://pypi.org/project/traceback-with-variables\"\u003e\u003cimg alt=\"PyPI\" src=\"https://img.shields.io/pypi/v/traceback-with-variables\"\u003e\u003c/a\u003e\n\u003ca href=\"https://anaconda.org/conda-forge/traceback-with-variables\"\u003e\u003cimg src=\"https://img.shields.io/conda/vn/conda-forge/traceback-with-variables\"\u003e\u003c/a\u003e\n\u003ca href=\"https://pypi.org/project/traceback-with-variables\"\u003e\u003cimg alt=\"PyPI\" src=\"https://img.shields.io/badge/python-3.6+-blue.svg\"\u003e\u003c/a\u003e\n\u003ca href=\"https://superuser.com/questions/413073/windows-console-with-ansi-colors-handling\"\u003e\u003cimg title=\"windows, mac, linux etc.\" alt=\"Platform\" src=\"https://img.shields.io/badge/platform-all-blue.svg\"\u003e\u003c/a\u003e\n\u003ca href=\"https://github.com/andy-landy/traceback_with_variables/tree/master/traceback_with_variables/override.py#L9\"\u003e\u003cimg title=\"type hints everywhere\" alt=\"Annotations coverage\" src=\"https://img.shields.io/badge/type--hints-100%25-blueviolet.svg\"\u003e\u003c/a\u003e\n\u003ca href=\"https://github.com/andy-landy/traceback_with_variables/tree/master/traceback_with_variables/override.py#L8\"\u003e\u003cimg title=\"no obscure objects, only transparent functions and data classes\" alt=\"No-OOP\" src=\"https://img.shields.io/badge/no OOP-100%25-blueviolet.svg\"\u003e\u003c/a\u003e\n\u003ca href=\"https://github.com/andy-landy/traceback_with_variables/tree/master/setup.py\"\u003e\u003cimg alt=\"Dependencies\" src=\"https://img.shields.io/badge/dependencies-0-blueviolet.svg\"\u003e\u003c/a\u003e\n\u003ca href=\"https://colab.research.google.com/github/andy-landy/traceback_with_variables/blob/master/examples/colab.ipynb\"\u003e\u003cimg src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/\u003e\u003c/a\u003e\n\u003ca href=\"https://gitter.im/andy-landy/traceback-with-variables\"\u003e\u003cimg alt=\"Gitter\" src=\"https://img.shields.io/gitter/room/andy-landy/traceback-with-variables?color=blueviolet\"\u003e\u003c/a\u003e\n\u003c!--\n\u003ca href=\"https://pypi.org/project/traceback-with-variables\"\u003e\u003cimg alt=\"pip Downloads\" src=\"https://static.pepy.tech/personalized-badge/traceback-with-variables?period=total\u0026units=none\u0026left_color=grey\u0026right_color=orange\u0026left_text=pip downloads\"\u003e\u003c/a\u003e\n\u003ca href=\"https://anaconda.org/conda-forge/traceback-with-variables/\"\u003e\u003cimg alt=\"conda-forge Downloads\" src=\"https://img.shields.io/conda/dn/conda-forge/traceback-with-variables?color=orange\u0026label=conda%20downloads\"\u003e\u003c/a\u003e\n--\u003e\n\u003c/p\u003e\n\u003cbr/\u003e\n\n\u003e “It is useless work that darkens the heart.” \n\u003e \u003cem\u003e– Ursula K. Le Guin\u003c/em\u003e\n\nTired of useless job of putting all your variables into debug exception messages? Just stop it. \nAutomate it and clean your code. Once and for all.\n\n---\n\n_Contents:_ **[Installation](#installation)** | **[🚀 Quick Start](#-quick-start)**\n| **[Colors](#colors)**\n| **[How does it save my time?](#how-does-it-save-my-time)** | \n**[Examples and recipes](#examples-and-recipes)** | **[Reference](#reference)**\n| **[FAQ](#faq)**\n\n---\n\n\u003e :warning: **I'm open to update this module to meet new use cases and to make using it easier and fun**: so any proposal or advice or warning is very welcome and will be taken into account of course. When I started it I wanted to make a tool meeting all standard use cases. I think in this particular domain this is rather achievable, so I'll try. Note `next_version` branch also. Have fun!\n\n---\n\n### Installation\n\n```\npip install traceback-with-variables==2.2.0\n```\n```\nconda install -c conda-forge traceback-with-variables\n```\n\nto use shorter `tb` alias in interactive mode call this once:\n```\npython3 -c 'from traceback_with_variables.tb_alias import create_tb_alias as c; c()'\n```\n\n### 🚀 Quick Start\n\nUsing without code editing, \u003ca href=\"https://github.com/andy-landy/traceback_with_variables/tree/master/examples/external_script.sh\"\u003erunning your script/command/module\u003c/a\u003e:\n```\ntraceback-with-variables tested_script.py ...srcipt's args...\n```\n\n\u003ca href=\"https://github.com/andy-landy/traceback_with_variables/tree/master/examples/simple.py\"\u003eSimplest usage\u003c/a\u003e, for the whole program:\n```python\n    from traceback_with_variables import activate_by_import\n```\nor just (if you added an alias by the above command)\n```python\n    import tb.a\n```\n\n\u003ca href=\"https://github.com/andy-landy/traceback_with_variables/tree/master/examples/print_for_function.py\"\u003eDecorator\u003c/a\u003e, for a single function:\n```python\n    @prints_exc\n    # def main(): or def some_func(...):\n```\n\n\u003ca href=\"https://github.com/andy-landy/traceback_with_variables/tree/master/examples/print_for_code_block.py\"\u003eContext\u003c/a\u003e, for a single code block:\n```python\n    with printing_exc():\n```\n\n\u003ca href=\"https://github.com/andy-landy/traceback_with_variables/tree/master/examples/work_with_traceback_lines.py\"\u003eWork with traceback lines\u003c/a\u003e in a custom manner:\n```python\n    lines = list(iter_exc_lines(e))\n```\n\n\u003ca href=\"https://github.com/andy-landy/traceback_with_variables/tree/master/examples/print_cur_tb.py\"\u003eNo exception but you want to print the stack anyway?\u003c/a\u003e:\n```python\n    print_cur_tb()\n```\n\nUsing a logger [\u003ca href=\"https://github.com/andy-landy/traceback_with_variables/tree/master/examples/log_for_function.py\"\u003ewith a decorator\u003c/a\u003e, \u003ca href=\"https://github.com/andy-landy/traceback_with_variables/tree/master/examples/log_for_code_block.py\"\u003ewith a context\u003c/a\u003e]:\n```python\n    with printing_exc(file_=LoggerAsFile(logger)):\n    # or\n    @prints_exc(file_=LoggerAsFile(logger)): \n```\n\n\u003ca href=\"https://github.com/andy-landy/traceback_with_variables/tree/master/examples/print_traceback.py\"\u003ePrint traceback\u003c/a\u003e in interactive mode after an exception:\n```\n    \u003e\u003e\u003e print_exc()\n```\n\n\u003ca href=\"https://github.com/andy-landy/traceback_with_variables/tree/master/examples/format_customized.py\"\u003eCustomize\u003c/a\u003e any of the previous examples:\n```python\n    from traceback_with_variables import fmt\n    fmt.max_value_str_len = 10000\n    fmt.skip_files_except = 'my_project'\n    fmt.custom_var_printers = []  # show all variables, no skips, no hides\n```\n\n### Colors\n\n![Example](https://raw.githubusercontent.com/andy-landy/traceback_with_variables/master/color_schemes.png)\n\n\n### How does it save my time?\n\n* Turn a code totally covered by debug formatting from this:\n\n    ```diff\n      def main():\n          sizes_str = sys.argv[1]\n          h1, w1, h2, w2 = map(int, sizes_str.split())\n    -     try:\n              return get_avg_ratio([h1, w1], [h2, w2])\n    -     except:\n    -         logger.error(f'something happened :(, variables = {locals()[:1000]}')\n    -         raise\n    -         # or\n    -         raise MyToolException(f'something happened :(, variables = {locals()[:1000]}')\n              \n      def get_avg_ratio(size1, size2):\n    -     try:\n              return mean(get_ratio(h, w) for h, w in [size1, size2])\n    -     except:\n    -         logger.error(f'something happened :(, size1 = {size1}, size2 = {size2}')\n    -         raise\n    -         # or\n    -         raise MyToolException(f'something happened :(, size1 = {size1}, size2 = {size2}')\n    \n      def get_ratio(height, width):\n    -     try:\n              return height / width\n    -     except:\n    -         logger.error(f'something happened :(, width = {width}, height = {height}')\n    -         raise\n    -         # or\n    -         raise MyToolException(f'something happened :(, width = {width}, height = {height}')\n    ```\n    into this (zero debug code):\n    \n    ```diff\n    + from traceback_with_variables import activate_by_import\n    \n      def main():\n          sizes_str = sys.argv[1]\n          h1, w1, h2, w2 = map(int, sizes_str.split())\n          return get_avg_ratio([h1, w1], [h2, w2])\n              \n      def get_avg_ratio(size1, size2):\n          return mean(get_ratio(h, w) for h, w in [size1, size2])\n    \n      def get_ratio(height, width):\n          return height / width\n    ```\n    \n    And obtain total debug info spending 0 lines of code:\n    \n    ```\n    Traceback with variables (most recent call last):\n      File \"./temp.py\", line 7, in main\n        return get_avg_ratio([h1, w1], [h2, w2])\n          sizes_str = '300 200 300 0'\n          h1 = 300\n          w1 = 200\n          h2 = 300\n          w2 = 0\n      File \"./temp.py\", line 10, in get_avg_ratio\n        return mean([get_ratio(h, w) for h, w in [size1, size2]])\n          size1 = [300, 200]\n          size2 = [300, 0]\n      File \"./temp.py\", line 10, in \u003clistcomp\u003e\n        return mean([get_ratio(h, w) for h, w in [size1, size2]])\n          .0 = \u003ctuple_iterator object at 0x7ff61e35b820\u003e\n          h = 300\n          w = 0\n      File \"./temp.py\", line 13, in get_ratio\n        return height / width\n          height = 300\n          width = 0\n    builtins.ZeroDivisionError: division by zero\n    ```\n\n* Automate your logging too:\n    \n    ```python\n    logger = logging.getLogger('main')\n    \n    def main():\n        ...\n        with printing_exc(file_=LoggerAsFile(logger))\n            ...\n    ```\n    \n    ```\n    2020-03-30 18:24:31 main ERROR Traceback with variables (most recent call last):\n    2020-03-30 18:24:31 main ERROR   File \"./temp.py\", line 7, in main\n    2020-03-30 18:24:31 main ERROR     return get_avg_ratio([h1, w1], [h2, w2])\n    2020-03-30 18:24:31 main ERROR       sizes_str = '300 200 300 0'\n    2020-03-30 18:24:31 main ERROR       h1 = 300\n    2020-03-30 18:24:31 main ERROR       w1 = 200\n    2020-03-30 18:24:31 main ERROR       h2 = 300\n    2020-03-30 18:24:31 main ERROR       w2 = 0\n    2020-03-30 18:24:31 main ERROR   File \"./temp.py\", line 10, in get_avg_ratio\n    2020-03-30 18:24:31 main ERROR     return mean([get_ratio(h, w) for h, w in [size1, size2]])\n    2020-03-30 18:24:31 main ERROR       size1 = [300, 200]\n    2020-03-30 18:24:31 main ERROR       size2 = [300, 0]\n    2020-03-30 18:24:31 main ERROR   File \"./temp.py\", line 10, in \u003clistcomp\u003e\n    2020-03-30 18:24:31 main ERROR     return mean([get_ratio(h, w) for h, w in [size1, size2]])\n    2020-03-30 18:24:31 main ERROR       .0 = \u003ctuple_iterator object at 0x7ff412acb820\u003e\n    2020-03-30 18:24:31 main ERROR       h = 300\n    2020-03-30 18:24:31 main ERROR       w = 0\n    2020-03-30 18:24:31 main ERROR   File \"./temp.py\", line 13, in get_ratio\n    2020-03-30 18:24:31 main ERROR     return height / width\n    2020-03-30 18:24:31 main ERROR       height = 300\n    2020-03-30 18:24:31 main ERROR       width = 0\n    2020-03-30 18:24:31 main ERROR builtins.ZeroDivisionError: division by zero\n    ```\n\n* Free your exceptions of unnecessary information load:\n    \n    ```python\n    def make_a_cake(sugar, eggs, milk, flour, salt, water):\n        is_sweet = sugar \u003e salt\n        is_vegan = not (eggs or milk)\n        is_huge = (sugar + eggs + milk + flour + salt + water \u003e 10000)\n        if not (is_sweet or is_vegan or is_huge):\n            raise ValueError('This is unacceptable, look why!')\n        ...\n    ```\n\n* — Should I use it after debugging is over, i.e. *in production*?\n\n    Yes, of course! That way it might save you even more time (\u003ca href=\"https://github.com/andy-landy/traceback_with_variables/tree/master/examples/secret_data.py\"\u003ewatch out\u003c/a\u003e for sensitive data\n    like 🔴**passwords** and **tokens** in you logs). Note: you can deploy more serious monitoring frameworks, e.g. `Sentry`\n\n\u003cbr/\u003e\n\n* Stop this tedious practice in production:\n   \n     step 1: Notice some exception in a production service. \\\n     step 2: Add more printouts, logging, and exception messages. \\\n     step 3: Rerun the service. \\\n     step 4: Wait till (hopefully) the bug repeats. \\\n     step 5: Examine the printouts and possibly add some more info (then go back to step 2). \\\n     step 6: Erase all recently added printouts, logging and exception messages. \\\n     step 7: Go back to step 1 once bugs appear.\n\n\n* — Is it slow? I have huge lists and dicts and data objects!\n\n    Data size doesn't matter! Printing is smart, so only the resulting suffix and prefix are generated, e.g.\n\n    `list(range(100000000))`   becomes just   `\"[0, 1, 2, ...9998, 99999999]\"`   in 0.00012s\n\n    Use `fmt`'s `.max_value_str_len`, and `.ellipsis_rel_pos` (0.0 to 1.0) to tweak the output.\n \n\n### Examples and recipes\n\n* \u003ca href=\"https://github.com/andy-landy/traceback_with_variables/tree/master/examples/external_script.sh\"\u003erun python code without changes: a script, a module, a commnad\u003c/a\u003e\n* \u003ca href=\"https://github.com/andy-landy/traceback_with_variables/tree/master/examples/simple.py\"\u003esimple usage\u003c/a\u003e\n* \u003ca href=\"https://github.com/andy-landy/traceback_with_variables/tree/master/examples/simple_jupyter.py\"\u003esimple usage in Jupyter or IPython\u003c/a\u003e\n* \u003ca href=\"https://github.com/andy-landy/traceback_with_variables/tree/master/examples/print_cur_tb.py\"\u003eprint current stack, when there's no exception\u003c/a\u003e\n* \u003ca href=\"https://github.com/andy-landy/traceback_with_variables/tree/master/examples/interactive_print_last_exception.py\"\u003eprint last exception in Python console\u003c/a\u003e\n* \u003ca href=\"https://github.com/andy-landy/traceback_with_variables/tree/master/examples/change_global_printer.py\"\u003emanually change global printer\u003c/a\u003e\n* \u003ca href=\"https://github.com/andy-landy/traceback_with_variables/tree/master/examples/change_global_printer_in_jupyter.py\"\u003emanually change global printer in Jupyter or IPython\u003c/a\u003e\n* \u003ca href=\"https://github.com/andy-landy/traceback_with_variables/tree/master/examples/print_for_function.py\"\u003eworking with a function\u003c/a\u003e\n* \u003ca href=\"https://github.com/andy-landy/traceback_with_variables/tree/master/examples/log_for_function.py\"\u003eworking with a function, logging\u003c/a\u003e\n* \u003ca href=\"https://github.com/andy-landy/traceback_with_variables/tree/master/examples/print_for_code_block.py\"\u003eworking with a code block\u003c/a\u003e\n* \u003ca href=\"https://github.com/andy-landy/traceback_with_variables/tree/master/examples/log_for_code_block.py\"\u003eworking with a code block, logging\u003c/a\u003e\n* \u003ca href=\"https://github.com/andy-landy/traceback_with_variables/tree/master/examples/work_with_traceback_lines.py\"\u003eget traceback lines for custom things\u003c/a\u003e\n* \u003ca href=\"https://github.com/andy-landy/traceback_with_variables/tree/master/examples/flask.py\"\u003eusing with `flask`\u003c/a\u003e\n* \u003ca href=\"https://github.com/andy-landy/traceback_with_variables/tree/master/examples/format_customized.py\"\u003ecustomize the output\u003c/a\u003e\n* \u003ca href=\"https://github.com/andy-landy/traceback_with_variables/tree/master/examples/secret_data.py\"\u003epasswords and tokens in logs\u003c/a\u003e\n\n### Reference\n\n#### All functions have `fmt=` argument, a `Format` object with fields:\n* `max_value_str_len` max length of each variable string, -1 to disable, default=1000\n* `objects_details` depth of details of objects inspection\n* `ellipsis_rel_pos` when truncating long strings where to put the \"...\", from 0.0 to 1.0, default=0.7\n* `max_exc_str_len` max length of exception, should variable print fail, -1 to disable, default=10000\n* `before` number of code lines before the raising line, default=0\n* `after` number of code lines after the raising line, default=0\n* `ellipsis_` string to denote long strings truncation, default=`...`\n* `skip_files_except` use to print only certain files; list of regexes, ignored if empty, default=None\n* `brief_files_except` use to print variables only in certain files; list of regexes, ignored if empty, default=None\n* `custom_var_printers` list of pairs of (filter, printer); filter is a name fragment, a type or a function or a list thereof; printer returns `None` to skip a var, standard ones are `hide`, `skip`, `show`\n* `color_scheme` is `None` or one of \u003ca href=\"https://github.com/andy-landy/traceback_with_variables/tree/master/traceback_with_variables/color.py\"\u003e`ColorSchemes`\u003c/a\u003e: `.none` , `.common`, `.nice`, `.synthwave`. `None` is for auto-detect \n\n---\n\n#### \u003ca href=\"https://github.com/andy-landy/traceback_with_variables/tree/master/traceback_with_variables/activate_by_import.py\"\u003e`activate_by_import`\u003c/a\u003e\nJust import it. No arguments, for real quick use in regular Python.\n```python\nfrom traceback_with_variables import activate_by_import\n```\n\n---\n\n#### \u003ca href=\"https://github.com/andy-landy/traceback_with_variables/tree/master/traceback_with_variables/activate_in_ipython_by_import.py\"\u003e`activate_in_ipython_by_import`\u003c/a\u003e\nJust import it. No arguments, for real quick use in Jupyter or IPython.\n```python\nfrom traceback_with_variables import activate_in_ipython_by_import\n```\n\n---\n\n#### \u003ca href=\"https://github.com/andy-landy/traceback_with_variables/tree/master/traceback_with_variables/global_hooks.py\"\u003e`global_print_exc`\u003c/a\u003e\nCall once in the beginning of your program, to change how traceback after an uncaught exception looks.\n```python\ndef main():\n    override_print_exc(...)\n```\n\n---\n\n#### \u003ca href=\"https://github.com/andy-landy/traceback_with_variables/tree/master/traceback_with_variables/global_hooks.py\"\u003e`global_print_exc_in_ipython`\u003c/a\u003e\nCall once in the beginning of your program, to change how traceback after an uncaught exception looks.\n```python\ndef main():\n    override_print_exc(...)\n```\n\n---\n\n#### \u003ca href=\"https://github.com/andy-landy/traceback_with_variables/tree/master/traceback_with_variables/print.py\"\u003e`print_exc`\u003c/a\u003e\nPrints traceback for a given/current/last (first being not `None` in the priority list) exception to a file, default=`sys.stderr`. Convenient for manual console or Jupyter sessions or custom try/except blocks. Note that it can be called with a given exception value or it can auto discover current exception in an `except:` block or it can auto descover last exception value (long) after `try/catch` block.\n```python\nprint_exc()\n```\n\n---\n\n#### \u003ca href=\"https://github.com/andy-landy/traceback_with_variables/tree/master/traceback_with_variables/print.py\"\u003e`print_cur_tb`\u003c/a\u003e\nPrints current traceback when no exception is raised.\n```python\nprint_cur_tb()\n```\n\n---\n\n#### \u003ca href=\"https://github.com/andy-landy/traceback_with_variables/tree/master/traceback_with_variables/print.py\"\u003e`prints_exc`\u003c/a\u003e\nFunction decorator, used for logging or simple printing of scoped tracebacks with variables. I.e. traceback is shorter as it includes only frames inside the function call. Program exiting due to unhandled exception still prints a usual traceback.\n```python\n@prints_exc\ndef f(...):\n\n@prints_exc(...)\ndef f(...):\n```\n\n---\n\n#### \u003ca href=\"https://github.com/andy-landy/traceback_with_variables/tree/master/traceback_with_variables/print.py\"\u003e`printing_exc`\u003c/a\u003e\nContext manager (i.e. `with ...`), used for logging or simple printing of scoped tracebacks with variables. I.e. traceback is shorter as it includes only frames inside the `with` scope. Program exiting due to unhandled exception still prints a usual traceback.\n```python\nwith printing_exc(...):\n```\n\n---\n\n#### \u003ca href=\"https://github.com/andy-landy/traceback_with_variables/tree/master/traceback_with_variables/print.py\"\u003e`LoggerAsFile`\u003c/a\u003e\nA logger-to-file wrapper, to pass a logger to print tools as a file.\n\n---\n\n#### \u003ca href=\"https://github.com/andy-landy/traceback_with_variables/tree/master/traceback_with_variables/core.py\"\u003e`iter_exc_lines`\u003c/a\u003e\nIterates the lines, which are usually printed one-by-one in terminal.\n\n---\n\n#### \u003ca href=\"https://github.com/andy-landy/traceback_with_variables/tree/master/traceback_with_variables/core.py\"\u003e`format_exc`\u003c/a\u003e\nLike `iter_exc_lines` but returns a single string.\n\n---\n\n#### \u003ca href=\"https://github.com/andy-landy/traceback_with_variables/tree/master/traceback_with_variables/core.py\"\u003e`iter_cur_tb_lines`\u003c/a\u003e\nLike `iter_exc_lines` but doesn't need an exception and prints upper frames..\n\n---\n\n#### \u003ca href=\"https://github.com/andy-landy/traceback_with_variables/tree/master/traceback_with_variables/core.py\"\u003e`format_cur_tb`\u003c/a\u003e\nLike `iter_cur_tb_lines` but returns a single string.\n\n---\n\n### FAQ\n\n* In Windows console crash messages have no colors. \n\n    The default Windows console/terminal cannot print [so called *ansi*] colors, but this is \n    \u003ca href=\"https://superuser.com/questions/413073/windows-console-with-ansi-colors-handling\"\u003efixable\u003c/a\u003e\n    , especially with modern Windows versions. Therefore colors are disabled by default,\n    but you can enable them and check if it works in your case.\n    You can force enable colors by passing `--color-scheme common` (for complete list of colors pass `--help`) console argument.\n\n* Windows console prints junk symbols when colors are enabled.\n\n    The default Windows console/terminal cannot print [so called *ansi*] colors, but this is \n    \u003ca href=\"https://superuser.com/questions/413073/windows-console-with-ansi-colors-handling\"\u003efixable\u003c/a\u003e\n    , especially with modern Windows versions. If for some reason the colors are wrongly enabled by default,\n    you can force disable colors by passing `--color-scheme none` console argument.\n    \n* Bash tools like grep sometimes fail to digest the output when used with pipes (`|`) because of colors.\n\n    Please disable colors by passing `--color-scheme none` console argument.\n    The choice for keeping colors in piped output was made to allow convenient usage of `head`, `tail`, file redirection etc.\n    In cases like `| grep` it might have issues, in which case you can disable colors.\n    \n* Output redirected to a file in `\u003e output.txt` manner has no colors when I `cat` it.\n\n    This is considered a rare use case, so colors are disabled by default when outputting to a file.\n    But you can force enable colors by passing `--color-scheme common` (for complete list of colors pass `--help`) console argument.\n  \n* `activate_by_import` or `global_print_exc` don't work in Jupyter or IPython as if not called at all.\n\n    In Jupyter or IPython you should use `activate_in_ipython_by_import` or `global_print_exc_in_ipython`. IPython handles exceptions differently than regular Python.  \n\n* The server framework (`flask`, `streamlit` etc.) still shows usual tracebacks.\n\n    In such frameworks tracebacks are printed not while exiting the program (the program continues running), hence you should override exception handling in a manner\n    proper for the given framework. Please address the `flask` example.\n  \n* How do I reduce output? I don't need all files or all variables.\n\n    Use `skip_files_except`, `brief_files_except`, `custom_var_printers` to cut excess output.\n\n* I have ideas about good colors.\n\n    Please fork, add a new `ColorScheme` to \u003ca href=\"https://github.com/andy-landy/traceback_with_variables/tree/master/traceback_with_variables/color.py\"\u003e`ColorSchemes`\u003c/a\u003e\n    and create a Pull Request to `next_version` branch. \u003ca href=\"https://en.wikipedia.org/wiki/ANSI_escape_code\"\u003eChoose the color codes\u003c/a\u003e and visually\n    test it like `python3 -m traceback_with_variables.main --color-scheme {its name} examples/for_readme_image.py`. \n\n* My code doesn't work.  \n\n    Please \u003ca href=\"https://gitter.im/andy-landy/traceback-with-variables\"\u003epost your case\u003c/a\u003e. You are very welcome!\n    \n* Other questions or requests to elaborate answers.\n\n    Please \u003ca href=\"https://gitter.im/andy-landy/traceback-with-variables\"\u003epost your question or request\u003c/a\u003e. You are very welcome!\n","funding_links":[],"categories":["Python"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fandy-landy%2Ftraceback_with_variables","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fandy-landy%2Ftraceback_with_variables","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fandy-landy%2Ftraceback_with_variables/lists"}