10

My Python 3.5.2 output in the terminal (on a mac) is limited to a width of ca. 80px, even if I increase the size of the terminal window.

This narrow width causes a bunch of line breaks when outputting long arrays which is really a hassle. How do I tell python to use the full command line window width?

For the record, i am not seeing this problem in any other program, for instance my c++ output looks just fine.

4 Answers 4

9

For numpy, it turns out you can enable the full output by setting

np.set_printoptions(suppress=True,linewidth=np.nan,threshold=np.nan).

Sign up to request clarification or add additional context in comments.

1 Comment

In Python 3.7 and numpy 1.16.2, this fails with ValueError: threshold must be numeric and non-NAN, try sys.maxsize for untruncated representation. However, numpy.set_printoptions(suppress=True,linewidth=numpy.nan) works!
7

In Python 3.7 and above, you can use

from shutil import get_terminal_size
pd.set_option('display.width', get_terminal_size()[0])

1 Comment

Is pd from import pandas as pd? Why? The question does not mention pandas at all. Is it needed to solve the problem? BTW shutil.get_terminal_size() is exactly what I was searching for. --- Link to the documentation: docs.python.org/3/library/shutil.html#shutil.get_terminal_size
3

I have the same problem while using pandas. So if this is what you are trying to solve, I fixed mine by doing pd.set_option('display.width', pd.util.terminal.get_terminal_size()[0])

1 Comment

Doesn't work in Python 3.7 with Pandas 0.23.3. Fails with AttributeError: module 'pandas.util' has no attribute 'terminal'
1

Default output of a 2x15 matrix is broken:

a.T
array([[ 0.2, -1.4, -0.8,  1.3, -1.5, -1.4,  0.6, -1.5,  0.4, -0.9,  0.3,
         1.1,  0.5, -0.3,  1.1],
       [ 1.3, -1.2,  1.6, -1.4,  0.9, -1.2, -1.9,  0.9,  1.8, -1.8,  1.7,
        -1.3,  1.4, -1.7, -1.3]])

Output is fixed using numpy set_printoptions() command

import sys
np.set_printoptions(suppress=True,linewidth=sys.maxsize,threshold=sys.maxsize)
a.T
[[ 0.2 -1.4 -0.8  1.3 -1.5 -1.4  0.6 -1.5  0.4 -0.9  0.3  1.1  0.5 -0.3  1.1]
 [ 1.3 -1.2  1.6 -1.4  0.9 -1.2 -1.9  0.9  1.8 -1.8  1.7 -1.3  1.4 -1.7 -1.3]]

System and numpy versions:

sys.version =  3.8.3 (default, Jul  2 2020, 17:30:36) [MSC v.1916 64 bit (AMD64)]
numpy.__version__ =  1.18.5

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.