我有一个这样的简单脚本(基于the docs for argparse):
def Main():
parser = argparse.ArgumentParser()
parser.add_argument("issuenumber", help="Create a local branch based on the specified issue number", type=int)
args = parser.parse_args()
if args.issuenumber:
print("Starting work on issue #"+str(args.issuenumber))
if __name__ == "__main__":
Main()然而,当我运行它时,它永远不会识别我传递给它的参数:
C:\Projects\PyTools>Gritter.py 1
usage: Gritter.py [-h] issuenumber
Gritter.py: error: the following arguments are required: issuenumber但是,如果我通过python调用来调用脚本,它就能正常工作:
C:\Projects\PyTools>python Gritter.py 1
Starting work on issue #1如果我打印出sys.argv,我会得到:
C:\Projects\PyTools>Gritter 1
['C:\\Projects\\PyTools\\Gritter.py']
C:\Projects\PyTools>Python Gritter.py 1
['Gritter.py', '1']所以我猜当脚本被直接调用时,有些东西没有传递参数。我想知道是否有什么可以做的事情,以便脚本可以直接调用?
发布于 2016-04-09 00:49:57
C\指示您正在使用Windows。您需要付出额外的努力来确保这个“直接调用”将参数传递给python。
查找我在Python文档中找到的windows shebang,您需要使用
#!/usr/bin/python -v传递参数
请参阅https://docs.python.org/3/using/windows.html
argparse使用sys.argv。如果只有脚本名称,则调用不会传递参数。
发布于 2016-04-11 16:29:33
基于mckoss` answer,我修改了以下注册表项:
HKEY_CLASSES_ROOT\Applications\python.exe\shell\open\command从这个开始:
"C:\Python34\python.exe" "%1"要这样做:
"C:\Python34\python.exe" "%1" %*现在我的脚本可以像我之前预期的那样工作了。
https://stackoverflow.com/questions/36496541
复制相似问题