【发布时间】:2013-11-07 13:57:20
【问题描述】:
我有一个执行 python 脚本的 java 应用程序,但在该行失败
cmd = "cmd /c asm.bat < 1.ADD.asm 2> nul"
os.system(cmd)
这在我从 Windows shell 启动 java 应用程序时有效,但如果我从 Eclipse 调试器中运行该应用程序会失败
RuntimeWarning: Unable to determine _shell_command for underlying os: nt.
我也尝试了cmd = "asm.bat < 1.ADD.asm 2> nul",但得到了同样的错误。
这是完整的堆栈跟踪:
File "MyScript.py", line 73, in runTestFile
os.system("cmd.exe")
File "C:\mycad\share\python\Lib\subprocess.py", line 456, in call
return Popen(*popenargs, **kwargs).wait()
File "C:\mycad\share\python\Lib\subprocess.py", line 753, in __init__
self._execute_child(args, executable, preexec_fn, close_fds,
File "C:\mycad\share\python\Lib\subprocess.py", line 1238, in _execute_child
args = _shell_command + args
TypeError: unsupported operand type(s) for +: 'NoneType' and 'lis
subprocess.py 中的第 456 行在这里
def call(*popenargs, **kwargs):
"""Run command with arguments. Wait for command to complete, then
return the returncode attribute.
The arguments are the same as for the Popen constructor. Example:
retcode = call(["ls", "-l"])
"""
return Popen(*popenargs, **kwargs).wait() # line 456
奇怪的是,我在 os.py 中找不到 os.system() 而我的 os.system("cmd.exe") 在 subprocess.py 中调用 call 函数。在 _execute_child 的第 1238 行,_shell_command 为 None,导致错误。在到达那里之前,_setup_platform 被执行并在两种情况下都返回_get_shell_commands = (['cmd.exe', '/c'], ['command.com', '/c'])。第一个可执行文件 cmd.exe 被distutils.spawn.find_executable(executable) 转换为C:\windows\system32\cmd.exe,而当我从调试器启动我的应用程序时,cmd.exe 和command.com 被转换为None。 distutils.spawn.find_executable(executable) 基本上迭代 path 环境变量中的所有条目,如果存在这样的文件,则将可执行文件 cmd.exe 附加到末尾返回结果。但是当应用程序是 Eclipse 启动时,路径 env 是空的,因为我已经覆盖了它。
【问题讨论】: