通过python通过子进程运行TCL,但不给出任何输出 [英] running TCL through python by subprocess, but not giving any output

查看:224
本文介绍了通过python通过子进程运行TCL,但不给出任何输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过python子进程运行我的tcl脚本,如下所示:

I am trying to run my tcl script through python subprocess as follow:

import subprocess
>>> subprocess.Popen(["tclsh", "tcltest.tcl"])
<subprocess.Popen object at 0x0000000001DD4DD8>
>>> subprocess.Popen(["tclsh", "tcltest.tcl"], shell=True )
<subprocess.Popen object at 0x0000000002B34550>

我不知道它是否有效,因为我什么都看不到! 我的tcl脚本也有我公司的一些软件包,当我使用Tkinter,Tk和eval时会导致错误.

I don't know if it is working or not, since I don't see any anything from it! my tcl script also has some packages from my company that causes errors when I use Tkinter, Tk, and eval,

import Tkinter
import socket

def TCLRun():
 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
 s.connect(('127.0.0.1', 5006))
 root = Tkinter.Tk()
## root.tk.eval('package require Azimuth-Sdk')
 tcl_script ="""
##package require Company-Sdk
## set players [ace_azplayer_list_players]
set players 2
puts $players 
##  if { $players != "" } {         
##  foreach player $players {   
##      set cmd ace_azplayer_remove_player
##      if { [catch { [ $cmd $player ] } err] } {   
##          puts " $cmd $player - $err" 
##          sleep 1 
##          }           
##      } 
##  } """
 # call the Tkinter tcl interpreter
 root.tk.call('eval', tcl_script)
 root.mainloop()

给我这个错误

import TCLCall
>>> TCLCall.TCLRun()

Traceback (most recent call last):
  File "<pyshell#2>", line 1, in <module>
    TCLCall.TCLRun()
  File "C:\Users\XXX\Desktop\PKT\TCLCall.py", line 24, in TCLRun
    root.tk.call('eval', tcl_script)
TclError: can not find channel named "stdout"

这就是为什么我切换到子流程的原因.至少它不会给我错误!

that's why I switched to subprocess. at least it doesn't give me error!

有什么想法如何通过python使用内部必需的软件包运行我的tcl脚本吗?!

any idea how to run my tcl script with internal required package through python?!

谢谢

推荐答案

要使用subprocess.Popen获取输出,可以尝试以下操作:

To get the output from using subprocess.Popen, you can try the following:

import subprocess

p = subprocess.Popen(
    "tclsh tcltest.tcl",
    shell=True,
    stdin=subprocess.PIPE,
    stdout=subprocess.PIPE,
    stderr=subprocess.PIPE)
stdout, stderr = p.communicate()
print stdout
print stderr

使用subprocess.Popen运行的脚本很可能也会生成错误,但是由于您没有明确寻找它而没有显示.

It's entirely possible that the script you're running with subprocess.Popen is also generating an error, but isn't displaying since you aren't explicitly looking for it.

为防止某些信息在下面的评论中丢失:

To prevent some information from being lost in the comments below:

您可能在这里有一些潜在的错误,或者可以尝试做的事情.

You probably have several potential errors here, or things you can try.

您的tcl脚本本身无法正确导入teapot,或者tcl脚本与python脚本之间无法进行某种交互,或者subprocess.Popen无法正确找到teapot从您的路径打包.

Either your tcl script by itself isn't able to import teapot properly, or some sort of interaction between the tcl script and the python script isn't working properly, or subprocess.Popen isn't correctly finding the teapot package from your path.

我会尝试按此顺序调试程序.首先确认您的tcl脚本可以在不使用python或subprocess.Popen的情况下正常运行,而直接从命令行运行(例如,C:\Users\blah tclsh tcltest.tcl)

I would try debugging your programs in that order. First confirm that your tcl script works without using python or subprocess.Popen and just directly run it from the command line (for example, C:\Users\blah tclsh tcltest.tcl)

然后,在确保脚本可以正常工作后,引入Python.从我所看到的,您对python的东西没有任何问题,但是您的tcl脚本或路径有问题.

Then, after you've made sure your script work, bring in Python. From what I'm seeing, you don't have any problem with the python stuff, but with either your tcl script or some issue with your path.

这篇关于通过python通过子进程运行TCL,但不给出任何输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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