简体   繁体   English

在python命令行中输入解析参数作为变量?

[英]Input parse argument as a variable in python command line?

I´m trying to input a string variable as a parse argument.我正在尝试输入一个字符串变量作为解析参数。

Run the code with following line:使用以下行运行代码:

for i in range(0,5):
  para=str(i)+"test.params"
  !python test.py --model I3D_resnet50 --resume-params para
 

The parse argument is added in test.py :test.py添加了 parse 参数:

parser.add_argument('--resume-params', type=str, default='default.params',
                        help='path of parameters to load from.')

But the code is not working, even if i add nargs='*' in parse.add_argument .但是,代码是不能工作,即使我添加nargs='*'parse.add_argument Is it possible that a variable can be input the parse argument?是否可以将变量输入解析参数?

I have a little script that echos the sys.argv :我有一个与sys.argv相呼应的小脚本:

In [173]: cat echo.py
import sys
print(sys.argv)

In an ipython session I can call it with:ipython会话中,我可以使用以下命令调用它:

In [174]: !python3 echo.py test commandline
['echo.py', 'test', 'commandline']

Or using a para variable:或者使用一个para变量:

In [175]: para = "test line"
In [176]: !python3 echo.py $para
['echo.py', 'test', 'line']

The sys.argv echo could be replaced with a working argparse parser. sys.argv echo 可以用一个有效的argparse解析器替换。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM