项目概述
🍦 Never use print() to debug again.
项目地址
https://github.com/gruns/icecream
项目页面预览
关键指标
- Stars:9991
- 主要语言:Python
- License:MIT License
- 最近更新:2026-01-14T08:15:51Z
- 默认分支:master
本站高速下载(国内可用)
当前未生成本站压缩包(稍后重试)。
安装部署要点(README 精选)
Configuration
ic.configureOutput(prefix, outputFunction, argToStringFunction, controls
includeContext, contextAbsPath)ic()‘s output.
prefix, if provided, adopts a custom output prefix. prefix can be a
string, like
>>> from icecream import ic
>>> ic.configureOutput(prefix='hello -> ')
>>> ic('world')
hello -> 'world'
or a function.
>>> import time
>>> from icecream import ic
>>>
>>> def unixTimestamp():
>>> return '%i |> ' % int(time.time())
>>>
>>> ic.configureOutput(prefix=unixTimestamp)
>>> ic('world')
1519185860 |> 'world': 'world'
prefix‘s default value is ic|.
outputFunction, if provided, is called once for every ic() call with
ic()‘s output, as a string, instead of that string being written to
stderr (the default).
>>> import logging
>>> from icecream import ic
>>>
>>> def warn(s):
>>> logging.warning("%s", 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!]
The default argToStringFunction is icecream.argumentToString, and
has methods to register and unregister functions to be dispatched
for specific classes using functools.singledispatch. It also has a
registry property to view registered functions.
>>> from icecream import ic, argumentToString
>>> import numpy as np
>>>
>>> # Register a function to summarize numpy array
>>> @argumentToString.register(np.ndarray)
>>> def _(obj):
>>> return f"ndarray, shape={obj.shape}, dtype={obj.dtype}"
>>>
>>> x = np.zeros((1, 2))
>>> ic(x)
ic| x: ndarray, shape=(1, 2), dtype=float64
>>>
>>> # View registered functions
>>> argumentToString.registry
mappingproxy({object: <function icecream.icecream.argumentToString(obj)>,
numpy.ndarray: <function __main__._(obj)>})
>>>
>>> # Unregister a function and fallback to the default behavior
>>> argumentToString.unregister(np.ndarray)
>>> ic(x)
ic| x: array([[0., 0.]])
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():
>>> i = 3
>>> ic(i)
>>> foo()
ic| example.py:12 in foo()- i: 3
includeContext is False by default.
contextAbsPath, if provided and True, outputs absolute filepaths, like
/path/to/foo.py, over just filenames, like foo.py, when ic() is
called with includeContext == True. This is useful when debugging
multiple files that share the same filename(s). Moreover, some editors,
like VSCode, turn absolute filepaths into clickable links that open the
file where ic() was called.
>>> from icecream import ic
>>> ic.configureOutput(includeContext=True, contextAbsPath=True)
>>>
>>> i = 3
>>>
>>> def foo():
>>> ic(i)
>>> foo()
ic| /absolute/path/to/example.py:12 in foo()- i: 3
>>>
>>> ic.configureOutput(includeContext=True, contextAbsPath=False)
>>>
>>> def foo():
>>> ic(i)
>>> foo()
ic| example.py:18 in foo()- i: 3
contextAbsPath is False by default.
If you want to use icecream with multiple log levels, like with Python’s
logging module, you can use ic.format() to integrate icecream’s
debugging with your logger:
import logging
from icecream import ic
foo = 'bar'
logging.debug(ic.format(foo))
❕ This is a bit clunky. Would you prefer built-in log level support in
icecream? If so, please share your thoughts in
issue.
Installation
Installing IceCream with pip is easy.
$ pip install icecream
常用命令(从 README 提取)
ic| foo(123): 456
ic| d['key'][1]: 'one'
ic| klass.attr: 'yep'
ic| example.py:4 in foo()
ic| example.py:11 in foo()
通用部署说明
- 下载源码并阅读 README
- 安装依赖(pip/npm/yarn 等)
- 配置环境变量(API Key、模型路径、数据库等)
- 启动服务并测试访问
- 上线建议:Nginx 反代 + HTTPS + 进程守护(systemd / pm2)
免责声明与版权说明
本文仅做开源项目整理与教程索引,源码版权归原作者所有,请遵循对应 License 合规使用。







暂无评论内容