Skip to content

Instantly share code, notes, and snippets.

@valosekj
Last active June 20, 2023 15:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save valosekj/205d0659ece3aa374981e0f8f310368c to your computer and use it in GitHub Desktop.
Save valosekj/205d0659ece3aa374981e0f8f310368c to your computer and use it in GitHub Desktop.
Run python script from shell #blog

Run python script from shell (bash, zsh)

Sometimes, I need to call/run python script with arguments from shell. I use bash or zsh as my shell and following workaround works great for me.

Using shell wrapper

  1. Some example python script which I need to run from bash:

my_python_script.py:

import os
import glob
import argparse

ext_files = '.nii.gz'


def get_parser():
    parser = argparse.ArgumentParser(
        description="Show all .nii.gz file in subject's directory.",
        add_help=True,
        prog=os.path.basename(__file__))

    parser.add_argument(
        '-path-data',
        required=True,
        metavar='<path_data>')

    parser.add_argument(
        '-sub',
        required=True,
        metavar='<subject_ID>')

    return parser.parse_args()


def main():
    # fetch input arguments
    args = get_parser()

    fname_data = os.path.join(args.path_data, args.sub)
    fname_files = glob.glob(fname_data + '/*' + ext_files)

    print(fname_files)


if __name__ == "__main__":
    main()
  1. Shell wrapper for above-mentioned python script:

wrapper_for_my_python_script.sh using venv:

#!/bin/bash

if [[ $# -ne 2 ]] || [[ $1 =~ "-h" ]];then
    echo -e "Help for my function"
else
    /path_to_venv/venv/bin/python /path_to_script/my_python_script.py -path-data $1 -sub $2
fi

wrapper_for_my_python_script.sh using conda:

#!/bin/bash

if [[ $# -ne 2 ]] || [[ $1 =~ "-h" ]];then
    echo -e "Help for my function"
else
    # Activate conda env
    eval "$(conda shell.bash hook)"
    conda activate <ENV_NAME>
    python /path_to_script/my_python_script.py -path-data $1 -sub $2
    conda deactivate
fi
  1. Finally, you can simply run your .sh script from your terminal to call the python script:
$ ./wrapper_for_my_python_script.sh /home/user sub-001

Call python script directly from shell

my_python_script.py:

import sys
import pandas as pd

# Fetch filename (passed as input argument)
filename = sys.argv[1]

# Read values and store them into pandas dataframe
pd_df = pd.read_csv(filename + '.csv')

Call from shell script can then looks like:

python3 ${PATH_TO_SCRIPT}/my_python_script.py ${filename}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment