ChatGPT解决这个技术问题 Extra ChatGPT

How do I set the maximum line length in PyCharm?

I am using PyCharm on Windows and want to change the settings to limit the maximum line length to 79 characters, as opposed to the default limit of 120 characters.

Where can I change the maximum amount of characters per line in PyCharm?


x
xtranophilist

Here is screenshot of my Pycharm. Required settings is in following path: File -> Settings -> Editor -> Code Style -> General: Right margin (columns)

https://i.stack.imgur.com/V3BLg.png


I am wondering why the default value is set to 120 characters. PEP 8 clearly states: Limit all lines to a maximum of 79 characters.
@Krøllebølle PEP 8 is just a recomentation, not a mandatory requirement.
Indeed, and in retrospect for the project where we followed PEP 8, we were way too strict. Following the 79 line limitation, the code quickly becomes unreadable and unmaintainable. Let's quote PEP-8: A 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 — line too long (> 79 characters) — is way deprecated nowadays. Even big and well know projects like Django don't use it anymore (code.djangoproject.com/ticket/23395), as you can see here: 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 I have an argument to counter the "ancient terminals" strawman(?) from Django: On my 1920px wide screen I can fit 3 source files that has a max width ~79 columns horizontally. I believe that it provides me a better overview of what I'm working with as I can have the library module as well as the template file open right next to the view I'm working on.
T
Tim Stack

For PyCharm 2018.1 on Mac:

Preferences (+,), then Editor -> Code Style:

https://i.stack.imgur.com/tQVsX.png

For PyCharm 2018.3 on Windows:

File -> Settings (Ctrl+Alt+S), then Editor -> Code Style:

To follow PEP-8 set Hard wrap at to 80.


M
Marco Sanchez

For PyCharm 4

File >> Settings >> Editor >> Code Style: Right margin (columns)

suggestion: Take a look at other options in that tab, they're very helpful


a
andy

You can even set a separate right margin for HTML. Under the specified path:

File >> Settings >> Editor >> Code Style >> HTML >> Other Tab >> Right margin (columns)

This is very useful because generally HTML and JS may be usually long in one line than Python. :)


I was looking for this. Thanks @andy, but even increasing right margin for HTML, the text continues wrapping in the 79th column. Is there some setting more?
B
Brian Tompsett - 汤莱恩

For anyone, or myself if I reload my machine, who this is not working for when you do a code reformat there is an additional option to check under editor->code style->python : ensure right margin is not exceeded. Once this was selected the reformat would work.

https://i.stack.imgur.com/7AAO4.png


Z
ZhefengJin

For PyCharm 2019.3.1, I see this.

https://i.stack.imgur.com/tF7A0.png


H
HeyMan

https://i.stack.imgur.com/XwpHE.png


A
AB Abhi

For PyCharm 2017

We can follow below: File >> Settings >> Editor >> Code Style.

Then provide values for Hard Wrap & Visual Guides for wrapping while typing, tick the checkbox.

NB: look at other tabs as well, viz. Python, HTML, JSON etc.