Windows 无法将参数传递给 python 脚本
在 py_script.py 中:
import os
import sys
l = len(sys.argv)
if l == 1:
print 'no args'
else:
if l > 1:
print 'first arg is %s'%sys.argv[1]
if l > 2:
print 'second arg is %s'%sys.argv[2]
现在在我的 winXP 平台上使用命令行:
d:\path\py_script.py 1 2
如果
first arg is 1
second arg is 2
在我的 Win7 平台上我得到了
no args
我这样做
d:\path\python py_script.py 1 2
我得到
first arg is 1
second arg is 2
如何使我的 Win7 环境按预期运行?
一些细节:
win7是64位的。
win7 上为 py2.6.6,winXP 上为 py 2.6.4。
in py_script.py:
import os
import sys
l = len(sys.argv)
if l == 1:
print 'no args'
else:
if l > 1:
print 'first arg is %s'%sys.argv[1]
if l > 2:
print 'second arg is %s'%sys.argv[2]
now going command-line, on my winXP platform:
d:\path\py_script.py 1 2
yields
first arg is 1
second arg is 2
yet on my Win7 platform I get
no args
If I do
d:\path\python py_script.py 1 2
I get
first arg is 1
second arg is 2
How can I make my Win7 environment act as expected ?
some details:
win7 is 64bit.
py2.6.6 on win7, py 2.6.4 on winXP.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我在 Win 7 上使用 2.7.1。
如果您想仅通过文件扩展名调用 Python 程序,您应该检查文件类型关联和命令行参数。我在 Windows 上安装/重新安装多个版本的 Python 时遇到了问题。
此 TechNet 页面可以提供一些更详细的背景信息。
http://technet.microsoft.com/en-us/library/bb490912.aspx
I'm using 2.7.1 on Win 7.
If you want to invoke Python programs by file extension alone, you should check the file type associations and command line parameters. I have experienced issues when installing/reinstalling multiple versions of Python on Windows.
This TechNet page can provide some more detailed background.
http://technet.microsoft.com/en-us/library/bb490912.aspx
基于jtp的回答。
好吧,我把注册表弄乱了一点。
这就是我认为的步骤:
正在做
assoc .py=Python.File
通过 win 资源管理器选择一个 .py 文件,右键单击 -> x64->打开>浏览到 c:\Python26\python.exe 选择“始终使用此打开..”框。
这实际上立即改变了 reg 值
HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts.py\UserChoice
到
Python.File
"C:\Python26\python.exe" "%1" %2 %3 %4 %5 %6 %7 %8 %9
注意:根据以前的经验,我确信混合版本会出现混乱。
卸载/重新安装应该是要走的路。顺便说一句,我不想经历这个,因为所有的包,包括我从源代码构建的包,都会变得一团糟。
Based on jtp's answer.
Well I messed up with the registry a bit.
This was what I think are the steps:
doing
assoc .py=Python.File
through win explorer pick a .py file, right click -> x64 -> open with > browse to c:\Python26\python.exe choose the 'always open with this..' box.
this in effect changes immediately the reg value
HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts.py\UserChoice
to
Python.File
"C:\Python26\python.exe" "%1" %2 %3 %4 %5 %6 %7 %8 %9
note: from previous experience i'm sure things are expected to mess up with mixed versions.
uninstalling/re-installing shall be the way to go. BTW, I didn't want to go through that becuase with all the packages including ones I built from source it would be a mess.
我知道这并不能回答你的问题,但是 python py_script.py 是执行 Python 脚本的标准方法。
I know this doesn't answer your question, but
python py_script.py
is the standard way to execute Python scripts.