Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Windows is not passing command line arguments to Python programs executed from the shell

I'm having trouble getting command line arguments passed to Python programs if I try to execute them directly as executable commands from a Windows command shell. For example, if I have this program (test.py):

import sys print "Args: %r" % sys.argv[1:] 

And execute:

>test foo Args: [] 

as compared to:

>python test.py foo Args: ['foo'] 

My configuration has:

PATH=...;C:\python25;... PATHEXT=...;.PY;....  >assoc .py .py=Python.File  >ftype | grep Python Python.CompiledFile="C:\Python25\python.exe" "%1" %* Python.File="C:\Python25\python.exe" "%1" %* Python.NoConFile="C:\Python25\pythonw.exe" "%1" %* 
like image 270
mckoss Avatar asked Apr 14 '10 20:04

mckoss


People also ask

How do I run Python script using arguments in Windows command line?

You can use the command line arguments by using the sys. argv[] array. The first index of the array consists of the python script file name. And from the second position, you'll have the command line arguments passed while running the python script.

Why is my Python not working in command prompt?

The “Python is not recognized as an internal or external command” error is encountered in the command prompt of Windows. The error is caused when Python's executable file is not found in an environment variable as a result of the Python command in the Windows command prompt.

How do I run a Python shell in Windows?

To run the Python Shell, open the command prompt or power shell on Windows and terminal window on mac, write python and press enter. A Python Prompt comprising of three greater-than symbols >>> appears, as shown below. Now, you can enter a single statement and get the result.


1 Answers

I think I solved this. For some reason there is a SECOND place in the registry (besides that shown by the file associations stored in HKEY_CLASSES_ROOT\Python.File\shell\open\command):

[HKEY_CLASSES_ROOT\Applications\python.exe\shell\open\command] @="\"C:\\Python25\\python.exe\" \"%1\" %*" 

This seems to be the controlling setting on my system. The registry setting above adds the "%*" to pass all arguments to python.exe (it was missing in my registry for some reason).

like image 154
mckoss Avatar answered Oct 13 '22 08:10

mckoss