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

查看:16
本文介绍了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 引起的,以及如何尝试在命令行上执行代码.在给定的示例中,测试脚本直接在命令行上调用,代码前没有 python,如建议的 在这个答案中.

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天全站免登陆