question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Format Document not working (autopep8, python, vscode)

See original GitHub issue

Environment data

  • VS Code version: 1.36.0
  • Extension version (available under the Extensions sidebar): 2019.6.22090
  • OS and version: macOS, Mojave 10.14.5
  • Python version (& distribution if applicable, e.g. Anaconda): Python 2.7.16
  • Type of virtual environment used (N/A | venv | virtualenv | conda | …): N/A
  • Relevant/affected Python packages and their versions: autopep8 1.4.4 pycodestyle: 2.5.0
  • Jedi or Language Server? (i.e. what is "python.jediEnabled" set to; more info #3977): Jedi Enabled is “checked” (so, True?)

Expected behaviour

When I run “Format Document”, the file is formatted.

Actual behaviour

When I run “Format Document”, the file is not formatted.

Steps to reproduce:

  1. Open a Workspace
  2. Open up a Python File in the Workspace
  3. Right Click -> Format Document

Current Settings:

User Settings (showing only Python related settings):

{
    .
    .
    .
    "workbench.statusBar.visible": true,
    "files.autoSave": "afterDelay",
    "editor.formatOnPaste": true,
    "editor.formatOnSave": true,
    "editor.formatOnSaveTimeout": 1500,
    "python.pythonPath": "/usr/local/bin/python",
    "python.formatting.provider": "autopep8",
    "python.formatting.autopep8Path": "/usr/local/lib/python2.7/site-packages/autopep8.py",
    "python.formatting.autopep8Args": [
        "--max-line-length",
        "70",
        "--aggressive",
        "--aggressive",
        "--in-place"
    ],
    "python.linting.pylintArgs": [
        "--init-hook='import sys; sys.path.append(\"/Users/bagursreenivasamurth/OneDrive - F5 Networks/dev/godev/src/gitswarm.f5net.com/proj/tests/libs\")'"
    ]
}

Workspace Settings:

{
	"folders": [
		{
			"path": "."
		},
		{
			"path": "godev"
		}
	],
	"settings": {
		"python.pythonPath": "/usr/local/bin/python",
	}
}

Extensions:

Extensions

What I have already tried:

  1. Ensured autopep8 is installed correctly to py2.7 : pip2 install --upgrade autopep8
  2. The interpreter seems to be correctly pointing to py2.7 too py27
  3. This works: /usr/local/lib/python2.7/site-packages/autopep8.py --max-line-length 60 --in-place --aggressive --aggressive test.py
  4. This works too: python -m autopep8 --max-line-length 60 --in-place --aggressive --aggressive test.py
  5. autopep8 imported in python interpreter is the same as the one pointing in vscode python.formatting.autopep8Path setting:
$ python
Python 2.7.16 (default, Apr 12 2019, 15:32:40) 
[GCC 4.2.1 Compatible Apple LLVM 10.0.1 (clang-1001.0.46.3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import autopep8
>>> autopep8.__file__
'/usr/local/lib/python2.7/site-packages/autopep8.pyc


vscode setting:
 "python.formatting.autopep8Path": "/usr/local/lib/python2.7/site-packages/autopep8.py"
  1. Uninstalled all extensions, reloaded. Reinstalled all extensions.
  • When I had uninstalled all extensions, including Python. On “Format Document”, I got a prompt asking me to choose formatter.
  • After reinstalling all extensions, including Python. On “Format Document”, I get no prompt. I’m guessing it is honoring my user settings. onFormatSave-disabled-extensions

Meanwhile,

$ which python
/usr/local/bin/python

$ python --version
Python 2.7.16

Logs

  1. No output in “Output” when I run “Format Document”
  2. Console log on Developer Tools on “Format Document” onFormatDocument

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6

github_iconTop GitHub Comments

1reaction
coldinocommented, Jul 11, 2019

Not 100% certain this is the same issue, but I am seeing similar behaviour with the yapf formatter - but only in certain sub-directories of a project.

I’ll follow a similar diagnostic procedure to the above in case this turns out to be relevant. Ignore if not.

Workspace settings.json:

{
    "files.exclude": {
        "**/__pycache__": true,
        "**/.pytest_cache": true,
        "**/.mypy_cache": true,
    },
    "python.testing.unittestEnabled": false,
    "python.testing.nosetestsEnabled": false,
    "python.testing.pytestEnabled": true,
    "python.testing.pytestArgs": [],
    "[python]": {
        "editor.formatOnSave": true
    },
    "python.jediEnabled": false,
    "python.formatting.provider": "yapf",
    "python.linting.pylintEnabled": true,
    "python.linting.mypyEnabled": true,
    "python.linting.enabled": true,
    "python.linting.lintOnSave": true,
    "git.ignoreLimitWarning": true
}

Interpreter is set to the relevant pipenv virtual environment correctly in user settings. yapf is installed in this virtual environment is has been used correctly by VSCode in the past:

$ where yapf
C:\Users\someone\.virtualenvs\UAssetExperiment-dd5KH3id\Scripts\yapf.exe
$ python
Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 04:59:51) [MSC v.1914 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import yapf
>>> print(yapf.__file__)
C:\Users\someone\.virtualenvs\UAssetExperiment-dd5KH3id\lib\site-packages\yapf\__init__.py

In both the top-level of the project and most other sub-directories Format Document and format-on-save works without issue.

In my project’s automate module sub-directory however no formatting takes place. There is sadly no format-related output in the Python output window.

The following commands work as expected:

yapf -r -i automate
python -m yapf -i automate/git.py

Issue has been present for at least the last 2-3 days when using the latest Insiders, maybe longer.

0reactions
sanathkumarbscommented, Jul 12, 2019

Thanks, @DonJayamanne. Could there be some additional checks to catch this issue? Possibly log in console with an error/warning?

Or mention about this in the docs?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Visual Studio Code, autopep8 doesn't run - Stack Overflow
Go to File -> Preferences -> Keyboard Shortcuts, then search format . Set the shortcut as ctrl + shift + p which is...
Read more >
Autopep8 not working in VSCode. Not sure what i've changed ...
Make sure VSCODE is using the same python interpreter that your command line is using. · Make sure you have installed AND UPDATED...
Read more >
Editing Python in Visual Studio Code
The formatter is not installed in the current environment. Open a command prompt, navigate to the location where your selected interpreter is, and...
Read more >
Formatting | Python in Visual Studio Code
The default code format provider is autopep8. Auto Formatting. Formatting the source code as and when you save the contents of the file...
Read more >
Python in Visual Studio Code - October 2022 Release
You can now give the autopep8 extension a try by downloading it from the marketplace. If you have any issues or feature requests,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found