python子进程的输出不可见_通过python通过子进程运行TCL,但不给出任何输出

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

import subprocess

>>> subprocess.Popen(["tclsh", "tcltest.tcl"])

>>> subprocess.Popen(["tclsh", "tcltest.tcl"], shell=True )

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()

gives me this error

import TCLCall

>>> TCLCall.TCLRun()

Traceback (most recent call last):

File "", line 1, in

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!

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

Thanks

解决方案

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

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.

Edit:

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

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

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.

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)

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值