无法在Windows 7上使用argparse和python 3.4.2传递争论 [英] can't pass arguements using argparse and python 3.4.2 on Windows 7
问题描述
我已升级到Python 3.4.2和argparse(来自optparse),但似乎都无法识别命令行选项.作为一个简单的测试,我运行它;
I've upgraded to Python 3.4.2 and argparse (from optparse) but neither appears to recognise command line options. As a simple test I run this;
#test_argparse.py
def main():
import argparse
parser = argparse.ArgumentParser(description='Execute database query.')
parser.add_argument("-q", "--query", dest="query",
help="Name of the query file to be run")
args = parser.parse_args()
print(args)
if __name__ == '__main__':
main()
在运行时从命令行
test_argparse.py -q get_msre_for_book
我得到:
Namespace(query=None)
但是当我从IDE中运行时:
But when I run from within the IDE:
*** Python 3.4.2rc1 (v3.4.2rc1:8711a0951384, Sep 21 2014, 21:16:45) [MSC v.1600 32 bit (Intel)] on win32. ***
Command Line : -q get_msre_for_book
Namespace(query='get_msre_for_book')
我正在尝试通过以下方式启动脚本
I'm trying to launch the script via
os.system('python.exe', '<path to script>test_argparse.py -q get_msre_for_book')
但是失败了,因为没有显示查询,如上所示.我通过使用
But that fails as no query is presented as shown above. I've worked round this by launching using
subprocess.call
但是我宁愿不必这么做.再次感谢任何建议或想法.
but I'd rather not have to. Again, any suggestions or ideas gratefully received.
推荐答案
为解决这个问题,以下是解决该问题的2条评论:
To give closure to this question, these are the 2 comments that solve the problem:
关于在Windows 7中传递命令行参数,还有较早的SO问题.解决方案包括正确获取注册表链接. stackoverflow.com/questions/2640971、stackoverflow.com/questions/9880540.例如"C:\ Python \ Python33 \ python.exe"%1"%*.
There are earlier SO questions about passing commandline arguments in Windows 7. The solutions involve getting a registry link right. stackoverflow.com/questions/2640971, stackoverflow.com/questions/9880540. e,g, "C:\Python\Python33\python.exe" "%1" %*.
已解决问题-非常感谢-但确切地说,注册表项必须为C:\ Python \ Python33 \ python.exe"%1%"(添加了最后的双引号)
That fixed it - thank you very much - but to be precise the registry entry needs to be C:\Python\Python33\python.exe" "%1" %" (added the final double quote)
这篇关于无法在Windows 7上使用argparse和python 3.4.2传递争论的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!