vscode 无法格式化python代码、无法格式化C++代码(vscode格式化失效)另一种解决办法:用外部工具yapf格式化(yapf工具)

文章目录

    • 我真的
    • 解决方法:用yapf
    • yapf工具使用方法
    • 示例
      • 格式化单个文件(格式化前先用-d参数预先查看格式化更改内容,以决定是否要更改)
      • 格式化某个目录
      • 递归格式化某个目录
    • 20230716 齐拉帕,我删除了虚拟环境目录,重装了一下python,好像又可以了。。但是格式化程序只有autopep8和Black Formatter可以,Python不行。。

我真的

神马情况,我的vscode死活不能格式化python代码,还有C++代码也不能格式化,json代码都能格式化,为啥到python、C++就不行了。。。。

(格式化json代码)

(格式化python代码)

都无反应。。。

解决方法:用yapf

弄了半天解决不了。。。只能用外部工具解决了,就是麻烦点

搞了个外部工具yapf来格式化python代码

安装方法:

pip install yapf

如果要格式化everythingMemory.py,就执行:

yapf -i Untitled-1.py

立马给格式化了

yapf工具使用方法

yapf -h
usage: yapf [-h] [-v] [-d | -i | -q] [-r | -l START-END] [-e PATTERN] [--style STYLE] [--style-help] [--no-local-style] [-p] [-m] [-vv] [files [files ...]]

Formatter for Python code.

positional arguments:
  files                 reads from stdin when no files are specified.

optional arguments:
  -h, --help            show this help message and exit
  -v, --version         show program's version number and exit
  -d, --diff            print the diff for the fixed source
  -i, --in-place        make changes to files in place
  -q, --quiet           output nothing and set return value
  -r, --recursive       run recursively over directories
  -l START-END, --lines START-END
                        range of lines to reformat, one-based
  -e PATTERN, --exclude PATTERN
                        patterns for files to exclude from formatting
  --style STYLE         specify formatting style: either a style name (for example "pep8" or "google"), or the name of a file with style settings. The default is pep8 unless a
                        .style.yapf or setup.cfg or pyproject.toml file located in the same directory as the source or one of its parent directories (for stdin, the current
                        directory is used).
  --style-help          show style settings and exit; this output can be saved to .style.yapf to make your settings permanent
  --no-local-style      don't search for local style definition
  -p, --parallel        run YAPF in parallel when formatting multiple files.
  -m, --print-modified  print out file names of modified files
  -vv, --verbose        print out file names while processing
用法:yapf [-h] [-v] [-d | -i | -q] [-r | -l START-END] [-e PATTERN] [--style STYLE] [--style-help] [--no-local-style] [-p] [-m] [-vv] [files [files ...]]

Python代码格式化工具。

位置参数:
  files                 当未指定文件时从stdin读取。

可选参数:
  -h, --help            显示帮助信息并退出
  -v, --version         显示程序的版本号并退出
  -d, --diff            打印修复后源代码的差异
  -i, --in-place        直接修改文件
  -q, --quiet           不输出任何内容并设置返回值
  -r, --recursive       递归运行目录下的文件
  -l START-END, --lines START-END
                        指定要重新格式化的行范围,从1开始计数
  -e PATTERN, --exclude PATTERN
                        排除格式化的文件模式
  --style STYLE         指定格式化样式:可以是样式名称(例如"pep8"或"google"),也可以是包含样式设置的文件名。默认样式是pep8,除非在源代码所在目录或其父目录中找到.style.yapf或setup.cfg或pyproject.toml文件(对于stdin,使用当前目录)。
  --style-help          显示样式设置并退出;可以将此输出保存到.style.yapf文件中以使设置永久生效
  --no-local-style      不搜索本地样式定义
  -p, --parallel        在格式化多个文件时并行运行YAPF。
  -m, --print-modified  打印修改过的文件名
  -vv, --verbose        在处理过程中打印文件名

示例

实际使用时可选择只格式化单个文件,格式化某个目录,或者递归格式化某个目录下所有文件。。

格式化单个文件(格式化前先用-d参数预先查看格式化更改内容,以决定是否要更改)

yapf -d /path/to/file.py
yapf -i /path/to/file.py

格式化某个目录

yapf -d /path/to/dir
yapf -i /path/to/dir

(这个我没测试过)

递归格式化某个目录

yapf -d -r /path/to/dir
yapf -i -r /path/to/dir

(这个我没测试过)

20230716 齐拉帕,我删除了虚拟环境目录,重装了一下python,好像又可以了。。但是格式化程序只有autopep8和Black Formatter可以,Python不行。。

apt purge python3
apt install python3

文章出处登录后可见!

已经登录?立即刷新

共计人评分,平均

到目前为止还没有投票!成为第一位评论此文章。

(0)
乘风的头像乘风管理团队
上一篇 2023年11月9日
下一篇 2023年11月9日

相关推荐