Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.3k views
in Technique[技术] by (71.8m points)

python - Black formatter does not work in VSCode after installing anaconda3

settings.json

"python.pythonPath": "/Users/brandonwie/opt/anaconda3/bin/python",
"[python]": {
    "editor.tabSize": 4
},
"python.languageServer": "Pylance",
"python.linting.enabled": true,
"python.linting.pylintEnabled": false,
"python.formatting.blackPath": "/Users/brandonwie/opt/anaconda3/bin/black",
"python.showStartPage": false,
"python.formatting.provider": "black",
"python.analysis.typeCheckingMode": "basic",

I'm using VSCode, I've installed anaconda3 to try conda environment, but then black formatter stopped working right after I start using conda Python(I don't know it's because of conda Python). So I installed black with (conda) pip, but nothing is working..

Initially, the blackPath in setting.json was set to /usr/local/bin/black. The black formatter was working flawlessly until I install and set up VSCode with conda, there was a pop up at the right bottom when I saved a file "cannot find black" something like that.(because of 'format on save') I've tried changing the path to /usr/... and /Users/ back and forth, but doesn't work as well.

How can I solve this issue?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You don't need to specify the blackpath in Settings.json.

  1. Run pip uninstall black to delete black in current activated conda environment;

  2. In Settings.json, set "python.formatting.provider": "black",

  3. Turn to .py file then press Ctrl+S to save it. At the same time, there will be an notification popping up:

    enter image description here

    Click Yes and Install using Pip: enter image description here

After its installation, you can use black to format your file successfully. enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...