Argparse 教程示例在 Windows 命令行中不起作用 [英] Argparse tutorial example doesn't work in windows command line

查看:30
本文介绍了Argparse 教程示例在 Windows 命令行中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我试图通过教程此处.该示例由以下保存为 tut.py 的代码给出.

So I'm trying to teach myself how to use the python library argparse via the tutorial here. The example is given by the following piece of code which is saved as tut.py.

import argparse

parser = argparse.ArgumentParser(description='Process some integers.')
parser.add_argument('integers', metavar='N', type=int, nargs='+',
                help='an integer for the accumulator')
parser.add_argument('--sum', dest='accumulate', action='store_const',
                const=sum, default=max,
                help='sum the integers (default: find the max)')

args = parser.parse_args()
print(args.accumulate(args.integers))

在教程中,他们在命令行中的每个命令之前放置了一个 $,我认为这是由于他们使用 Linux.首先,如果我在任何命令之前在 Windows 命令行中添加一个 $ ,我会收到错误

In the tutorial they put a $ before every command in the command line which is due to them using Linux I think. First if I add an $ in my windows command line before any command I get the error

The "$" command was either misspelled or could not be found.

如果我再跑

 python tut.py 1 2 3 4

我没有收到错误消息,但命令行中也没有显示任何输出.但是,预期的是这些整数的总和.

I don't get an error but neither is any output displayed in the command line. What would be expected is the sum of those integers though.

如何让输出显示在命令提示符中?

How can I make the output show up in the command prompt ?

推荐答案

一开始我认为指定我使用 anaconda 分发版 Python 并不重要,但是它变成了使用 anaconda 命令提示符而不是 windos 10 one 可以解决问题.

At the beginning I didn't think that it was important to specify that I use the anaconda distribution for python, however it turns out that using the anaconda command prompt instead of the windos 10 one solves the problem.

(base) C:\Users\Admin\Desktop\HiWi\Codect>python tut.py 1 2 3 4
4

(base) C:\Users\Admin\Desktop\HiWi\Codect>python tut.py 1 2 3 4 --sum
10

(base) C:\Users\Admin\Desktop\HiWi\Codect>

这篇关于Argparse 教程示例在 Windows 命令行中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆