Install autopep8 via pip
mainYou can install or upgrade autopep8 using pip. It is recommended to use the --user option if you do not want to install it globally.
$ pip install --upgrade autopep8repository·main·Indexed 26 days ago
https://github.com/hhatto/autopep8autopep8 is a tool that automatically formats Python code to conform to the PEP 8 style guide by using pycodestyle to identify and fix formatting issues. It provides a CLI for in-place editing, recursive directory processing, and parallel execution, as well as a programmatic API via fix_code and fix_file. The tool supports aggressive and experimental formatting modes, custom fix extensions, and configuration via pyproject.toml.
You can install or upgrade autopep8 using pip. It is recommended to use the --user option if you do not want to install it globally.
$ pip install --upgrade autopep8To ensure you are testing against the latest version of autopep8 when reporting bugs, upgrade using the following command:
$ pip install --upgrade git+https://github.com/hhatto/autopep8Before submitting a pull request:
pycodestyle bug. If it is, file the PR at the pycodestyle repository.test/test_autopep8.py that demonstrates your changes.To use autopep8 as a pre-commit hook, add the following to your .pre-commit-config.yaml file:
repos:
- repo: https://github.com/hhatto/autopep8
rev: ... # select the tag or revision you want
hooks:
- id: autopep8You can disable autopep8 for specific sections of a file by using # autopep8: off and re-enabling it with # autopep8: on. Alternatively, # fmt: off and # fmt: on are also valid markers.
# autopep8: off
[
[23, 23, 13, 43],
[32, 34, 34, 34],
[56, 34, 34, 11],
[10, 10, 10, 10],
]
# autopep8: onpycodestyle) and applying corresponding fixes to the source code.autopep8 searches for configuration in several locations:
$HOME/.config/pycodestyle (or ~/.pycodestyle on Windows). You can also specify a custom global file using --global-config.setup.cfg, tox.ini, .pep8, or .flake8 in the directory of the target file. These can use the [pycodestyle], [pep8], or [flake8] sections.[tool.autopep8] section. This takes precedence over all other configuration files.[tool.autopep8]
max_line_length = 120
ignore = "E501,W6" # or ["E501", "W6"]
in-place = true
recursive = true
aggressive = 3You can add custom formatting rules to autopep8 by defining functions in the module with the pattern fix_<code_name>(source).
source as an argument.apply_global_fixes().For fixes that depend on pycodestyle, you should add them as methods to the FixPEP8 class.
If you encounter pkg_resources.DistributionNotFound while running autopep8, it is likely due to an outdated version of setuptools. Upgrade setuptools to resolve this issue.
$ pip install --upgrade setuptoolsWhen submitting a bug report, ensure the issue is not actually a bug in pycodestyle. If pycodestyle is behaving incorrectly, file the report at the pycodestyle repository instead.
For an autopep8 bug report, please include:
autopep8 --version outputpycodestyle --version outputpython --version outputuname -a (if on Unix)autopep8 command-line options usedTo automatically format a file and save the changes directly to the file, use the --in-place flag. Adding --aggressive multiple times increases the level of non-whitespace changes applied.
$ autopep8 --in-place --aggressive --aggressive <filename>autopep8 programmatically by calling the fix_code() function. You can pass a string of code and an optional options dictionary to control behavior.