将argparse bash脚本转换为python脚本

发布于 2025-02-09 06:13:36 字数 272 浏览 2 评论 0原文

考虑下面的bash脚本。脚本argparse_test.py是一个具有各种变量的argparse脚本。

python -u argparse_test.py \
  --is_training 3 \
  --root_path ./dataset/ \
  --data_path data.csv \
  -/test_$seq_len'_'196.log 

我如何将此bash脚本转换为python脚本,以便我可以在带有各种变量的python iDE中运行它?

Consider the bash script below. The script argparse_test.py is an argparse script with various variables.

python -u argparse_test.py \
  --is_training 3 \
  --root_path ./dataset/ \
  --data_path data.csv \
  -/test_$seq_len'_'196.log 

How would I convert this bash script to a python script so that I could run it in a python IDE with the various variables?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

倚栏听风 2025-02-16 06:13:36

bash输入

python -u argparse_test.py \
  --is_training 3 \
  --root_path ./dataset/ \
  --data_path data.csv \
  -/test_$seq_len'_'196.log 

应产生sys.argv,看起来

['argparse_test.py', '--is_training', '3', '--root_path ./dataset/','--data_path', 'data.csv', '-/test_$seq_len'_'196.log'] 

这是一个粗略的猜测; bash可能正在处理斜线不同(我现在在Windows机器上)。

“在IDE中运行”是一个模糊的请求。

某些IDE具有运行菜单项,以及指定命令行参数的方法(通常在单独的菜单设置项目中)。

如果您的代码设置了解析器:

parser = argparse.ArgumentParser(...)
...

该行

args = parser.parse_args()

读取sys.argv [1:]并解析它。因此,使用解析器的另一种方法是直接提供等效的字符串列表:

argv = ['--is_training','3',...]
args = parser.parse_args(argv)

如果您可以修改脚本,则可以添加几个显示

import sys
print(sys.argv)

,然后在parse_args

print(args)

args将是argparse.namespace应像

Namespace(is_training=3, root_path='./dataset/', data_path='data.csv', ...)

vars(args)

dict具有相同键的dict的对象,并值

任何具有相同属性的Python对象都应与在您的代码其余部分中,args 。

我觉得这个答案太长而详细 - 因为这个问题的模糊性。

This bash input

python -u argparse_test.py \
  --is_training 3 \
  --root_path ./dataset/ \
  --data_path data.csv \
  -/test_$seq_len'_'196.log 

should produce a sys.argv that looks like

['argparse_test.py', '--is_training', '3', '--root_path ./dataset/','--data_path', 'data.csv', '-/test_$seq_len'_'196.log'] 

that's a rough guess; bash may be handling the slashes different (I'm on a windows machine now).

"running in an IDE" is a vague request.

Some IDE have a run menu item, and a way of specifying commandline arguments (usually in a separate menu setup item).

If you have code that setups up a parser:

parser = argparse.ArgumentParser(...)
...

the line

args = parser.parse_args()

reads that sys.argv[1:] and parses it. So an alternative way to use the parser is to provide the equivalent list of strings directly:

argv = ['--is_training','3',...]
args = parser.parse_args(argv)

If you can modify the script, you could add a couple of displays

import sys
print(sys.argv)

and after the parse_args

print(args)

args will be a argparse.Namespace object that should display like

Namespace(is_training=3, root_path='./dataset/', data_path='data.csv', ...)

vars(args)

is a dict with the same keys and values

Any python object with the same attributes should be have the same as args in the rest of your code.

I have a feeling this answer is too long and detailed - because of the vagueness of the question.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文