Search code examples
pythonbashhpc

Pass values from bash script to python script


I'm working with an HPC where I write a bash script which executes my python_script.py job like so:

#!/bin/bash -l
#$ -l h_rt=0:00:10
#$ -l mem=1G
#$ -cwd

module load python3/3.8
python3 python_script.py

In my python_script.py I define the variables repeat, and directory like so:

repeat = 10
directory = r'User/somedir/'

I would like to be able to set these variables in my bash script, so that they overwrite these values within python_script.py and are used instead.


Solution

  • The easier way is to use in your python script :

    import sys
    repeat = sys.argv[1]
    directory = sys.argv[2]
    

    and in your bash script

    repeat="10"
    directory="User/somedir/"
    python3 python_script.py $repeat $directory