ipython:如何设置终端宽度

Mar*_*ter 26 python numpy ipython

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

z = zeros((2,20))
print z
Run Code Online (Sandbox Code Playgroud)

据推测,ipython期望我的终端有80列.事实上,我的终端宽度为176个字符,我想使用全宽.

我试过更改以下参数,但这没有效果:

c.PlainTextFormatter.max_width = 160
Run Code Online (Sandbox Code Playgroud)

如何判断ipython我的终端的全宽?

ipython 1.2.1在Debian Wheezy上使用

Gar*_*ett 29

你可以看到你当前的线宽

numpy.get_printoptions()['linewidth']
Run Code Online (Sandbox Code Playgroud)

并设置它

numpy.set_printoptions(linewidth=160)
Run Code Online (Sandbox Code Playgroud)

自动设置打印宽度

如果您希望自动设置终端宽度,可以让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
Run Code Online (Sandbox Code Playgroud)

并让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
Run Code Online (Sandbox Code Playgroud)

  • 我认为这应该是公认的答案。虽然@Dolda2000 提供的答案有效,但这种方法更清晰、更简单。关于设置 Python 启动脚本的额外信息也很有用。 (2认同)

Dol*_*000 20

在对代码进行一些挖掘之后,看起来您正在寻找的变量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.]])
Run Code Online (Sandbox Code Playgroud)

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

  • 变量`_line_width`似乎不存在(`np .__ version__ =='1.15.1'`).它现在是`_format_options ["linewidth"]`(默认为75) (6认同)
  • 我的 `$HOME/.config/ipython/profile_default/ipython_config.py` 中没有 `numpy.core.arrayprint._line_width`。我应该在那里添加该行,还是`numpy` 在其他地方有自己的配置文件? (2认同)

Mar*_*ato 7

要在窗口大小发生变化时自动调整 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)
Run Code Online (Sandbox Code Playgroud)

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

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