在 Windows 上从 PATH 运行时,Python 脚本丢失参数 [英] Python script losing arguments when run from PATH on Windows

查看:33
本文介绍了在 Windows 上从 PATH 运行时,Python 脚本丢失参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道我的标题不是描述性的,所以让我在这里解释一下.

I know my title is not descriptive so let me try to explain it here.

通常我会像这样执行我的 python 脚本:

Normally I execute my python script like this:

D:\github\Miscellaneous-Programs\Python>python check.py -h
hello
['check.py', '-h']

现在我所做的是在我的 windows path 环境变量中添加了文件夹 D:\github\Miscellaneous-Programs\Python.比我尝试像这样执行我的脚本:

Now what I did is added the folder D:\github\Miscellaneous-Programs\Python in my windows path environment variable. Than I tried to execute my script like this:

C:\Users\noob>check -h
hello
['D:\\github\\Miscellaneous-Programs\\Python\\check.py']

如您所见,它没有显示我提供给它的 -h 参数.

As you see it didn't showed the -h argument I supplied to it.

我的check.py

import sys
print "hello"
print sys.argv

如果我从上面提到的 python 脚本中删除 print sys.argv 它在我上面提到的两种情况下都可以正常工作,即它打印hello"就好了.

If I remove print sys.argv from the above mentioned python script it work fine in both cases I mentioned above i.e, it prints "hello" just fine.

所以,我的问题是如何在脚本添加到环境变量后执行接受一些命令行参数的 python 脚本.

So, my question is how does one execute a python script that accepts some command line arguments after the script is added to environment variable.

我的目的是从 windows 命令提示符的任何地方执行我的 python 脚本,这有点类似于 chmod +x check.py.

My purpose is to execute my python script from anywhere in the windows command prompt which is somewhat similar to chmod +x check.py.

我在 cygwin 中尝试了 chmod 选项,它在两种情况下都可以正常工作.

I tried the chmod option in cygwin it works fine for both cases.

Cygwin 输出

noob@noob-PC ~
$ chmod +x check.py

noob@noob-PC ~
$ ./check.py h
['./check.py', 'h']

推荐答案

Windows 没有将解释器作为 #! 给出的可执行脚本文件的概念,因此您打算执行的操作不能工作.Windows 所做的是调用 WinAPI 函数 ShellExecute,它执行以下操作:

Windows does not have a notion of executable script files with the interpreter given as a #!, so what you intend to do cannot work. What Windows does is to call the WinAPI function ShellExecute which does the following:

然而,它更常用于启动一个应用程序对特定文件进行操作.例如,可以打开 .txt 文件通过微软写字板.因此,.txt 文件的打开动词将对应如下命令:

However, it is more commonly used to launch an application that operates on a particular file. For instance, .txt files can be opened by Microsoft WordPad. The open verb for a .txt file would thus correspond to something like the following command:

"C:\Program Files\Windows NT\Accessories\Wordpad.exe" "%1"

参见 MSDN

如您所见,只有 first 参数提供给应用程序.在您的情况下,这可以转化为以下内容:

As you can see, only the first parameter is supplied to the application. In your case, this translates to something along the lines of:

"C:\Program Files\Python\Python.exe" "D:\github\Miscellaneous-Programs\Python\check.py"

为了避免这种情况,您可以做的是创建一个名为 check.bat 的小 .bat 文件:

What you can do to avoid this is to create a little .bat file named check.bat:

python check.py %*

(有关更多信息,请参阅此 SO 问题详细信息.如果找不到 check.py 或 python,您可能还需要提供绝对路径)

(See this SO question for more details. You might also have to supply an absolute path for check.py or python if they cannot be found)

这篇关于在 Windows 上从 PATH 运行时,Python 脚本丢失参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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