从 Windows 命令行运行 Python 脚本,未传递参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7860872/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Run Python scripts from Windows command line, argument not passed
提问by Wai Yip Tung
I have a bunch of scripts written in Python. I run them from a Windows command prompt like
我有一堆用 Python 编写的脚本。我从 Windows 命令提示符运行它们
c:> my_script.py arg1 arg2 arg3
This works in every computer and every Windows version since many years ago. Just now it this has broken on my Windows 7 system. The script is loaded and executed. But none of the arguments are passed into the script.
自多年前以来,这适用于每台计算机和每个 Windows 版本。刚才它在我的 Windows 7 系统上坏了。脚本被加载并执行。但是没有任何参数传递到脚本中。
To illustrate this, I have a script named py_echo.py:
为了说明这一点,我有一个名为 py_echo.py 的脚本:
from pprint import pprint as pp
import sys
if __name__ =='__main__':
pp(sys.argv)
Then I execute it with the argument a, b, c. None of them are passed.
然后我用参数 a, b, c 执行它。他们都没有通过。
c:\Python27\Lib\site-packages>py_echo.py a b c
['C:\0\usr\bin\py_echo.py']
If I run python.exe explicitly, the arguments are passed correctly
如果我显式运行 python.exe,则参数将正确传递
c:\Python27\Lib\site-packages>python.exe c:HKEY_CLASSES_ROOT\Applications\python.exe\shell\open\command
HKEY_CLASSES_ROOT\py_auto_file\shell\open\command
\usr\bin\py_echo.py a b c
['c:\0\usr\bin\py_echo.py', 'a', 'b', 'c']
It was working before. It only start to break down after I uninstalled a bunch old version Python interpreter and modules from my PC. Reinstalling Python does not help. I wonder what can I do to fix this??
它以前工作过。在我从我的 PC 上卸载了一堆旧版本的 Python 解释器和模块后,它才开始崩溃。重新安装 Python 没有帮助。我想知道我能做些什么来解决这个问题?
I have become very dependent on my scripts I built over the years. I feel very handicapped when they breaks :(
我已经变得非常依赖我多年来构建的脚本。当他们休息时,我感到非常残疾:(
采纳答案by David C
To move the answer to SO (rather than the link in Jon's answer):
将答案移至 SO(而不是Jon 的答案中的链接):
Modifying the following two registries so that the arguments are passed along to Python:
修改以下两个注册表,以便将参数传递给 Python:
##代码##Add %*
to the existing "C:\PythonXX\python.exe" "%1"
, so that the key now looks like: "C:\PythonXX\python.exe" "%1" %*
.
添加%*
到现有的"C:\PythonXX\python.exe" "%1"
,以便密钥现在看起来像:"C:\PythonXX\python.exe" "%1" %*
。
Source: http://eli.thegreenplace.net/2010/12/14/problem-passing-arguments-to-python-scripts-on-windows/
来源:http: //eli.thegreenplace.net/2010/12/14/problem-passing-arguments-to-python-scripts-on-windows/
回答by Jon H
I had the same problem with Windows 7 / Python, and eventualy found that I had to set up correct file associations AND update two registry entries through regedit.
我在 Windows 7 / Python 上遇到了同样的问题,最终发现我必须设置正确的文件关联并通过 regedit 更新两个注册表项。
It is all described in this excelent article:
这一切都在这篇优秀的文章中描述:
http://eli.thegreenplace.net/2010/12/14/problem-passing-arguments-to-python-scripts-on-windows/
http://eli.thegreenplace.net/2010/12/14/problem-passing-arguments-to-python-scripts-on-windows/