I am using PyDev for Python programming in Eclipse.
Is it possible to make PyDev auto-format my code to limit the maximum number of characters on a single line?
To get started right away with sensible defaults, choose the python file you want to format and then write black filename.py in the terminal, then the black will format your python file. Let's look at this simple example, here is my two python functions in my python file called sample_code.py.
You can set your formatting options through the menus Tools > Options > Text Editor > Python > Formatting and its nested tabs. Formatting options by default are set to match a superset of the PEP 8 style guide.
Black can be installed by running pip install black . It requires Python 3.7+ to run. If you want to format Jupyter Notebooks, install with pip install 'black[jupyter]' .
Black is the uncompromising Python code formatter. By using it, you agree to cede control over minutiae of hand-formatting. In return, Black gives you speed, determinism, and freedom from pycodestyle nagging about formatting. You will save time and mental energy for more important matters.
It seems that this option is only available in Eclipse's Java Editor.
The Java editor allows you to create "profiles" for the code formatter, while PyDev's options for the code formatter are very limited.
You can hack this. PythonTidy.py
is an awesome script that cleans up Python code to make it follow PEP8 conventions, and that can be tweaked with your own settings.
Get here (homepage) the source for PythonTidy.
You will see inside the file, at the beginning of the code and just after the comments, that many settings are defined.
The first one of these is COL_LIMIT
with its default value set to 72
. Now you can use PythonTidy to format your code the way you want.
Now you have to make this work with PyDev's formatting. This blog post will explain it really better than me, but still I'll sum up the steps :
pyedit_pythontidy.py
, along with the PythonTidy.py
file.Configure PyDev to use this script as its Code Formatter. You can do this in Preferences > PyDev > Scripting PyDev
Note #1: I really recommend reading the original blog post to have a better understanding
Note #2: The wrapper script author did not implement Code Block formatting, so this means you can only format a full file. This should not be that hard to implement, up to you.
Note #3: All credits goes to bear330 for the PyDev integration part.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With