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.
- if not already installed:
pip install poetry
- upgrade pip:
python -m pip install --upgrade pip
- 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]
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.
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.
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"
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"],
},