I often still do a lot of local testing on slightly long running scripts, I have some linting setups, but I'm often just using VIM on a fairly raw VM, and I've not quite cracked getting my perfect editing setup sorted quickly.

What would often help me is if every time I tried to run a python script, black ran over it first, to check everything was valid python.

Simple, but would just save a few errors at the end of long running scripts.

1

There are 1 best solutions below

0
On

If you want a simple way of running black on a python file and then running the python file itself, you can simply define a bash function like this:

runpy() {                                                                                                                                                                                                                       
    if ! black "$1"; then
        echo "Black encountered an error. Aborting."
        return 1
    fi
    python3 "$@"
}

You can then run it as:

runpy my_python_file.py argument1 --option2

It will first run black on $1 which is the python file, and then python3 $@ where $@ means all the arguments. The if block makes sure the function exits if black returns an error. (Put it into your bashrc or zshrc or whatever shell you use to source it whenever you start a terminal)