【问题标题】:How do I set the maximum line length in PyCharm?如何在 PyCharm 中设置最大行长?
【发布时间】:2013-06-23 13:17:40
【问题描述】:

我在 Windows 上使用 PyCharm,并希望更改设置以将最大行长度限制为 79 字符,而不是默认限制 120 字符。

我在哪里可以更改 PyCharm 中每行的最大字符数?

【问题讨论】:

    标签: python pycharm pep8


    【解决方案1】:

    这是我的 Pycharm 的截图。所需设置位于以下路径:File -> Settings -> Editor -> Code Style -> General: Right margin (columns)

    【讨论】:

    • 我想知道为什么默认值设置为 120 个字符。 PEP 8 明确指出:Limit all lines to a maximum of 79 characters.
    • @Krøllebølle PEP 8 只是建议,不是强制性要求。
    • 确实,回想一下我们遵循 PEP 8 的项目,我们太严格了。在 79 行限制之后,代码很快变得不可读和不可维护。让我们引用PEP-8A style guide is about consistency. Consistency with this style guide is important. Consistency within a project is more important. Consistency within one module or function is most important. But most importantly: know when to be inconsistent -- sometimes the style guide just doesn't apply.
    • PEP8 E501 — 行太长(> 79 个字符) — 现在已被弃用。即使像 Django 这样的大型知名项目也不再使用它 (code.djangoproject.com/ticket/23395),您可以在此处看到:One big exception to PEP 8 is our preference of longer line lengths. We’re well into the 21st Century, and we have high-resolution computer screens that can fit way more than 79 characters on a screen. Don’t limit lines of code to 79 characters if it means the code looks significantly uglier or is harder to read.
    • @ChrisCogdon 我有一个论据来对抗来自 Django 的“古代终端”稻草人(?):在我的 1920 像素宽屏幕上,我可以容纳 3 个横向最大宽度约为 79 列的源文件。我相信它可以让我更好地了解我正在使用的内容,因为我可以在我正在处理的视图旁边打开库模块和模板文件。
    【解决方案2】:

    对于 Mac 上的 PyCharm 2018.1:

    首选项 (+,),然后是 Editor -> Code Style:

    对于 Windows 上的 PyCharm 2018.3:

    文件 -> 设置(Ctrl+Alt+S),然后Editor -> Code Style:

    要遵循 PEP-8,请将 Hard wrap at 设置为 80。

    【讨论】:

      【解决方案3】:

      对于 PyCharm 4

      文件>>设置>>编辑器>>代码样式:右边距(列)

      建议:看看该标签中的其他选项,它们非常有帮助

      【讨论】:

        【解决方案4】:

        您甚至可以为 HTML 设置单独的右边距。指定路径下:

        文件 >> 设置 >> 编辑器 >> 代码样式 >> HTML >> 其他选项卡 >> 右边距(列)

        这非常有用,因为通常 HTML 和 JS 在一行中通常比 Python 长。 :)

        【讨论】:

        • 我在找这个。感谢@andy,但即使增加了 HTML 的右边距,文本继续在第 79 列中换行。还有一些设置吗?
        【解决方案5】:

        对于任何人,或者我自己,如果我重新加载我的机器,当你重新格式化代码时这对谁不起作用,有一个额外的选项可以在 editor->code style->python 下检查:确保不超过右边距。选择此选项后,重新格式化将起作用。

        【讨论】:

          【解决方案6】:

          对于 PyCharm 2019.3.1,我看到了这个。

          【讨论】:

            【解决方案7】:

            对于 PyCharm 2017

            我们可以按照以下方式进行: 文件 >> 设置 >> 编辑器 >> 代码样式。

            然后为Hard WrapVisual Guides 提供值 for wrapping 输入时,勾选复选框。

            注意:也请查看其他选项卡,即。 Python、HTML、JSON 等

            【讨论】:

              【解决方案8】:

              对于 PyCharm 2021.x,这对 python 起到了作用:

              【讨论】:

                猜你喜欢
                • 2019-09-01
                • 2019-02-26
                • 2021-08-03
                • 1970-01-01
                • 2012-09-16
                • 2015-02-16
                • 1970-01-01
                • 2023-03-15
                • 2012-04-05
                相关资源
                最近更新 更多