在Windows上直接调用Python脚本时,sys.argv中没有参数
在我的路径中有一些方便的python脚本,我希望通过直接调用它们来运行它们,例如在Windows上直接调用Python脚本时,sys.argv中没有参数,python,Python,在我的路径中有一些方便的python脚本,我希望通过直接调用它们来运行它们,例如myscript.py argument1。但是参数将不会显示在sys.argv中 c:\Users\MrLeeh\bin>test.py hello ['C:\\Users\\MrLeeh\\bin\\test.py'] test.py #!python3.5 import sys print(sys.argv) 因此,当我直接调用这个小脚本时,我无法通过sys.argv访问给定的参数 c:\Users
myscript.py argument1
。但是参数将不会显示在sys.argv
中
c:\Users\MrLeeh\bin>test.py hello
['C:\\Users\\MrLeeh\\bin\\test.py']
test.py
#!python3.5
import sys
print(sys.argv)
因此,当我直接调用这个小脚本时,我无法通过sys.argv
访问给定的参数
c:\Users\MrLeeh\bin>test.py hello
['C:\\Users\\MrLeeh\\bin\\test.py']
当我使用python
命令显式调用它时,我可以访问我的参数
c:\Users\MrLeeh\bin>python test.py hello
['C:\\Users\\MrLeeh\\bin\\test.py', 'hello']
我问自己这是什么原因,以及在使用直接调用时如何访问参数。我的python文件与
py.exe
相关联,可能您错过了一些注册表配置
HKEY\U CLASSES\U ROOT\Python.File\shell\open\command中保存了一个注册表:
[HKEY_CLASSES_ROOT\Applications\python.exe\shell\open\command]
@="\"C:\\Python27\\python.exe\" \"%1\" %*"
上面的注册表设置添加了“%*”以将所有参数传递给python.exe
- 通过