skip to Main Content

I have both Autopep8 and Black Formatter Installed in VS Code. I don’t want to wrap python code to new lines. Please provide different ways to do this.

I tried to format this line, So that wrapping will go away.

definition_mst_id = self.request.query_params.get(
    'defination_id', None)

I tried this:

"python.formatting.provider": "autopep8"
"python.formatting.autopep8Args": [
    "--max-line-length=200"
]

But it gives this error.
This setting will soon be deprecated. Please use the Autopep8 extension or the Black Formatter extension. Learn more here: https://aka.ms/AAlgvkb.(2)

Then I tried this:

"[python]": {
    "editor.defaultFormatter": "ms-python.black-formatter",
    "editor.wordWrap": "off",
    "editor.wordWrapColumn": 1000,
},
"editor.wordWrapColumn": 1000,
"editor.wrappingIndent": "none",
"debug.console.wordWrap": false,

But, this is also not working.
When I try Format Selection, I get following error:
Extension 'autopep8' is configured as formatter but it cannot format 'python'-files
Which is same for Black.

2

Answers


  1. If you want to use Autopep8 and disable line wrapping, you can try the following configuration in your VS Code settings:

    {
        "python.formatting.provider": "autopep8",
        "python.formatting.autopep8Args": ["--max-line-length=1000"],
        "editor.wordWrap": "off",
        "editor.wordWrapColumn": 1000
    }
    

    The "python.formatting.autopep8Args" line sets the maximum line length to a very high value (1000 in this case), effectively disabling line wrapping for most cases. The "editor.wordWrap" and "editor.wordWrapColumn" settings ensure that the editor itself does not wrap lines.

    If you prefer Black Formatter, you can configure it like this:

    {
        "python.formatting.provider": "black",
        "editor.wordWrap": "off",
        "editor.wordWrapColumn": 1000
    }
    

    This configuration sets Black as the default Python formatter and disables line wrapping in the editor.

    You can also create an .editorconfig file in your project directory with the following settings to enforce a consistent code style across different editors, including Visual Studio Code:

    # .editorconfig
    [*.py]
    max-line-length = 1000
    

    With this configuration, you set the maximum line length to a high value (1000), which effectively disables line wrapping. Make sure you have the "EditorConfig for VS Code" extension installed in VS Code to recognize and apply these settings.

    Remember to adjust the max-line-length value to your preferred maximum line length if 1000 is not suitable for your project.

    Login or Signup to reply.
  2. This settings.json should be enough to do what you want.

    {
      "[python]": {
        "editor.defaultFormatter": "ms-python.autopep8"
      },
      "autopep8.args": [
            "--max-line-length",
            "120"
        ]
    }
    

    "python.formatting.provider": "autopep8" is deprecated as is does not refer to the autopep8 extension but the "bundled" autopep8.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search