krishan duhan
krishan duhan

Reputation: 131

Passing commandline argument in google colab

How to pass commandline argument when running a python code in google colab? I have written a code which takes a file as input via sys.argv[]. How do I do this?

Upvotes: 1

Views: 13940

Answers (4)

user21136698
user21136698

Reputation: 1

I made a bioinformatic tool locally in my machine to parse Uniprot big data files of proteins. The tool I made needs the passing of different parameters using command line arguments. After the tool was working locally, I upload data files and python source files to my google drive. I did not make any changes to my files. I just run directly the following command in google colab:

!python3 drive/MyDrive/uniprot/uniprot_select.py FIELDS "ID,OS,SQ" FROM drive/MyDrive/data/uniprot.dat WHERE "SQ#EYDRRR" FASTA

It works perfectly!

No need of special parsing, no need to additional imports. All the work you normally do locally in your machine, can be executed without changes.

Upvotes: 0

CB Acnt
CB Acnt

Reputation: 91

So if you want to run a python code that works like this:

!python test.py --image_folder '/content/image' --workers 2 --Prediction CTC --rgb True

You have to open test.py or your file with editor then you will find line inside the file similer like this:

parser = argparse.ArgumentParser()
parser.add_argument('--image_folder', required=True, help='path to image_folder')
parser.add_argument('--workers', type=int, default=1, help='number of workers')
parser.add_argument('--Prediction', type=str, default='CTC', help='Prediction stage.')
parser.add_argument('--rgb', action='store_true', help='use rgb input')

args = parser.parse_args()

But this will give you " Error SystemExit: 2 "

Then you have to change like this:

parser = argparse.ArgumentParser()
parser.add_argument('--image_folder', required=False, default='/content/image', help='path to image_folder')
parser.add_argument('--workers', type=int, default=2, help='number of workers')
parser.add_argument('--Prediction', type=str, default='CTC', help='Prediction stage.')
parser.add_argument('--rgb', action='store_false', help='use rgb input')
parser.add_argument("-f", "--file", required=False)

args = parser.parse_args()

You must add in the end of " parser.add_argument " line:

parser.add_argument("-f", "--file", required=False)

Then you can call commandline argument like this:

image = args.image_path
Or
img = Image.open(args.image_path)

workers = args.workers

But if your last line like this:

args = vars(ap.parse_args())

Then you have to call it like this:

image = args["image_path"]
Or
img = Image.open(args["image_path"])

workers = args["workers"]

#Note ( action='store_false' ) will default to ( False )

Likewise, ( action='store_false' ) will default to ( True )

Tested with Google colab

Upvotes: 0

YOUSSEF MOUSSAAIF
YOUSSEF MOUSSAAIF

Reputation: 1

I tried this in a google colab notebook

import sys

sys.argv[0] = "first_arg" # this is to assign the first command line argument
sys.argv[1] = "second_arg" # This line to assign the second arg for example

And it worked for me.

Upvotes: 0

55597
55597

Reputation: 2132

As far as I know, there is no special way to pass command line arguments to python code. This is a working code sample I use to when creating tfrecords.

!python generate_tfrecord.py --csv_input=data/test_labels.csv --output_path=data/test.record --image_dir=images/

I don't see any difference between the regular command line python argument passing and the colab. Please add more code to your question to get better help.

Upvotes: 4

Related Questions