Karambir Nain

My Python Programming Workflow

Note: This is a live document with pointers, not a blog post

How do I do python programming.

Basic Setup

Setup asdf to manage multiple python, node and golang versions

Install python versions using asdf:

asdf install python 3.9.21
asdf install python 3.10.16
asdf install python 3.11.11

Install pipx using system package manager like pacman, apt, brew. It creates separate virtualenvs for each python cli tool you install. Tell pipx to use one of the python version installed by asdf by adding following to .bashrc or .zshrc:

PIPX_DEFAULT_PYTHON=/home/user/.asdf/installs/python/3.11.11/bin/python

Use pipx to manage python cli software

Project Setup

Activate correct python version using asdf asdf local python 3.11.4. This creates a .tool-versions file in local directory so in-future it automatically picks correct version. This file can be committed to git as well.

Create and use virtualenv using built-in venv package via bash aliases

alias cv='python -m venv .venv'
alias cv='python -m venv .venv'

Poetry can also be used to create and activate virtualenv automatically using poetry run commands. Configure poetry with:

poetry config virtualenvs.create true
poetry config virtualenvs.in-project true

Why not using mkvirtualenvwrapper?

Use Poetry to install and manage dependencies.

For linting:

For documentation and versioning:

For testing:

Keep most configs for these packages in pyproject.toml. If not, use setup.cfg. Avoid tool specific config file.

Use Makefile with frequently used commands.

Editor Setup

Use Vim or Vim Mode in Pycharm

Have plugins like EditorConfig

root = true

[*]
charset = utf-8
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true

[*.{py,rst,ini}]
indent_style = space
indent_size = 4

[*.{html,css,scss,json,yml}]
line_length=120
indent_style = space
indent_size = 2

[*.md]
trim_trailing_whitespace = false

[Makefile]
indent_style = tab

For pycharm, setup configurations to run tests and tool being developed.

Major packages I find awesome

While Programming

Use extensive logging with packages like loguru. Enable debug logging locally for external calls like File IO, db or network requests. Try using structured logging(json?) to attach extra context to logs

CI

Use GitHub actions or Gitlab CI for testing, building and releasing.

#setup #python