如何在 Windows 中执行 Python 脚本? [英] How to execute Python scripts in Windows?

查看:38
本文介绍了如何在 Windows 中执行 Python 脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的脚本 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.exepython.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 argument

It prints argument but if I execute script by:

blah.py argument

error 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.File

Next, 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" foo

If 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.exeshellopencommand

key to:

"C:Python26python26.exe" "%1" %*

Likely, previously, %* was missing. Similarly, set

 HKEY_CLASSES_ROOTpy_auto_fileshellopencommand

to the same value. See http://eli.thegreenplace.net/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 脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆