Running Python and Shell scripts in Terminal, and passing arguments

In the Python script

import sys
var1= eval(sys.argv[1])
assert isinstance(var1, int)
var2= eval(sys.argv[2])
assert isinstance(var2, bool)

myfunc(var1,var2)

Asserting because passed arguments in terminal are only considered texts. The first argument sys.argv[0] is reserved to the file name itself and can't be used or assigned.

In the shell script

cd file_path_I_need
conda deactivate
conda activate <myenv>

#just to see which is which
echo "the file name: $0"
echo "first argument: $1"
echo "second argument: $2"

python3 path_to_python_script.py $1 $2

Shell files doesn't take arguments names, only their position, and they need to be declared with the dollar sign beforehand. The first argument can't be reassigned, it's reserved to the .sh file name itself.

What to run in iTerm

source path_to_shell_file.sh 4 True

Here I called the .sh file I need, and passed two arguments to it that are taken in the Python script in it.

Important: use source not sh to run the .sh script, that will save you a ton of headache like I had; if you run sh it won't execute the conda commands and will error out, you need source for that.

☞ Instructions on how to download and customize iTerm are on a dedicated page for that.

Last updated