Command Line Inputs from Windows

2015-01-02 Thread Ken Stewart

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

--
https://mail.python.org/mailman/listinfo/python-list


Re: Command Line Inputs from Windows

2015-01-03 Thread Ken Stewart

Chris, Dennis, James, and Mark:

SUCCESS!  Thanks for your suggestions.  It was the registry.  Kudos to 
Dennis.  The data strings for a lot of different "command" keys in the 
registry were missing the %* (percent star) characters.  Thanks Chris for 
the explanation on why the %* string is needed.


Here is a sample key:
S1-5-21-1560217580-722697556-320042093-1000-Classes
   py_auto_file
   shell
   open
   command

The corrected data for the key looks like this:

"C:\Python34\python.exe" %1 %*

I used the 'find' tool in regedit to search for python in the registry. 
There were many hits on the word python but only a handful had data fields 
similar to the one above.  Every instance was missing the %* string.  I 
modified them all.  My script didn't start working until after I'd modified 
the very last one.  Happily, my computer still boots after mucking around in 
the registry.


I haven't yet investigated the launcher suggested by Chris and Mark.  That 
may well be the proper solution.  At the moment it looks like the Python 
installer didn't create these registry entries properly in Windows 7.


Thanks again

Ken Stewart

--
https://mail.python.org/mailman/listinfo/python-list