繁体   English   中英

Windows上带有参数的Python程序

[英]Python program with arguments on Windows

如何在安装了python 2.7的Windows XP上运行保存在文件test.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)

我试图用命令行运行它。 例如

$ python test.py 1 2 3 4

要么

$ python test.py 1 2 3 4 --sum

给出错误“无效语法”。

我尝试在命令行上运行脚本,它可以完美运行:

$ python arg.py 1 2 3 4 --sum
10

在上面, $是shell的提示符。 我输入的是python arg.py 1 2 3 4 --sum 有用。

现在,让我们做我怀疑您正在做的事情:让我们启动一个交互式python shell并输入上面的内容:

$ python
Python 2.7.12+ (default, Aug  4 2016, 20:04:34) 
[GCC 6.1.1 20160724] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> python test.py 1 2 3 4 --sum 
  File "<stdin>", line 1
    python test.py 1 2 3 4 --sum 
              ^
SyntaxError: invalid syntax

这将生成您看到的SyntaxError: invalid syntax错误。 (有一个小的区别:我在Linux上,而您在Windows上。)

解决方案是退出python交互式shell,然后在命令提示符下输入命令。

这只是我天真,但考虑到您发布的简短错误消息...

您是否有机会从本书中获得此代码并尝试在命令行上运行它?

本书使用$标记命令行/终端命令,但实际上该字符不是您应使用的语法或命令的一部分。

因此,而不是运行此:

$ python 1 2 3

运行这个:

python 1 2 3

您的test脚本是Python argparse文档上的第一个示例。 https://docs.python.org/3/library/argparse.html

您添加了新行的评论是

Python 2.7.8 (default, Jun 30 2014, 16:03:49) 
[MSC v.1500 32 bit (Intel)] on win32 
Type "copyright", "credits" or "license()" for more information. 
>>> ================================ RESTART ================================ 
>>> usage: test [-h] [--sum] N [N ...] test: error: too few arguments 
>>> $ python test.py 1 2 3 4 
SyntaxError: invalid syntax 
>>> python test.py 1 2 3 4 
SyntaxError: invalid syntax 
>>> $ python test.py 1 2 3 4 
SyntaxError: invalid syntax 
>>> python test.py 1 2 3 4 --sum 
SyntaxError: invalid syntax 
>>> python test.py 1 2 3 4 --sum

据此,我推断出您将脚本另存为test (最好使用“ test.py”),然后从Windows命令行运行该脚本,如下所示:

python -i test

产生

usage: test [-h] [--sum] N [N ...] test: error: too few arguments

来自parser使用消息; test是脚本的名称。

我不确定RESTART行。 我的测试(最后)建议您的Python调用(或某些默认环境功能)包括-i选项,即使在argparse步骤失败后,该选项也将使您进入交互式Python会话。

下一个命令直接来自Python示例:

>>> $ python test.py 1 2 3 4 
SyntaxError: invalid syntax 

但是上下文都是错误的。 文档中包含$以指示正在命令行(Linux shell或Windows commmand)中键入此内容。 在正确的上下文中,含义是:

  • 运行Python
  • 告诉它运行test.py脚本
  • 并将参数“ 1”,“ 2”等传递给它

但是,如果您已经在Python解释器中(由>>>提示字符串指示),则没有任何意义。 pythontest.py是在Python内部没有默认含义的字符串。 因此,解释器会为您提供语法错误。 而且没有任何变种可以解决该问题。

再进一步, argparse文档提供了一个示例,该示例从Python交互式会话中调用parser

>>> parser.parse_args(['--sum', '7', '-1', '42'])

那有非常不同的语法。 在此python -i上下文中,它应该运行。

返回Windows命令窗口并输入

python test 1 2 3 4

有更好的工作机会。 如果那行不通,那么您/我们需要专注于运行更基本的Python脚本。

=========

这是从Linux Shell运行另一个简单脚本的示例。 ...$是shell提示符; >>>是python提示符。 -i添加到初始python调用中可确保其在解析后仍保留在python中。

0957:~/mypy$ python -i simple.py
usage: simple.py [-h] foo
simple.py: error: too few arguments
Traceback (most recent call last):
  File "simple.py", line 4, in <module>
    print(parser.parse_args())
   ...
SystemExit: 2
>>> python simple.py 1 2
  File "<stdin>", line 1
    python simple.py 1 2
                ^
SyntaxError: invalid syntax

我的测试与您的测试之间的主要区别在于,我没有得到RESTART ,而是得到了追溯。 如果没有-i我只会得到用法消息并返回命令行。

1000:~/mypy$ python simple.py
usage: simple.py [-h] foo
simple.py: error: too few arguments
1000:~/mypy$

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM