我真的
神馬情況,我的vscode死活不能格式化python代碼,還有C++代碼也不能格式化,json代碼都能格式化,為啥到python、C++就不行了。。。。
(格式化json代碼)
(格式化python代碼)
都無反應(yīng)。。。
解決方法:用yapf
弄了半天解決不了。。。只能用外部工具解決了,就是麻煩點(diǎn)
搞了個(gè)外部工具yapf來格式化python代碼
安裝方法:
pip install yapf
如果要格式化everythingMemory.py,就執(zhí)行:
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代碼格式化工具。
位置參數(shù):
files 當(dāng)未指定文件時(shí)從stdin讀取。
可選參數(shù):
-h, --help 顯示幫助信息并退出
-v, --version 顯示程序的版本號(hào)并退出
-d, --diff 打印修復(fù)后源代碼的差異
-i, --in-place 直接修改文件
-q, --quiet 不輸出任何內(nèi)容并設(shè)置返回值
-r, --recursive 遞歸運(yùn)行目錄下的文件
-l START-END, --lines START-END
指定要重新格式化的行范圍,從1開始計(jì)數(shù)
-e PATTERN, --exclude PATTERN
排除格式化的文件模式
--style STYLE 指定格式化樣式:可以是樣式名稱(例如"pep8"或"google"),也可以是包含樣式設(shè)置的文件名。默認(rèn)樣式是pep8,除非在源代碼所在目錄或其父目錄中找到.style.yapf或setup.cfg或pyproject.toml文件(對(duì)于stdin,使用當(dāng)前目錄)。
--style-help 顯示樣式設(shè)置并退出;可以將此輸出保存到.style.yapf文件中以使設(shè)置永久生效
--no-local-style 不搜索本地樣式定義
-p, --parallel 在格式化多個(gè)文件時(shí)并行運(yùn)行YAPF。
-m, --print-modified 打印修改過的文件名
-vv, --verbose 在處理過程中打印文件名
示例
實(shí)際使用時(shí)可選擇只格式化單個(gè)文件,格式化某個(gè)目錄,或者遞歸格式化某個(gè)目錄下所有文件。。
格式化單個(gè)文件(格式化前先用-d參數(shù)預(yù)先查看格式化更改內(nèi)容,以決定是否要更改)
yapf -d /path/to/file.py
yapf -i /path/to/file.py
格式化某個(gè)目錄
yapf -d /path/to/dir
yapf -i /path/to/dir
(這個(gè)我沒測(cè)試過)
遞歸格式化某個(gè)目錄
yapf -d -r /path/to/dir
yapf -i -r /path/to/dir
(這個(gè)我沒測(cè)試過)文章來源:http://www.zghlxwxcb.cn/news/detail-751313.html
20230716 齊拉帕,我刪除了虛擬環(huán)境目錄,重裝了一下python,好像又可以了。。但是格式化程序只有autopep8和Black Formatter可以,Python不行。。
apt purge python3
apt install python3
文章來源地址http://www.zghlxwxcb.cn/news/detail-751313.html
到了這里,關(guān)于vscode 無法格式化python代碼、無法格式化C++代碼(vscode格式化失效)另一種解決辦法:用外部工具yapf格式化(yapf工具)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!