pythonargumentsgoogle-colaboratory

Run python script with integer arguments & variables in Colab


In Colab, I set variables outside and I want to pass them to a python script I call. Normal methods with {VAR} or $VAR do not work. What I understand, as it opens a new shell to run it, all is passed as strings and the python script does not have any conversion utilities.

MYVAR=3

!python3 myscript.py --int-arg {MYVAR}
!python3 myscript.py --int-arg $MYVAR

The first version complains:

error: argument --int_arg: invalid int value: '{MYVAR}'

The second version complains:

error: argument --int_arg: expected one argument

How can I handle this?

EDIT: As indicated in the comment, the following question might seem similar, but as you can see, I've already tried more than the answer in that topic proposed, and the reason came out very different, at least in the Colab version at that time (2 1/2 years before). Run a python file with arguments in Colab

It is usual in many programming languages to specify constants in capitals, like I did, but this caused the problem.


Solution

  • It turns out, that Colab's implementation does not like capitals. The following works:

    my_var=3
    
    !python3 myscript.py --int-arg {my_var}