How to run Python scripts with shell scripts

26 Sep 2022

In order to run my Python scripts from other places than VS Code (eg Streamdeck action, Hazel, Alfred, etc..) I cannot run the scripts as single lines command (eg python3 <url>) because they require their own virtual environments to be activated Managing Virtual Environments in Python

So there is a missing link, which I think shell scripts are for.

Shell scripts are text files or small computer programs that consist of a collection of UNIX commands. You use a shell script to execute commands that you would normally type on the command line. However, rather than inputting them one by one into a Mac’s terminal, you can use shell scripts to execute the said group of commands with just one single action.

For my needs:

create file

create a file in Atom, starting with #! /bin/zsh (Zsh being the shell in macOS Monterey, differs by system/preference), with .sh (shell) extension.

Each line is a terminal command line, like I would enter manually, eg:

#! /bin/zsh

source /Users/xxx/Python/transcribee/venv/bin/activate

python3 /Users/xxx/Python/transcribee/test.py

run script

Single terminal command:

zsh <path_to_sh_file>

to make a script executable

chmod +x <path_to_sh_file>

Once done, simply run the path to the file in the terminal.

use variable

define as VAR= and call with $VAR.

ex.:

CURRENTDATETIME=`date +"%Y-%m-%d %T"`

used as:

$CURRENTDATETIME

insert breakline

echo -e "Hellow\\nWorld"

The option -e causes the newline character to be interpreted.

open URL

open opens files and URLs in the default or specified application.

open https://stackoverflow.com

or with Safari:

open -a Safari https://stackoverflow.com

for Chrome:

open -a /Applications/Google\ Chrome.app https://stackoverflow.com

note the backslash \ to escape white spaces in paths.

displaying a notification if background shell script encounters an issue

13 Dec 2022

I'm running a lot of Python scripts these days using shell scripts.

One issue is that when one of the Python scripts within the shell scripts fails, the shell script continues to run, and I don't get any notification.

Example would be my static sites where the Python generator might fail, but the Github push proceeds anyway without the site being updated:

#! /bin/zsh

source /path/to/project/venv/bin/activate

python3 /path/to/project/generate_btobsales.py

cd /path/to/github/folder/

git add .

CURRENTDATETIME=`date +"%Y-%m-%d %T"`

git commit -m $CURRENTDATETIME

git push origin main

open -a /Applications/Google\ Chrome.app https://github.com/my_project

Solution seems to be as follows:

  • adding set -e to return an error if any command fails.
  • adding trap 'zenity --error --text="The Python script failed"' ERR to display a notification if an error is returned.

like:

#! /bin/zsh

set -e

source /path/to/project/venv/bin/activate

python3 /path/to/project/generate_btobsales.py

trap 'zenity --error --text="The Python script failed"' ERR

cd /path/to/github/folder/

git add .

CURRENTDATETIME=`date +"%Y-%m-%d %T"`

git commit -m $CURRENTDATETIME

git push origin main

open -a /Applications/Google\ Chrome.app https://github.com/my_project

However zenity is not installed on my Mac.

Needs to be installed with brew as brew install zenity.

Not working though.

TODO come back to this later.

Managing errors

16 Mar 2023

One issue has been that running shell scripts in the background does not raise any error if any of the Python scripts fail.

Workaround:

Append as follows in script python myscript.py 2>&1 | mailx -s "Script Error" youremail@example.com

#!/bin/bash

# Set the script name variable
SCRIPT_NAME="myscript.py"

# Run the script and send any errors via email
python "$SCRIPT_NAME" 2>&1 | mailx -s "Error in $SCRIPT_NAME" youremail@example.com

links

social