Python脚本,args未转移到脚本 [英] Python Script, args not transferred to Script

查看:168
本文介绍了Python脚本,args未转移到脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为gcc_opt.pyw的Python脚本,我将其目录包含在Windows PATH环境变量中。



但是没有传递一个命令行参数到脚本。打印出sys.argv告诉我在argv-list中只有文件名。



这个命令:

  gcc_opt HelloWorld.c -o HelloWorld.exe -shared 

导致

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



你能告诉我为什么没有其他参数?



不知道它是否重要,但我已经将python.exe设置为默认程序来执行.pyw文件,因为我没有看到任何打印使用pythonw.exe(为什么这是)。

解决方案

你没有获取参数的原因是因为你打破了.py
关联,所以你可以双击这些文件在NotePad ++,
中打开它们,然后打破.pyw协会做什么.py应该做的。



总之,你忘了包括%* 在您的Python.exe命令的结尾
行为您的定制的(滚动).pyw关联。



ASSOC和FTYPE命令用于显示关联和文件类型,即
运行某个程序来处理具有特定扩展名的文件。这是
这些命令在我的系统上产生的:

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

正常的.py关联使用控制台窗口运行python.exe,以便
可以看到print语句的输出。



正常的.pyw关联运行pythonw.exe没有控制台窗口。



你可以看到在每个命令行的结尾,有一个%* 。这是向命令发送
参数的原因。 (实际上,%1 是第一个参数,%*
表示所有剩余参数。 )



当你试图在命令行运行一个python文件而不输入
扩展名或初始的python命令时,会发生一些事情。 / p>

首先,PATHEXT环境变量用于查找匹配的扩展。
在您的情况下,它发现您的命令名称gcc_opt+ .PYW导致一个
匹配文件。



然后关联.PYW文件被查找,它找到文件类型
Python.NoConFile,在你的情况下设置为python.exe(应该是
pythonw.exe)。 (您可以在HKEY_CLASSES_ROOT下的注册表中看到这些)。



然后,系统从命令模板中找到
为该文件类型创建一个实际命令行,您的案例可能是

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

这告诉它只使用第一个参数,你的python脚本名称
gcc_opt.pyw。



快速修复是将%* 添加到该命令的末尾。



CORRECT修复将把事情恢复到正确的关联和
打开Python文件,通过更标准的方法编辑(放置图标到
NotePad ++,或者也许右单击并使用NotePad ++编辑)。


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

这篇关于Python脚本,args未转移到脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆