【问题标题】:ipython: how to set terminal widthipython:如何设置终端宽度
【发布时间】:2014-08-30 14:28:11
【问题描述】:

当我使用 ipython terminal 并想打印一个有很多列的 numpy.ndarray 时,这些行会在大约 80 个字符的地方自动断开(即行的宽度是 cca 80 个字符):

z = zeros((2,20))
print z

大概,ipython 期望我的终端有 80 列。然而事实上,我的终端有 176 个字符的宽度,我想使用全宽。

我曾尝试更改以下参数,但没有效果:

c.PlainTextFormatter.max_width = 160

我如何告诉ipython 使用我的终端的全宽?

我在 Debian Wheezy 上使用 ipython 1.2.1

【问题讨论】:

  • 顺便说一下,这不是 iPython 特有的,因此您可能需要更新标签以反映这一点。该行为特定于 numpy,无论您使用 iPython 还是任何其他 Python 解释器,该行为都是相同的。
  • 1980 年代被称为。他们希望他们的终端宽度回来。 :(

标签: python numpy ipython


【解决方案1】:

您可以使用

查看当前的线宽
numpy.get_printoptions()['linewidth']

并设置它

numpy.set_printoptions(linewidth=160)

自动设置打印宽度

如果您希望自动设置终端宽度,可以让 Python 执行启动脚本。因此,创建一个文件 ~/.python_startup.py 或任何您想调用的文件,其中包含以下内容:

# Set the printing width to the current terminal width for NumPy.
#
# Note: if you change the terminal's width after starting Python,
#       it will not update the printing width.
from os import getenv
terminal_width = getenv('COLUMNS')
try:
    terminal_width = int(terminal_width)
except (ValueError, TypeError):
    print('Sorry, I was unable to read your COLUMNS environment variable')
    terminal_width = None

if terminal_width is not None and terminal_width > 0:
    from numpy import set_printoptions
    set_printoptions(linewidth = terminal_width)

del terminal_width

要让 Python 每次都执行此操作,请打开您的 ~/.bashrc 文件,然后添加

# Instruct Python to execute a start up script
export PYTHONSTARTUP=$HOME/.python_startup.py
# Ensure that the startup script will be able to access COLUMNS
export COLUMNS

【讨论】:

  • 我认为这应该是公认的答案。虽然@Dolda2000 提供的答案有效,但这种方法更干净、更简单。设置 Python 启动脚本的额外信息也很有用。
【解决方案2】:

在对代码进行一些挖掘之后,您要查找的变量似乎是numpy.core.arrayprint._line_width,默认情况下为 75。将其设置为 160 对我有用:

>>> numpy.zeros((2, 20))
array([[ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0., 0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.]])

默认情况下用于数组格式化的函数是numpy.core.numeric.array_repr,尽管您可以使用numpy.core.numeric.set_string_function 更改它。

【讨论】:

  • 我的$HOME/.config/ipython/profile_default/ipython_config.py 中没有numpy.core.arrayprint._line_width。我应该只在那儿做广告,还是numpy 在其他地方有它自己的配置文件?
  • @MartinVegter:我不是在谈论配置文件,我怀疑numpy 甚至有一个。它是一个普通的 Python 变量,你可以在导入 numpy 后执行 numpy.core.arrayprint._line_width = 160 简单地设置它。如果您想以某种方式使其成为默认值,那么恐怕我无法帮助您,因为我不知道 iPython 如何执行其启动顺序以及它可能运行或可能不运行的 rc 文件类型。根据您的设置,您可能需要考虑直接编辑numpy 的源代码。
  • 如果你提到的这个$HOME/.config/ipython/profile_default/ipython_config.py是一个Python文件,iPython只是在启动时执行,你可以考虑把import numpy然后numpy.core.arrayprint._line_width = 160放进去。
  • 当我在开始使用from numpy import * 时导入numpy 时,如何访问numpy.core.arrayprint._line_width?变量numpy.core.arrayprint._line_width 不存在。
  • 变量_line_width 似乎不存在(np.__version__ == '1.15.1')。现在是 _format_options["linewidth"](默认为 75)
【解决方案3】:

要在您的窗口大小发生变化时自动调整 numpy 和 IPython 的大小,请将以下内容添加到您的 ipython_config.py

import IPython
import signal
import shutil
import sys
try:
    import numpy as np
except ImportError:
    pass

c = get_config()

def update_terminal_width(*ignored):
    """Resize the IPython and numpy printing width to match the terminal."""
    w, h = shutil.get_terminal_size()

    config = IPython.get_ipython().config
    config.PlainTextFormatter.max_width = w - 1
    shell = IPython.core.interactiveshell.InteractiveShell.instance()
    shell.init_display_formatter()

    if 'numpy' in sys.modules:
        import numpy as np
        np.set_printoptions(linewidth=w - 5)

# We need to configure IPython here differently because get_ipython() does not
# yet exist.
w, h = shutil.get_terminal_size()
c.PlainTextFormatter.max_width = w - 1
if 'numpy' in sys.modules:
    import numpy as np
    np.set_printoptions(linewidth=w - 5)

signal.signal(signal.SIGWINCH, update_terminal_width)

如果您想延迟加载 numpy 直到需要,请查看 Post import hooks in Python 3 以获取解决方案。

如果您不使用 IPython,请将上述内容放入您的 PYTHONSTARTUP 文件并删除 IPython 特定的行。

【讨论】:

  • 对象c是什么?
  • @wim 感谢您指出这一点。已修复(get_config())。
  • 这很好,但似乎只适用于 Python 3.3 或更高版本。早期版本似乎没有 shutil.get_terminal_size()
【解决方案4】:

使用这个方法:

np.set_printoptions(linewidth=300)

并使用此扩展您的 iphyton 视图:

from IPython.core.display import display, HTML
display(HTML("<style>.container { width:95% !important; }</style>"))

【讨论】:

    猜你喜欢
    • 2015-03-04
    • 2019-03-07
    • 1970-01-01
    • 2015-06-17
    • 2012-01-21
    • 2018-10-02
    • 1970-01-01
    • 2010-12-19
    相关资源
    最近更新 更多