Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visual Studio Code, autopep8 doesn't run

On Windows 10. I did this:

pip install autopep8

and in vscode user settings I have

"python.linting.pep8Enabled": true,
"python.formatting.provider": "autopep8",

When I run format document, or explicitly invoke autopep8 from the command pallette, the error is:

Error: Command failed: autopep8 c:\tca-backend\lambdas\utilities\NetMenuAPIUtil.py
'autopep8' is not recognized as an internal or external command,
operable program or batch file.

Clearly, vsc wants to invoke autopep8.exe but there is no exe. Just py. So I created a autopep8.bat which works when I test from the command line, but when run from vsc, it inserts the content of the batch file into the top of the document. (Yes, that's as strange as it sounds.)

All other Python-related operations work ok, including the ESLint extension.

like image 758
Elroy Flynn Avatar asked Apr 11 '18 20:04

Elroy Flynn


People also ask

How do I enable auto format in VS code?

The code formatting is available in Visual Studio Code (VSCode) through the following shortcuts or key combinations: On Windows Shift + Alt + F. On macOS Shift + Option + F. On Linux Ctrl + Shift + I.

What is autopep8?

autopep8 automatically formats Python code to conform to the PEP 8 style guide. It uses the pycodestyle utility to determine what parts of the code needs to be formatted. autopep8 is capable of fixing most of the formatting issues that can be reported by pycodestyle.


3 Answers

VS code Python extension supports source code formatting using either autopep8 (the default), black, or yapf so you don't need to install python formatting tools by yourself.

The way I use formatting is to set a shortcut in vs code.

Go to File -> Preferences -> Keyboard Shortcuts, then search format. Set the shortcut as ctrl + shift + p which is the same as the shortcut of autopep8 or you can set any combination you prefer.

enter image description here

Click the shortcuts in your .py files then you will get the formatted code.

Ref: https://code.visualstudio.com/docs/python/editing

like image 199
JT Dong Avatar answered Oct 17 '22 16:10

JT Dong


Since you are using VSCode on Windows, please click on the Terminal Section and type

pip install pep8

This will start installing pep8.

like image 2
Mintu Avatar answered Oct 17 '22 15:10

Mintu


for me, the autopep8 doc installation isn't enough, still seeing this error, I had to follow this https://pip.pypa.io/en/latest/user_guide/#user-installs. it works like a charm.

I'm on windows so I used this:

py -m pip install –-user autopep8  
like image 2
stoneshishang Avatar answered Oct 17 '22 17:10

stoneshishang