Command Line Inputs from Windows

Ken Stewart gordon_ken_stewart at msn.com
Fri Jan 2 14:44:55 EST 2015


Court of King Arthur,

I’d appreciate any help you can provide.  I’m having problems passing 
command line parameters from Windows 7 into a Python script (using Python 
3.4.2).  It works correctly when I call the interpreter explicitly from the 
Windows command prompt, but it doesn’t work when I enter the script name 
without calling the Python interpreter.

This works:
python myScript.py arg1 arg2 arg3

This doesn’t work:
myScript.py arg1 arg2 arg3

The Windows PATH environment variable contains the path to Python, as well 
as the path to the Script directory.  The PATHEXT environment variable 
contains the Python extension (.py).

There are other anomalies too between the two methods of invoking the script 
depending on whether I include the extension (.py) along with the script 
name.  For now I’m only interested in passing the arguments without 
explicitly calling the Python interpreter.

************************************
Here is the script:

#! python

import sys

def getargs():
    sys.stdout.write("\nHello from Python %s\n\n" % (sys.version,))
    print ('    Number of arguments =', len(sys.argv))
    print ('    Argument List =', str(sys.argv))

if __name__ == '__main__':
    getargs()

************************************
Result_1 (working correctly):

C:\Python34\Scripts> python myScript.py arg1 arg2 arg3

Hello from Python 3.4.2 (v3.4.2:ab2c023a9432, Oct  6 2014, 22:15:05) [MSC 
v.1600 32 bit (Intel)]

    Number of arguments = 4
    Argument List = ['myScript.py', 'arg1', 'arg2', 'arg3']

************************************
Result_ 2 (Fail)

C:\Python34\Scripts> myScript.py arg1 arg2 arg3

Hello from Python 3.4.2 (v3.4.2:ab2c023a9432, Oct  6 2014, 22:15:05) [MSC 
v.1600 32 bit (Intel)]

    Number of arguments = 1
    Argument List = ['C:\\Python34\\Scripts\\myScript.py']

As a beginner I’m probably making a mistake somewhere but I can’t find it. 
I don’t think the shebang does anything in Windows but I’ve tried several 
variations without success.  I’ve tried writing the script using only 
commands, without the accouterments of a full program (without the def 
statement and without the if __name__ == ‘__main__’ …) to no avail.  I’m out 
of ideas.  Any suggestions?

Ken Stewart




More information about the Python-list mailing list