Windows fails to pass the args to a python script

2019-05-20 21:55发布

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.

3条回答
兄弟一词,经得起流年.
2楼-- · 2019-05-20 22:28

I know this doesn't answer your question, but python py_script.py is the standard way to execute Python scripts.

查看更多
孤傲高冷的网名
3楼-- · 2019-05-20 22:40

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.

C:\dv>assoc .py
.py=Python.File

C:\dv>ftype Python.File
Python.File="C:\Python27\python.exe" "%1" %*

This TechNet page can provide some more detailed background.

http://technet.microsoft.com/en-us/library/bb490912.aspx

查看更多
女痞
4楼-- · 2019-05-20 22:40

Based on jtp's answer.
Well I messed up with the registry a bit.

This was what I think are the steps:

  1. doing

    assoc .py=Python.File

  2. 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

  3. set HKEY_CLASSES_ROOT\Python.File\shell\Open\Command to "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.

查看更多
登录 后发表回答