Python and VSCode

Gerhard Bräunlich

February 2025

Python ecosystem

  • language (REPL, interpreter)
  • packaging / package management
  • environment isolation
  • documentation
  • testing
  • code quality
    • linting
    • type checking
    • formatting

Setup

🐧 Visual Studio Code on Linux
🪟 Visual Studio Code on Windows
🍏 Visual Studio Code on macOS

By default, VSCode collects telemetry data

Disable: Settings > Search “telementry” > Telemetry: Enable Telemetry

{"telemetry.telemetryLevel": "off"}

Unofficial (FOSS) alternative: vscodium.

Interesting alternative: Zed

Basic features

Code Navigation
Rename Symbol
Integrated Terminal
Testing
Debug

Quick Open: Ctrl+P / ⌘+P

Start char Effect
Open file
> Command palette
@ Symbol lookup
# Symbol lookup
: Jump to line

Config

Via command palette: Ctrl+⇧+P / ⌘+⇧+P or F1

Or directly: Ctrl+, / ⌘+,

Config path (share / backup config):

$HOME/.config/Code/User/settings.json # 🐧
%APPDATA%\Code\User\settings.json # 🪟
$HOME/Library/Application Support/Code/User/settings.json # 🍏

Per project config:

.vscode/settings.json

Install extensions

Via extensions tab
Via Quick Open (Ctrl+P / ⌘+P) -> ext install <extension-id> -> Enter

Python extensions

Basic python extension

  • Code completion
  • Code navigation
  • Debugging
ext install ms-python.python

linting (ruff)

Scans for

  • violation of code style conventions
  • bad practices
  • errors (syntax / undefined variables / …)

Why lint?

Example:

def prepare_config(base_config={}, **kwargs):
    base_config.update(kwargs)
    return base_config


print(prepare_config(a=1, b=2))
print(prepare_config(c=3))

Output 😲:

{'a': 1, 'b': 2}
{'a': 1, 'b': 2, 'c': 3}
 ruff check example.py 
example.py:1:25: B006 Do not use mutable data structures for argument defaults
  |
1 | def make_args(base_args={}, **kwargs):
  |                         ^^ B006
2 |     base_args.update(kwargs)
3 |     return base_args
  |
  = help: Replace with `None`; initialize within function

Found 1 error.
No fixes available (1 hidden fix can be enabled with the `--unsafe-fixes` option).

ruff extension

ext install charliermarsh.ruff

Ships with: ruff

(Legacy) alternatives: pylint, flake8, …

Auto formatting (via ruff)

  • minimize commit conflicts
  • code review on actual changes only
  • focus on coding
  • automate the formatting

Settings:

{
  "[python]": {
    "editor.formatOnSave": true,
    "editor.defaultFormatter": "charliermarsh.ruff",
    "editor.codeActionsOnSave": {
      "source.organizeImports": "explicit"
    }
  }
}

Alternatives: black, autopep8, …

Type checking (mypy)

  • avoid runtime errors
  • typehint consistency
  • typehints via IDE
  • code awareness

Why type checking?

d = {"a": 1, "b": 2}
d.get("a") + d.get("B")  # 💥
Traceback (most recent call last):
  File "/tmp/example.py", line 2, in <module>
    d.get("a") + d.get("B")
    ~~~~~~~~~~~^~~~~~~~~~~~
TypeError: unsupported operand type(s) for +: 'int' and 'NoneType'
 ruff check example.py
All checks passed!
 mypy example.py 
example.py:2: error: Unsupported operand types for + ("int" and "None")  [operator]
example.py:2: error: Unsupported operand types for + ("None" and "int")  [operator]
example.py:2: error: Unsupported left operand type for + ("None")  [operator]
example.py:2: note: Both left and right operands are unions
Found 3 errors in 1 file (checked 1 source file)

A type checker also checks if your typehints are consistent:

def f(x: float) -> float:
    return x


def g(x: int) -> int:
    return f(x)
example.py:6: error: Incompatible return value type (got "float", expected "int")  [return-value]

mypy extension

ext install ms-python.mypy-type-checker

Ships with: mypy

Alternatives: pyright, …

Working remotely

Remote SSH

ext install ms-vscode-remote.remote-ssh # ⚠️ proprietary

Requires: Setup of SSH keys

Euler: Use the Euler-tunnel to prevent jams on login nodes

Working in a container

Remote container

ext install ms-vscode-remote.remote-containers # ⚠️ proprietary

Requires: docker

Other useful extensions

Code Spell Checker

ext install streetsidesoftware.code-spell-checker

Git graph

  ext install mhutchie.git-graph

FAQ

Install proprietary extensions in VS Codium

Remote ssh: Grab a release from here

 codium --install-extension \
  downloads/ms-vscode-remote.remote-ssh-*.vsix

Remote container: Grab a release from here

 codium --install-extension \
  downloads/ms-vscode-remote.remote-containers-*.vsix

VS Code Marketplace: https://marketplace.visualstudio.com

VS Codium repository: https://open-vsx.org

Acknowledgements

Slides:

The End

📊 Slides: ✨ Live version / 📖 Source