Skip to content Skip to sidebar Skip to footer

Python Script, Args Not Transferred To Script

I have got a Python Script called 'gcc_opt.pyw' and I included its directory to the Windows PATH environment variable. But not a single commandline argument is passed to the script

Solution 1:

The reason why you're not getting parameters is because you broke the .py association so you could double-click those files to open them in NotePad++, and subsequently broke the .pyw association to do what .py is supposed to do.

In short, you forgot to include the %* at the end of your Python.exe command line for your "customized" (mangled) .pyw association.

The ASSOC and FTYPE commands are used to show associations and file types, ie, what program gets run to handle a file with a particular extension. Here is what those commands produce on my system:

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

C:\test>assoc .pyw
.pyw=Python.NoConFile

C:\test>ftype python.file
python.file="C:\Python27\python.exe""%1" %*

C:\test>ftype python.noconfile
python.noconfile="C:\Python27\pythonw.exe""%1" %*

The normal .py association runs python.exe with a console window so that you can see the output of print statements.

The normal .pyw association runs pythonw.exe with no console window.

You can see at the end of each command line, there is a %*. This is what sends the parameters to a command. (Actually, %1 is the first parameter, and %* means "all remaining parameters".)

When you try to run a python file at the command line without typing its extension or the initial "python" command, a few things happen.

First the PATHEXT environment variable is used to find a matching extension. In your case it finds that your command name "gcc_opt" + .PYW results in a matching file.

Then the association for .PYW files is looked up, which finds the filetype Python.NoConFile, which in your case is set to "python.exe" (supposed to be pythonw.exe). (You can see these in the registry under HKEY_CLASSES_ROOT.)

The system then creates an actual command line from the command template found for that filetype, which in your case is probably

"[your-python-path]python.exe""%1"

This tells it to use just the first parameter, your python script name "gcc_opt.pyw".

The quick fix is to add the %* to the end of that command.

The CORRECT fix would be to put things back to the correct associations and open Python files for editing by a more standard method (drop icon onto NotePad++, or maybe right click and Edit with NotePad++).

Solution 2:

You should rename it to .py.

.pyw is intended to be used for GUI applications, because they don't need console window.

Post a Comment for "Python Script, Args Not Transferred To Script"