Python development set up

When I cd into a folder, I like the environment to be automatically ready to go. No having to figure out versions of tools, or set environment variables. To do that, I use mise.

For Python, I’ve found this awkward only because there seem to be lots of ways to manage and isolate an environment. I've settled on the built in venv support in mise. It goes like this for a new project:

Step 1: Create a mise.toml file. For example:
[tools]
python = "3.12"

[env]
_.python.venv = { path = "venv", create = true }

Step 2: One-time steps to bless the file, and install dependencies. In place of miniconda’s environment file, I use the pip requirements file
$ mise trust

$ cat requirements.txt
markdown2
pyyaml

$ pip install -r requirements.txt
And that’s it. The environment is isolated, and ready to go when I’m in the directory.

Step 3: Update, at some point
I tend to take a copy of the installed version numbers:
pip freeze -r requirements.txt --local > pip-freeze.txt
And also sometimes update:
pip list --local --outdated
pip install -r requirements.txt --upgrade

Working with Conda (miniconda)

There's experimental support in mise to trigger conda. I've found this mise.toml incantation is needed to trigger a conda environment called "my-env":
[settings]
experimental = true

[hooks.enter]
shell = "zsh"
script = """
source /Users/richard/opt/miniconda/etc/profile.d/conda.sh
conda activate my-env
"""

Obviously you'd need to adjust that to the path of your own miniconda install.

Helix

For developing, I’ll use a mix of helix or code, or marimo. For Helix to have the right language server for the virtual environemnt, I also will need to do pip install pyright as I have the following in ~/.config/helix/languages.toml:
[[language]]
name = "python"
language-servers = [ "pyright" ]