Command Line Inputs from Windows

Mark Lawrence breamoreboy at yahoo.co.uk
Sat Jan 3 02:57:48 EST 2015


On 02/01/2015 19:44, Ken Stewart wrote:
> Court of King Arthur,

Court of BDFL actually.

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

Works fine for me with this:-

c:\Users\Mark\Documents\MyPython>assoc .py
.py=Python.File

c:\Users\Mark\Documents\MyPython>assoc .pyw
.pyw=Python.NoConFile

c:\Users\Mark\Documents\MyPython>ftype Python.File
Python.File="C:\Windows\py.exe" "%1" %*

c:\Users\Mark\Documents\MyPython>ftype Python.NoConFile
Python.NoConFile="C:\Windows\pyw.exe" "%1" %*

c:\Users\Mark\Documents\MyPython>set pathext
PATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC;.PY

This is set up by the Python Launcher for Windows, introduced in 3.3, 
see https://docs.python.org/3/using/windows.html#launcher, noting that 
shebang lines also work.

-- 
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.

Mark Lawrence




More information about the Python-list mailing list