在python中显示来自模块的'print'命令

时间:2013-01-13 20:47:54

标签: python module

我有两个python脚本,我们称之为 file1.py file2.py ,我有不同的功能,让我们说 function11 function12 位于 file1 中。我需要在 file2.py 函数中使用这些函数: function21

现在的问题是我有一个简单的错误,但是我需要对变量进行“打印”以确切了解为什么会出现这些错误。但错误是从 file1 的一个功能发送的(我正在使用 file2 )。当我添加一些打印件时,结果不会出现在终端中。

有什么方法可以做到这一点吗?

1 个答案:

答案 0 :(得分:0)

我不确定你的脚本是什么样的,但你可能会尝试这样的事情(如果你不仅仅是在寻找调试器):

file1.py

def function11(arg1, arg2):
    try:
        # try something with arg1, arg2 <---- throws error
        . . .
        # return something
    except:
        return arg1, arg2

def function12(arg3):
    # try something else
    . . .
    return something

file2.py

from file1 import *

def function21():
    try:
        # the following function should normally throw an error, but you have to be sure 
        # the result arg1, arg2 from function11 (in file1) still throws
        # an error and does not look like the expected result
        f = function11(arg1, arg2) 
        . . .
        return something
    except:     # <---- if function imported from file1.py throws error, you want to print variables instead.
        print arg1, arg2