396

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?

Benyamin Jafari
  • 27,880
  • 26
  • 135
  • 150
Ansuman Bebarta
  • 7,011
  • 6
  • 27
  • 44

8 Answers8

586

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

Pycharm 4 Settings Screenshot

xtranophilist
  • 582
  • 8
  • 14
Alex G.P.
  • 9,609
  • 6
  • 46
  • 81
  • 50
    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](http://legacy.python.org/dev/peps/pep-0008/#maximum-line-length). – Krøllebølle Nov 12 '14 at 08:20
  • 26
    @Krøllebølle PEP 8 is just a recomentation, not a mandatory requirement. – Alex G.P. Nov 13 '14 at 07:49
  • 41
    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](https://www.python.org/dev/peps/pep-0008/#a-foolish-consistency-is-the-hobgoblin-of-little-minds): `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.` – Krøllebølle Feb 04 '16 at 10:08
  • 1
    @Krøllebølle. I believe (without any evidence) that the tight restriction in PEP8 was intended for the code Python code-base so that it's workable in the most restrictive environments: those doing coding with 80 column terminals. Those writing libraries and programs in more sophisticated environments would find 80 columns to do more damage than good. – Chris Cogdon Mar 02 '16 at 20:35
  • 51
    PEP8 E501 — line too long (> 79 characters) — is way deprecated nowadays. Even big and well know projects like Django don't use it anymore (https://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.` – JChris Apr 06 '16 at 17:42
  • 36
    @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. – joar Jun 08 '16 at 12:51
  • 2
    +1 @joar and I have one further point; once you decide 120 characters is OK, suddenly people find that too restrictive too :P I myself have adopted a "soft 79" rule in some of my projects; I aim for 79, but don' worry if lines here and there go slightly over. –  Feb 13 '18 at 12:32
  • 1
    @joar is right - the reason for the limitation is not old monitors, but readability and being able to show files next to each other (think of diffs or three-way-merges). Having said this, 79 is probably really too small. The "black" formatter uses 88 as default with the explanation that this "... happens to be 10% over 80. This number was found to produce significantly shorter files than sticking with 80 (the most popular), or even 79 (used by the standard library). In general, 90-ish seems like the wise choice." In practice, I found that maximum line sizes around 90 also worked best for me. – Cito Aug 22 '20 at 09:00
81

For PyCharm 2018.1 on Mac:

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

enter image description here

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.

Tim Stack
  • 3,209
  • 3
  • 18
  • 39
lmiguelvargasf
  • 63,191
  • 45
  • 217
  • 228
25

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

Marco Sanchez
  • 788
  • 7
  • 22
8

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.

preference_highlighted

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Steve Stacha
  • 178
  • 2
  • 8
7

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. :)

andy
  • 3,951
  • 9
  • 29
  • 40
  • 2
    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? – Caco May 24 '17 at 10:34
  • Same here, any idea how to make this work? just html stops afterwards? – EsseTi Feb 03 '23 at 12:59
6

For PyCharm 2021.x this did the trick for python: enter image description here

HeyMan
  • 1,529
  • 18
  • 32
4

For PyCharm 2019.3.1, I see this.

enter image description here

ZhefengJin
  • 930
  • 8
  • 17
1

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.

Abhishake Gupta
  • 2,939
  • 1
  • 25
  • 34