Elroy Flynn
Elroy Flynn

Reputation: 3218

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.

Upvotes: 30

Views: 80749

Answers (8)

Vaibhav Patil
Vaibhav Patil

Reputation: 59

The same happened to me, then I just changed the key binding to other keys and it simply worked.

Upvotes: -1

astroflyer
astroflyer

Reputation: 248

If someone encounters this on VSCode, even after properly installing autopep8, configuring the default formatters as others suggested, configuring the keyboard shortcut (though now is default to Shift Ctrl F)...

Try deleting the .vscode folder in the current folder entirely, this is what made the document formatter worked for me.

There might be some conflicting parameters inside and causing this nightmare.

Upvotes: 2

stoneshishang
stoneshishang

Reputation: 443

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  

Upvotes: 2

Juan Montes
Juan Montes

Reputation: 21

I encountered this same error running WSL: Ubuntu-20.04, well this was my solution:

$ pip --version

This will make you verify that you have python3 pip installed correctly in your distribution, if that's not the case, it will pop up an error:

    Command 'pip' not found, but can be installed with:

apt install python3-pip

Just run the indicated command to installed it and then after that, run:

$ python3 -m pip install autopep8

Now everything should be working as it should, including the formatting autpep8.

Upvotes: 1

folaRin
folaRin

Reputation: 129

I encountered an error message while trying to do same and below was what I did (in my terminal):

  1. Install or upgrade pep8:

    pip install --upgrade autopep8

  2. Navigate to the folder/directory where the file you need formatted is, then use the following command:

    autopep8 --in-place file_name

There you have it!

Upvotes: 4

smoore4
smoore4

Reputation: 4866

You need to add this as a PATH under System Environment Variables:

c:\users\<username>\appdata\roaming\python\python39\site-packages 

Then close and restart VS Code. Type autopep8 at a terminal prompt and you should see this:

C:\Foobar>autopep8
usage: autopep8 [-h] [--version] [-v] [-d] [-i] [--global-config filename] [--ignore-local-config] [-r] [-j n] [-p n] [-a] [--experimental] [--exclude globs] [--list-fixes] [--ignore errors] [--select errors]
                [--max-line-length n] [--line-range line line] [--hang-closing] [--exit-code]
                [files ...]
autopep8: error: incorrect number of arguments

Upvotes: 1

Mintu
Mintu

Reputation: 512

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

pip install pep8

This will start installing pep8.

Upvotes: 2

JT Dong
JT Dong

Reputation: 246

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

Upvotes: 16

Related Questions