【问题标题】:Pycharm os.get_terminal_size() not workingPycharm os.get_terminal_size() 不工作
【发布时间】:2020-08-20 16:16:27
【问题描述】:

Ubuntu 18.04.2 LTS 上的 Python 3.7.1

使用 Pycharm 版本:

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

os.get_terminal_size() 函数调用有问题

从终端窗口运行命令有效:

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)

但是从 Python 控制台窗口运行它不会

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

我的谷歌搜索没有针对我手头的问题提供太多信息。 OSError: [Errno 25] Inappropriate ioctl for device 究竟是什么意思,我该如何解决?

【问题讨论】:

    标签: module operating-system pycharm python-3.7


    【解决方案1】:

    您的 Python 实现依赖于终端符合操作系统对终端大小的请求。在CPython implementation 中,ioctl() 中的系统调用将失败,因为设备(终端)无法识别该命令。你可以尝试设置

    -Drun.processes.with.pty=true
    

    在Help/Edit Custom VM Options... 中根据this 的回答。

    【讨论】:

      【解决方案2】:

      您可以使用shutil,而不是使用os。这在 Pycharm(并且希望通过扩展,IntelliJ)中没有任何障碍。

      import shutil
      terminal_size = shutil.get_terminal_size(fallback=(120, 50))
      # attributes
      print('cols=', terminal_size.columns)
      print('rows=', terminal_size.rows)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-07-25
        • 1970-01-01
        • 2023-02-13
        • 2015-09-29
        • 2020-12-25
        相关资源
        最近更新 更多