【问题标题】:Using argparse in Python - and default file association在 Python 中使用 argparse - 和默认文件关联
【发布时间】:2012-07-26 13:34:18
【问题描述】:

我有一个名为“test.py”的文件,它使用简单的代码导入 argparse:

import argparse
parser = argparse.ArgumentParser(description='Description')
parser.add_argument('-e','--event', help='event', required=True)
args = vars(parser.parse_args())
myArgument = args['event']

如果我用

调用它,则从 Windows
python "test.py" -e hello

它工作正常,但如果我尝试直接调用它

test.py -e hello

它正在调用 python,但我收到一个错误“-e/--event is required” - 即它没有传递参数。

我在 windows 中的关联设置为:

assoc .py
.py=Python.File

ftype Python.File
Python.File = "C:\Python27,python.exe" "%1" %*

似乎无法解决这个问题,因为我在另一台计算机上运行它,所以我猜我的某些路径或环境变量设置不正确?

提前致谢

【问题讨论】:

标签: python windows argparse


【解决方案1】:

我认为您的文件关联存在错误。 我认为:

Python.File = "C:\Python27,python.exe" "%1" %*

应该是:

Python.File = "C:\Python27\python.exe" "%1" %*

(将, 更改为\

...如果这只是一个错字,那么this 问题和答案可能会很有趣。

基本上,这些关联不一定是您运行程序时所调用的。 (即,我基本上和你有相同的关联,但是如果我运行一个 python 程序,比如:“test.py -e hello”,程序 test.py 只是在我的编辑器中打开 - 它实际上并没有运行 python程序。)

您可能想同时查看:

  • HKEY_CURRENT_USER\Software\Classes\.py,和
  • HKEY_LOCAL_MACHINE\SOFTWARE\Classes\.py

那里的关联。

【讨论】:

    【解决方案2】:

    ftype 显示 HKEY_LOCAL_MACHINE\Software\Classes 中的内容,但此注册表分支中的值可能已被来自 HKEY_CURRENT_USER\Software\Classes 的值覆盖。看看reg query HKCU\Software\Classes\Python.File\shell\open\command /ve 的输出是什么。我猜您的问题是由于此注册表项中缺少%* 片段引起的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-20
      • 2021-12-03
      • 2022-08-05
      • 1970-01-01
      • 2019-11-20
      相关资源
      最近更新 更多