Skip to content

Instantly share code, notes, and snippets.

@franTarkenton
Last active May 29, 2024 18:43
Show Gist options
  • Save franTarkenton/786cf352526749839c980d14af7e24a4 to your computer and use it in GitHub Desktop.
Save franTarkenton/786cf352526749839c980d14af7e24a4 to your computer and use it in GitHub Desktop.
VSCode / Python Setup

Intro

Overview

Every time I setup VSCode for a python project I end up doing it incrementally. This is a guide to help me with a default env / configuration.

Poetry

Global config

  • if not already installed:

pip install poetry

  • upgrade pip:

python -m pip install --upgrade pip

Project specific related config

  • init the project creating the pyproject.toml
poetry init
  • install any dependencies defined in the init
poetry install
  • install project specific dependencies
poetry add <package-name>
  • install dev dependencies
poetry add --dev <package-name>
  • define specific version
poetry add --dev [email protected]

VSCode Extensions

Extensions that I install:

  • autoDocstring
  • black formatter
  • flake8
  • isort
  • ruff
  • pylance

Many of these extensions may install by default when you install the microsoft python plugin.

VSCode Python Config

Specify Interpreter

Ctrl->Shift->P then "Select Python Interpreter", the interpreters are grouped by their type, look for the Poetry group and choose the option for the interpreter associated with the project.

Manually activate poetry env in terminal

poetry shell

I've had issues where the shell isn't actually activating the venv, however it should output the location of the venv allowing you to manually activate it.

kjnether@NG401489:~/rfc_proj/climate_obs$ poetry shell
Spawning shell within /home/kjnether/.cache/pypoetry/virtualenvs/climate-obs-yi146A03-py3.11
. /home/kjnether/.cache/pypoetry/virtualenvs/climate-obs-yi146A03-py3.11/bin/activate
kjnether@NG401489:

If you run into this bug... it sounds like poetry shell is on of the bugger commands in the poetry package. A workaround is to create the following alias: reference

alias acpoet="source $(poetry env info --path)/bin/activate"

Configure Formatting

Having installed the isort plugin to vscode, and black to your python venv as dev dependency, adding the following lines should setup vscode for formatting and for sorting your imports.

"[python]": {
        "editor.defaultFormatter": "ms-python.black-formatter",
        "editor.codeActionsOnSave": {
            "source.organizeImports": true
        },
        "isort.args":["--profile", "black"],
},
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment