Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pycharm os.get_terminal_size() not working

Python 3.7.1 on Ubuntu 18.04.2 LTS

Using Pycharm version:

PyCharm 2019.1.3 (Professional Edition)
Build #PY-191.7479.30, built on May 30, 2019
Linux 4.18.0-22-generic

I'm having issues with the os.get_terminal_size() function call

Running the command from the terminal window works:

Python 3.7.1 (default, Oct 22 2018, 11:21:55) 
[GCC 8.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> os.get_terminal_size()
os.terminal_size(columns=223, lines=18)

But running it from the Python Console window doesn't

>>>import os
>>>os.get_terminal_size()
Traceback (most recent call last):
  File "<input>", line 1, in <module>
OSError: [Errno 25] Inappropriate ioctl for device

My googling hasn't produced much information specific to my issue at hand. What does OSError: [Errno 25] Inappropriate ioctl for device actually mean and how do I fix it?

like image 348
James Schinner Avatar asked Sep 18 '25 09:09

James Schinner


2 Answers

Your implementation of Python relies on the terminal being compliant to the request for the terminal size by the OS. In the CPython implementation, the system call in ioctl() will fail because the device (terminal) doesn't recognize the command. You can try to set

-Drun.processes.with.pty=true

in Help/Edit Custom VM Options... as per this answer.

like image 54
Leonard Avatar answered Sep 21 '25 02:09

Leonard


Instead of using os you can use shutil. This works without any hitch in Pycharm (and hopefully, by extension, IntelliJ).

import shutil
terminal_size = shutil.get_terminal_size(fallback=(120, 50))
# attributes
print('cols=', terminal_size.columns)
print('rows=', terminal_size.rows)
like image 35
polarise Avatar answered Sep 21 '25 02:09

polarise