Windows 7上的python3.2.3中的argparse似乎无法解析 [英] argparse in python3.2.3 on windows 7 does not seem to parse

查看:194
本文介绍了Windows 7上的python3.2.3中的argparse似乎无法解析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于我在Windows上运行python,因此这是我在argparse中遇到的下一个问题,但没有找到解决方案.我以前使用过optparse.这是我的代码:

since I got python on windows running, here is the next problem I encountered with argparse, and for which I did not see a solution. I uses optparse before. Here is my code:

import argparse
parser = argparse.ArgumentParser(
        description = 'Test description')       # main description for help

parser.add_argument('-d', '--dir',                 # -u or --user option           
        dest = "dir",
        help = 'directory to start with')           
args = parser.parse_args()
print(args.dir)

但是当我使用

code.py -d test
code.py --dir test

我总是得到一个None作为输出.我觉得这是一件琐碎的事情,也是我忽略的显而易见的事情,但我看不到它.

I always get a None as output. I feel this is something trivial, and something obvious I overlooked, but I cannot see it.

坦克

亚历克斯

推荐答案

问题似乎是由Windows引起的,以及如何尝试在命令行上执行代码.在给定的示例中,按照建议

The problem seem to be caused by Windows, and how the code is tried to be executed on the command line. In the given example the test script was called directly on the command line, without python before the code, as suggested in this answer.

如果代码像这样执行

python code.py

可以看到预期的行为,并且在代码中正确解析了参数.

the expected behavior is seen, and the arguments are correctly parsed in the code.

因此,Windows系统的安装尚未完成,或者以上链接中的建议未完成.

So either the setup of the Windows system is stil incomplete, or the suggestion in the above link is incomplete.

这篇关于Windows 7上的python3.2.3中的argparse似乎无法解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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