我有一个简单的脚本 blah.py(使用 Python 2):
导入系统打印 sys.argv[1]如果我通过以下方式执行我的脚本:
python c:/..../blah.py 参数它打印参数,但如果我通过以下方式执行脚本:
blah.py 参数出现错误:
索引错误...
所以参数不会传递给脚本.
PATH 中的 python.exe.带有 blah.py 的文件夹也在 PATH 中.python.exe 是执行 *.py 文件的默认程序.
有什么问题?
解决方案当你执行一个脚本而不在前面输入python"时,你需要知道 Windows 如何调用程序的两件事.首先是找出Windows认为它是什么类型的文件:
C:>关联.py.py=Python.File接下来,您需要了解 Windows 如何使用该扩展程序执行操作.它与文件类型Python.File"相关联,因此此命令显示了它将执行的操作:
C:>ftype Python.FilePython.File="c:python26python.exe" "%1" %*所以在我的机器上,当我输入blah.py foo"时,它会执行这个确切的命令,结果与我自己输入完整内容没有区别:
"c:python26python.exe" "blah.py" foo如果您输入相同的内容,包括引号,那么您将获得与您仅输入blah.py foo"时相同的结果.现在您可以自己解决剩下的问题了.
(或者在您的问题中发布更多有用的信息,例如您在控制台中看到的内容的实际剪切和粘贴副本.请注意,执行此类操作的人会获得他们的问题投票,并获得声誉点,并且更多的人可能会帮助他们给出好的答案.)
来自评论:即使 assoc 和 ftype 显示正确的信息,也可能会发生参数被剥离的情况.在这种情况下可能有帮助的是直接修复 Python 的相关注册表项.设置
HKEY_CLASSES_ROOTApplicationspython26.exeshellopencommand关键:
"C:Python26python26.exe" "%1" %*以前可能缺少 %*.同样,设置
HKEY_CLASSES_ROOTpy_auto_fileshellopencommand相同的值.请参阅 HKEY_CLASSES_ROOTApplicationspython.exeshellopencommand 注册表路径可能不同,使用 python26.exe 或 python.exe 或任何一个已在注册表中.
HKEY_CLASSES_ROOTpy_auto_fileshellopencommand
I have a simple script blah.py (using Python 2):
import sys print sys.argv[1]If I execute my script by:
python c:/..../blah.py argumentIt prints argument but if I execute script by:
blah.py argumenterror occurs:
IndexError...
So arguments do not pass to script.
python.exe in PATH. Folder with blah.py also in PATH. python.exe is default program to execute *.py files.
What is the problem?
解决方案When you execute a script without typing "python" in front, you need to know two things about how Windows invokes the program. First is to find out what kind of file Windows thinks it is:
C:>assoc .py .py=Python.FileNext, you need to know how Windows is executing things with that extension. It's associated with the file type "Python.File", so this command shows what it will be doing:
C:>ftype Python.File Python.File="c:python26python.exe" "%1" %*So on my machine, when I type "blah.py foo", it will execute this exact command, with no difference in results than if I had typed the full thing myself:
"c:python26python.exe" "blah.py" fooIf you type the same thing, including the quotation marks, then you'll get results identical to when you just type "blah.py foo". Now you're in a position to figure out the rest of your problem for yourself.
(Or post more helpful information in your question, like actual cut-and-paste copies of what you see in the console. Note that people who do that type of thing get their questions voted up, and they get reputation points, and more people are likely to help them with good answers.)
Brought In From Comments:Even if assoc and ftype display the correct information, it may happen that the arguments are stripped off. What may help in that case is directly fixing the relevant registry keys for Python. Set the
HKEY_CLASSES_ROOTApplicationspython26.exeshellopencommandkey to:
"C:Python26python26.exe" "%1" %*Likely, previously, %* was missing. Similarly, set
HKEY_CLASSES_ROOTpy_auto_fileshellopencommandto the same value. See eli.thegreenplace/2010/12/14/problem-passing-arguments-to-python-scripts-on-windows/
HKEY_CLASSES_ROOTApplicationspython.exeshellopencommand The registry path may vary, use python26.exe or python.exe or whichever is already in the registry.
HKEY_CLASSES_ROOTpy_auto_fileshellopencommand
更多推荐
如何在 Windows 中执行 Python 脚本?
发布评论