开发者

Python Script, args not transferred to Script [duplicate]

This question already has an answer here: Python Script does not take sys.argv in Windows (1 answer) Closed 8 months ago.

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. Printing out sys.argv tells me there 开发者_开发技巧is only the filename in the argv-list.

This command:

gcc_opt HelloWorld.c -o HelloWorld.exe -shared

results in

["C:\\Scripts\\gcc_opt.pyw"]

Can you tell me why there are no other arguments ?

I don't know if it is important, but I've set python.exe to be the default program to execute .pyw files as i don't see any prints using pythonw.exe (why ever this is).


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++).


You should rename it to .py.

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜