'Visual Studio Code and Autopep8 Formatter

I am trying to setup my python formatter to autopep8. It is installed properly, however it never as a formatting option in VSCode.

"python.formatting.autopep8Path": "C:\\Users\\DawsonSchaffer\\AppData\\Roaming\\Python\\Python39\\Scripts\\autopep8.exe",
    "python.formatting.autopep8Args": [
        "--max-line-length",
        "160",
        "--aggressive",
        "--aggressive",
        "--in-place"
    ],
    "[python]": {
        "editor.defaultFormatter": "autopep8",
    },

enter image description here

I have changed all the setting. However I'm not sure what to put for autopep8.

"[python]": {
    "editor.defaultFormatter": "ms-python.python",
},

or

"[python]": {
    "editor.defaultFormatter": "autopep8",
},

Also autopep8 is not available in control pallet.

enter image description here

Thanks Dawson



Solution 1:[1]

This is to configure the

  "[python]": {
    "editor.defaultFormatter": "ms-python.python"
  },

in the settings.json.

You can set like this to apply autopep8 formatting:

"python.formatting.provider": "autopep8",
"editor.formatOnSave": true,
"files.autoSave": "afterDelay",

Related official docs.

Solution 2:[2]

To keep it all inside your .venv dependencies, add the dependency paths in the .config/settings.json, as in this example based on VS Code documentation:

{
  "python.linting.pylintEnabled": true,
  "python.linting.pylintPath": ".venv/bin/pylint",
  "python.formatting.autopep8Path": ".venv/bin/autopep8"
}

This way you don't have to rely on local installs and manual tasks. It's all versioned.

Just make sure you declare those dependencies (dev is the correct type) and that you source the environment (AKA: activate it). This is a Pipfile example but anything works.

[dev-packages]
autopep8 = "*"
pylint = "*"

In case you're wondering how to use in-project environments I'll add some examples.

Venv/Virtualenv

There's no big deal here, just create it in-project as usual with the one you prefer:

# venv
python3 -m venv .venv

# virtualenv
python3 -m virtualenv .venv

Activation: source .venv/bin/activate

Pipenv

For Pipenv specifically you have three methods of using in-project .venv:

  1. Create the .venv directory before installing the dependencies (not in docs for some reason).
  2. Set WORKON_HOME environment variable to your .venv directory.
  3. Set PIPENV_VENV_IN_PROJECT to 1 or True.

Activation: pipenv shell

Currently it is not possible to do it in a dedicated config file. Also, if you set these environment variables in a .env file they will not work, as explained in the same link.

Poetry

You can explicitly configure Poetry to use {project_home}/.venv by adding a configuration with the --local option like this:

poetry config virtualenvs.in-project true --local

This will create a poetry.toml file which you can then keep as part of your code and not having to worry about it anymore.

[virtualenvs]
in-project = true

Activation: poetry shell

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Steven-MSFT
Solution 2