Maximum Line Length¶
- Pep 8 Cheat Sheet Printable
- Pep 8 Cheat Sheet 2019
- Pep 8 Cheat Sheet 2020
- Pep8 Cheat Sheet
- Python Pep 8 Cheat Sheet
There are also some PEP-8 cheat sheets that can be also used. I found a good one here. It summarizes the most important points of the PEP-8 guideline. If you want to learn more about PEP-8, then you can read the full documentation. Visit also pep8.org. The website contains same information as the documentation, but it has been nicely formatted.
Limit all lines to a maximum of 79 characters.
There are still many devices around that are limited to 80 characterlines; plus, limiting windows to 80 characters makes it possible tohave several windows side-by-side. The default wrapping on suchdevices disrupts the visual structure of the code, making it moredifficult to understand. Therefore, please limit all lines to amaximum of 79 characters. For flowing long blocks of text (docstringsor comments), limiting the length to 72 characters is recommended.
The preferred way of wrapping long lines is by using Python’s impliedline continuation inside parentheses, brackets and braces. Long linescan be broken over multiple lines by wrapping expressions inparentheses. These should be used in preference to using a backslashfor line continuation.
See more: need python tutoring, freelancer python coder, python coder betfair, pep 8 cheat sheet, python coding standards pdf, python pep, google python style guide, python code format, python pep8 checker, python variable naming conventions, python docstring, python coder, project need python script, hire python coder, i need a python coder. See the Cheat Sheet: Writing Python 2-3 compatible code cheat sheet and What else you need to know for more info. After each change, re-run the tests on Py3 and Py2 to ensure they pass on both. Push your code and announce to the world! Hashtags #python3 #python-future.
Backslashes may still be appropriate at times. For example, long,multiple with
-statements cannot use implicit continuation, sobackslashes are acceptable:
Another such case is with assert
statements.
Make sure to indent the continued line appropriately. The preferredplace to break around a binary operator is after the operator, notbefore it. Some examples:
How to convert Py2 code to Py2/3 code using futurize
:
Step 0: setup¶
Step 0 goal: set up and see the tests passing on Python 2 and failing on Python 3.
Clone the package from github/bitbucket. Optionally rename your repo to
package-future
. Examples:reportlab-future
,paramiko-future
,mezzanine-future
.Create and activate a Python 2 conda environment or virtualenv. Install the package with
pythonsetup.pyinstall
and run its test suite on Py2.7 or Py2.6 (e.g.pythonsetup.pytest
orpy.test
ornosetests
)Optionally: if there is a
.travis.yml
file, add Python version 3.3 and remove any versions < 2.6.Install Python 3.3 with e.g.
sudoapt-getinstallpython3
. On other platforms, an easy way is to use Miniconda. Then e.g.:

Step 1: modern Py2 code¶
The goal for this step is to modernize the Python 2 code without introducing any dependencies (on future
or e.g. six
) at this stage.
1a. Install future
into the virtualenv using:
1b. Run futurize--stage1-w*.pysubdir1/*.pysubdir2/*.py
. Note that withrecursive globbing in bash
or zsh
, you can apply stage 1 to all Pythonsource files recursively with:
Pep 8 Cheat Sheet Printable
1c. Commit all changes
1d. Re-run the test suite on Py2 and fix any errors.
See Stage 1: “safe” fixes for more info.
Example error¶
One relatively common error after conversion is:
If you get this error, try adding an empty __init__.py
file in the packagedirectory. (In this example, in the tests/ directory.) If this doesn’t help,and if this message appears for all tests, they must be invoked differently(from the cmd line or e.g. setup.py
). The way to run a module inside apackage on Python 3, or on Python 2 with absolute_import
in effect, is:
(For more info, see PEP 328 andthe PEP 8 section on absoluteimports.)
Pep 8 Cheat Sheet 2019
Step 2: working Py3 code that still supports Py2¶
The goal for this step is to get the tests passing first on Py3 and then on Py2again with the help of the future
package.
2a. Run:
Or, using recursive globbing with bash
or zsh
, you can view the stage 2changes to all Python source files recursively with:
Pep 8 Cheat Sheet 2020
To apply the changes, add the -w
argument.
This stage makes further conversions needed to support both Python 2 and 3.These will likely require imports from future
on Py2 (and sometimes on Py3),such as:
Optionally, you can use the --unicode-literals
flag to add this import tothe top of each module:
All strings in the module would then be unicode on Py2 (as on Py3) unlessexplicitly marked with a b'
prefix.
Pep8 Cheat Sheet
If you would like futurize
to import all the changed builtins to have theirPython 3 semantics on Python 2, invoke it like this:
2b. Re-run your tests on Py3 now. Make changes until your tests pass on Python 3.
2c. Commit your changes! :)
2d. Now run your tests on Python 2 and notice the errors. Add wrappers fromfuture
to re-enable Python 2 compatibility. See theCheat Sheet: Writing Python 2-3 compatible code cheat sheet and What else you need to know for more info.
After each change, re-run the tests on Py3 and Py2 to ensure they pass on both.


Python Pep 8 Cheat Sheet
2e. You’re done! Celebrate! Push your code and announce to the world! Hashtags#python3 #python-future.
