当 运行 python 文件直接在 Windows 上时命令行参数被丢弃
Command-line arguments dropped when running python file directly on Windows
我正在学习在我的代码中包含命令行参数。我已经阅读了 argparse
的文档,并尝试从那里 运行 使用此脚本。
#argparse_trial.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 argparse_trial.py 1
在命令行中,我得到了预期的结果1
但是如果我 运行
>argparse_trial.py 1
我明白了
usage: arg_parse_trial.py [-h] [--sum] N [N ...]
argparse_trial.py: error: the following arguments are required: N
我检查过,在第二种情况下,代码似乎接收到的唯一参数是文件名本身,无论给出了多少参数。
我在 Windows 机器上,python 在我的路径上。
为什么第二个案例在这个脚本中失败了?我怎样才能让它工作?
这是一个猜测,我现在无法测试,但我相信这就是正在发生的事情:
- 您键入 python 文件的名称。
- Windows 无法将文件 运行 作为程序,所以...
- Windows 尝试使用相关程序打开文件(类似于使用
start
)。
- 这样做时,它会忽略线上的其他内容,并且...
- …为 python 生成基本命令行
要使用的解释器,仅具有解释器本身和
要打开的目标文件。
想一想当您 "run" 文本文档时会发生什么。
如果命令行生成不正确,运行宁此命令应该修复它(根据需要替换路径):
ftype Python.File=C:\Path\to\python.exe "%1" %*
注意最后的 %*
。如果有误,参数将被删除。
我认为代码运行良好。您可以通过选择默认程序 python.exe
for python files 来执行该程序。
在您的情况下 python.exe "1" %*
正如@spectras 所建议的那样,因此它将能够接受命令行参数。
同时提供路径环境变量。
像pip,virtualenv and youtube-dl
这样的程序都是可执行文件,如果已经设置了环境变量,我们在任何地方都可以使用它。
如何在 windows 上选择默认程序:
http://windows.microsoft.com/en-in/windows/change-default-programs#1TC=windows-7
您可以使用 python 中包含的 sys 库并使用 argv() 函数执行各种命令参数。
发现此视频教程直观易懂!
这是对 spectras 和 Bhansa 的回答的补充。
对于 Windows 10 个用户,设置高级文件关联有点复杂。您必须编辑注册表文件。
运行 regedit
.
转到 HKEY_CURRENT_USER\SOFTWARE\Classes\py_auto_file\shell\open\command
并将 Data
的值从
更改为
"C:\Path\To\Python\python.exe" "1"
至
"C:\Path\To\Python\python.exe" "1" %*
这可能也适用于 Windows 的早期版本。
我正在学习在我的代码中包含命令行参数。我已经阅读了 argparse
的文档,并尝试从那里 运行 使用此脚本。
#argparse_trial.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 argparse_trial.py 1
在命令行中,我得到了预期的结果1
但是如果我 运行
>argparse_trial.py 1
我明白了
usage: arg_parse_trial.py [-h] [--sum] N [N ...]
argparse_trial.py: error: the following arguments are required: N
我检查过,在第二种情况下,代码似乎接收到的唯一参数是文件名本身,无论给出了多少参数。
我在 Windows 机器上,python 在我的路径上。
为什么第二个案例在这个脚本中失败了?我怎样才能让它工作?
这是一个猜测,我现在无法测试,但我相信这就是正在发生的事情:
- 您键入 python 文件的名称。
- Windows 无法将文件 运行 作为程序,所以...
- Windows 尝试使用相关程序打开文件(类似于使用
start
)。 - 这样做时,它会忽略线上的其他内容,并且...
- …为 python 生成基本命令行 要使用的解释器,仅具有解释器本身和 要打开的目标文件。
想一想当您 "run" 文本文档时会发生什么。
如果命令行生成不正确,运行宁此命令应该修复它(根据需要替换路径):
ftype Python.File=C:\Path\to\python.exe "%1" %*
注意最后的 %*
。如果有误,参数将被删除。
我认为代码运行良好。您可以通过选择默认程序 python.exe
for python files 来执行该程序。
在您的情况下 python.exe "1" %*
正如@spectras 所建议的那样,因此它将能够接受命令行参数。
同时提供路径环境变量。
像pip,virtualenv and youtube-dl
这样的程序都是可执行文件,如果已经设置了环境变量,我们在任何地方都可以使用它。
如何在 windows 上选择默认程序:
http://windows.microsoft.com/en-in/windows/change-default-programs#1TC=windows-7
您可以使用 python 中包含的 sys 库并使用 argv() 函数执行各种命令参数。
发现此视频教程直观易懂!
这是对 spectras 和 Bhansa 的回答的补充。
对于 Windows 10 个用户,设置高级文件关联有点复杂。您必须编辑注册表文件。
运行 regedit
.
转到 HKEY_CURRENT_USER\SOFTWARE\Classes\py_auto_file\shell\open\command
并将 Data
的值从
更改为
"C:\Path\To\Python\python.exe" "1"
至
"C:\Path\To\Python\python.exe" "1" %*
这可能也适用于 Windows 的早期版本。