VS Code Setup for Python

Getting Started with Python in VS Code is a great tutorial for setting up VS Code. This note we’re covering relevant aspects of the setup {for the impatient}.

Following is contents of .vscode/settings.json

{
"editor.tabSize": 4,
"editor.rulers": [79, 120],
"editor.formatOnSave": true,
"python.linting.lintOnSave": true,
"python.pythonPath": "env/bin/python",
"python.linting.enabled": true,
"python.linting.pylintEnabled": true,
"python.linting.pylintPath": "env/bin/pylint",
"python.formatting.provider": "autopep8",
"python.formatting.autopep8Path": "env/bin/autopep8",
"python.formatting.autopep8Args": [
    "--max-line-length",
    "120",
    "--experimental"
    ]
}

Note

If you’re using pipenv, you can run pipenv where to figure out base directory where virtual environment is installed

In crux following is what we’re doing

  1. We’re creating .vscode/settings.json which is like a workspace configuration file {within your project/repo}
  2. In this file we’re specifically defining
    1. python.pythonPath which points to Python binary of your virtual environment
    2. python.linting.pylintPath specifies binary of your linter. {View list of officially supported Linters}
    3. python.formatting.provider, python.formatting.autopep8Path and python.formatting.autopep8Args are setting we’re using for formatters. {View list of officially supporte Formatters}

Associated with this there is also Automate Python workflow using pre-commits: black and flake8

bandit is another tool that checks for security issues in Python code

References:

  1. Get Productive with Python in Visual Studio Code by Dan Taylor
  2. How Code Linters Can Help You Write More Pythonic Code