https://github.com/gruns/icecream Skip to content Sign up Sign up * Why GitHub? Features - + Mobile - + Actions - + Codespaces - + Packages - + Security - + Code review - + Project management - + Integrations - + GitHub Sponsors - + Customer stories- * Team * Enterprise * Explore + Explore GitHub - Learn and contribute + Topics - + Collections - + Trending - + Learning Lab - + Open source guides - Connect with others + The ReadME Project - + Events - + Community forum - + GitHub Education - + GitHub Stars program - * Marketplace * Pricing Plans - + Compare plans - + Contact Sales - + Education - [ ] [search-key] * # In this repository All GitHub | Jump to | * No suggested jump to results * # In this repository All GitHub | Jump to | * # In this user All GitHub | Jump to | * # In this repository All GitHub | Jump to | Sign in Sign up Sign up {{ message }} gruns / icecream * Notifications * Star 3.4k * Fork 68 Never use print() to debug again. MIT License 3.4k stars 68 forks Star Notifications * Code * Issues 14 * Pull requests 1 * Actions * Projects 0 * Security * Insights More * Code * Issues * Pull requests * Actions * Projects * Security * Insights master Switch branches/tags [ ] Branches Tags Nothing to show {{ refName }} default View all branches Nothing to show {{ refName }} default View all tags 1 branch 3 tags Go to file Code Clone HTTPS GitHub CLI [https://github.com/g] Use Git or checkout with SVN using the web URL. [gh repo clone gruns/] Work fast with our official CLI. Learn more. * Open with GitHub Desktop * Download ZIP Launching GitHub Desktop If nothing happens, download GitHub Desktop and try again. Go back Launching GitHub Desktop If nothing happens, download GitHub Desktop and try again. Go back Launching Xcode If nothing happens, download Xcode and try again. Go back Launching Visual Studio If nothing happens, download the GitHub extension for Visual Studio and try again. Go back Latest commit @gruns gruns switch to more apt emdash ... a2c8f5b Mar 10, 2021 switch to more apt emdash a2c8f5b Git stats * 185 commits Files Permalink Failed to load latest commit information. Type Name Latest commit message Commit time icecream icecream v2.1.0 Jan 27, 2021 tests determine whether an argument and value are the same with ast.literal... Jan 27, 2021 .gitignore The future of print debugging. Feb 13, 2018 .travis.yml Drop support for Python 3.4 Oct 25, 2019 LICENSE.txt The future of print debugging. Feb 13, 2018 MANIFEST.in Include README.md in source distributions. May 15, 2019 README.md switch to more apt emdash Mar 10, 2021 changelog.txt add newline to homogenize whitespace Jan 27, 2021 logo.svg Rename icon.svg to the more apposite logo.svg. Aug 28, 2018 setup.cfg Publish both a source distribution and a Wheel to PyPi. Sep 3, 2018 setup.py Update slogan. Aug 31, 2020 tox.ini Drop support for Python 3.4 Oct 25, 2019 View code IceCream -- Never use print() to debug again Inspect Variables Inspect Execution Return Value Miscellaneous Import Tricks Configuration Installation IceCream in Other Languages README.md icecream [6874747073] [6874747073] [6874747073] [6874747073] IceCream -- Never use print() to debug again Do you ever use print() or log() to debug your code? Of course you do. IceCream, or ic for short, makes print debugging a little sweeter. IceCream is well tested, permissively licensed, and supports Python 2, Python 3, PyPy2, and PyPy3. Inspect Variables Have you ever printed variables or expressions to debug your program? If you've ever typed something like print(foo('123')) or the more thorough print("foo('123')", foo('123')) then ic() is here to help. With arguments, ic() inspects itself and prints both its own arguments and the values of those arguments. from icecream import ic def foo(i): return i + 333 ic(foo(123)) Prints ic| foo(123): 456 Similarly, d = {'key': {1: 'one'}} ic(d['key'][1]) class klass(): attr = 'yep' ic(klass.attr) Prints ic| d['key'][1]: 'one' ic| klass.attr: 'yep' Just give ic() a variable or expression and you're done. Easy. Inspect Execution Have you ever used print() to determine which parts of your program are executed, and in which order they're executed? For example, if you've ever added print statements to debug code like def foo(): print(0) first() if expression: print(1) second() else: print(2) third() then ic() helps here, too. Without arguments, ic() inspects itself and prints the calling filename, line number, and parent function. from icecream import ic def foo(): ic() first() if expression: ic() second() else: ic() third() Prints ic| example.py:4 in foo() ic| example.py:11 in foo() Just call ic() and you're done. Simple. Return Value ic() returns its argument(s), so ic() can easily be inserted into pre-existing code. >>> a = 6 >>> def half(i): >>> return i / 2 >>> b = half(ic(a)) ic| a: 6 >>> ic(b) ic| b: 3 Miscellaneous ic.format(*args) is like ic() but the output is returned as a string instead of written to stderr. >>> from icecream import ic >>> s = 'sup' >>> out = ic.format(s) >>> print(out) ic| s: 'sup' Additionally, ic()'s output can be entirely disabled, and later re-enabled, with ic.disable() and ic.enable() respectively. from icecream import ic ic(1) ic.disable() ic(2) ic.enable() ic(3) Prints ic| 1: 1 ic| 3: 3 ic() continues to return its arguments when disabled, of course; no existing code with ic() breaks. Import Tricks To make ic() available in every file without needing to be imported in every file, you can install() it. For example, in a root A.py: #!/usr/bin/env python3 # -*- coding: utf-8 -*- from icecream import install install() from B import foo foo() and then in B.py, which is imported by A.py, just call ic(): # -*- coding: utf-8 -*- def foo(): x = 3 ic(x) install() adds ic() to the builtins module, which is shared amongst all files imported by the interpreter. Similarly, ic() can later be uninstall()ed, too. ic() can also be imported in a manner that fails gracefully if IceCream isn't installed, like in production environments (i.e. not development). To that end, this fallback import snippet may prove useful: try: from icecream import ic except ImportError: # Graceful fallback if IceCream isn't installed. ic = lambda *a: None if not a else (a[0] if len(a) == 1 else a) # noqa Configuration ic.configureOutput(prefix, outputFunction, argToStringFunction, includeContext) can be used to adopt a custom output prefix (the default is ic| ), change the output function (default is to write to stderr), customize how arguments are serialized to strings, and/or include the ic() call's context (filename, line number, and parent function) in ic() output with arguments. >>> from icecream import ic >>> ic.configureOutput(prefix='hello -> ') >>> ic('world') hello -> 'world' prefix can optionally be a function, too. >>> import time >>> from icecream import ic >>> >>> def unixTimestamp(): >>> return '%i |> ' % int(time.time()) >>> >>> ic.configureOutput(prefix=unixTimestamp) >>> ic('world') 1519185860 |> 'world': 'world' outputFunction, if provided, is called with ic()'s output instead of that output being written to stderr (the default). >>> import logging >>> from icecream import ic >>> >>> def warn(s): >>> logging.warning(s) >>> >>> ic.configureOutput(outputFunction=warn) >>> ic('eep') WARNING:root:ic| 'eep': 'eep' argToStringFunction, if provided, is called with argument values to be serialized to displayable strings. The default is PrettyPrint's pprint.pformat(), but this can be changed to, for example, handle non-standard datatypes in a custom fashion. >>> from icecream import ic >>> >>> def toString(obj): >>> if isinstance(obj, str): >>> return '[!string %r with length %i!]' % (obj, len(obj)) >>> return repr(obj) >>> >>> ic.configureOutput(argToStringFunction=toString) >>> ic(7, 'hello') ic| 7: 7, 'hello': [!string 'hello' with length 5!] includeContext, if provided and True, adds the ic() call's filename, line number, and parent function to ic()'s output. >>> from icecream import ic >>> ic.configureOutput(includeContext=True) >>> >>> def foo(): >>> ic('str') >>> foo() ic| example.py:12 in foo()- 'str': 'str' includeContext is False by default. Installation Installing IceCream with pip is easy. $ pip install icecream IceCream in Other Languages IceCream should be enjoyed with every language. * Dart: icecream * Rust: icecream-rs * Node.js: node-icecream * C++: IceCream-Cpp * PHP: icecream-php * Go: icecream-go * Ruby: Ricecream * Java: icecream-java If you'd like a similar ic() function in your favorite language, please open a pull request! IceCream's goal is to sweeten print debugging with a handy-dandy ic() function in every language. About Never use print() to debug again. Topics python debugging library python3 debug print inspects debugging-tool Resources Readme License MIT License Releases 3 IceCream v2.0.0. Latest Oct 26, 2019 + 2 releases Packages 0 No packages published Contributors 11 * @gruns * @alexmojaki * @ss18 * @nodai2hITC * @renatoGarcia * @miggaiowski * @baopham * @ntzm * @Akshay-Thakare * @jmerle * @chang Languages * Python 100.0% * (c) 2021 GitHub, Inc. * Terms * Privacy * Security * Status * Docs * Contact GitHub * Pricing * API * Training * Blog * About You can't perform that action at this time. You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.