1

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.

8
  • 2
    Pass the variables as arguments to your script. Or set them as environment variables in your bash script, then use os.environ in your Python script. Commented Mar 16, 2022 at 12:43
  • 2
    From the limited information in your question, I'd go with my first suggestion: pass the necessary variables as arguments to your script. Dive into the argparse module to make this easy (and safer) for your script. Commented Mar 16, 2022 at 12:44
  • Pendatic nitpick: your user name is missing an auxiliary verb. Commented Mar 16, 2022 at 12:47
  • 1
    Does this answer your question? How to read/process command line arguments? Commented Mar 16, 2022 at 13:07
  • 1
    I guess they should have taken the hobbits to Bree, that would have fit. Commented Mar 16, 2022 at 14:26

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

1 Comment

For anyone else trying this: I found everything comes through as a string (even if using repeat=10 instead of repeat='10'), so just had to fix with repeat_int = int(repeat) in the python script and then use repeat_int from there on

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.