Notebooks: Passing string variables as arguments to python script via command line(!), using quotes And $, eg -arg1 '$varible1'

两盒软妹~` 提交于 2020-08-10 05:35:12

问题


In using variables in the command line from a notebook cell, I saw that we can use put a $ in front of the variable, or surround the variable using {} , for example

!command {variable}

or

!command $variable

But when I was running a python script using the command line from a notebook cell, I would get errors

variable1 = '/path/to/directory'
variable2 = 7

!Script.py -arg1 $variable1 -arg2 $variable2

and

!Script.py -arg1 {variable1} -arg2 {variable2}

did not work.

After experimenting a little bit, I found that if a variable is a string, surrounding the whole arg with quotes got it to work.

variable1 = '/path/to/directory'
variable2 = 7

!Script.py -arg1 '$variable1' -arg2 $variable2 

What is going on? I tried to look up this phenomena but I couldn't find anything.

If it makes a difference, I am using google colab colaboratory


回答1:


Have you tried?

!Script.py -arg1 $variable1\ -arg2 $variable2\ 


来源:https://stackoverflow.com/questions/57330956/notebooks-passing-string-variables-as-arguments-to-python-script-via-command-li

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!