I have started using Python and Django and I am very new in this field. And, this is my first time to ask a question here...I do apologise in advance if there is a known solution to this issue...
When I installed and set VSCode formatter 'black' (after setting linter as flake8), the tutorial video tutor's side shows up pop-up like 'formatter autopep8 is not installed. install?'. & Mine did not show up that message.
So what I did was...
manually input 'pipenv install flack --dev --pre' on terminal.
manually input "python.formatting.provider": "black", to 'settings.json' on '.vscode' folder.
Setting(VSCode) -> flake8, Python > Linting: Flake8 Enabled (Also modified in: workspace), (ticked the box) Whether to lint Python files using flake8
The bottom code is from settings.json (on vscode folder).
{
"python.linting.pylintEnabled": false,
"python.linting.flake8Enabled": true,
"python.linting.enabled": true,
"python.formatting.provider": "black", # input manually
"python.linting.flake8Args": ["--max-line-length=88"] # input manually
}
I found a 'black formatter' document. https://github.com/psf/black & it stated... python -m black {source_file_or_directory} & I get the following error message.
Usage: __main__.py [OPTIONS] [SRC]...
Try '__main__.py -h' for help.
Error: Invalid value for '[SRC]...': Path '{source_file_or_directory}' does not exist.
Yes, honestly, I am not sure which source_file_or_directory I should set...but above all now I am afraid whether I am on the right track or not.
Can I hear your advice? At least some direction to go, please. Thanks..
Update 2023-09-15:
Now VSCode has a Microsoft oficial Black Formatter extension. It will probably solve your problems.
Original answer:
I use Black from inside VSCode and it rocks. It frees mental cycles that you would spend deciding how to format your code. It's best to use it from your favorite editor. Just run from the command line if you need to format a lot of files at once.
First, check if you have this in your VSCode
settings.json
(open it with Ctrl-P + settings):Remember that there may be 2 setting.json files: one in your home dir, and one in your project (
.vscode/settings.json
). The one inside the project prevails.That said, these kind of problems usually are about using a python interpreter where black isn't installed. I recommend the use of virtual environments, but first check your python interpreter on the status bar:
If you didn't explicitly select an interpreter, do it now clicking on the Python version in your status bar. You can also do it with Ctrl-P + "Python: Select Interpreter". The status bar should change after selecting it.
Now open a new terminal. Since you selected your interpreter, your virtual environment should be automatically activated by VSCode. Run python using your interpreter path and try to import black:
Failed import? Problem solved. Just install black using the interpreter from the venv:
python -m pip install black
. You also can install using Conda, but in my experience VSCode works better with pip.Still not working? Click in the "OUTPUT" tab sibling of the TERMINAL and try to get more info at the "Log" output (if you use the newer Black plugun it may be called "Black Formatter"). Select it in the pull down menu: