如何在Python中打印命令?

时间:2022-08-02 00:05:55

I'm not in the programming area but I recently got interested in Python. I was writing some functions but for debugging I need to see what commands are running. For instance:

我不在编程领域,但最近我对Python感兴趣。我正在编写一些函数但是为了调试我需要查看正在运行的命令。例如:

def foo():
    for i in xrange(0,5):
        a = 1 + i

Is it possible to make the interpreter output

是否可以进行解释器输出

>>> for i in xrange(0,5)
>>> a = 1 + 0
>>> a = 1 + 1
>>> a = 1 + 2
>>> a = 1 + 3
>>> a = 1 + 4

For

对于

>>> foo()

Or at least write to a file what is happening? I did some scripting in the past and I remember that this was possible in DOS, using @ECHO ON or something. I did some reading and I feel like it's related to stdin and stdout in Python so I tried

或者至少写一个文件发生了什么?我在过去做了一些脚本,我记得在DOS下可以使用@ECHO ON或其他东西。我做了一些阅读,我觉得它与Python中的stdin和stdout有关所以我试过了

import sys
def foo():
    for i in xrange(0,5):
        a = 1 + i
        sys.stdin.flush()
        sys.stdout.flush()

But I get nothing... I also tried

但我什么都没得到......我也试过了

import sys
# foo()
sys.stdin.read()
sys.stdout.read()

and https://*.com/a/3289051/2032568, but it just hangs. Sorry if this is not the right place for beginners. I couldn't find anything that answers my question.

和https://*.com/a/3289051/2032568,但它只是挂起。对不起,如果这不适合初学者。我找不到任何能回答我问题的东西。

5 个解决方案

#1


4  

To make the interpreter print out expression values during runtime you can use the print statement. Also take a note of Python's string formatting facilities.

要使解释器在运行时打印出表达式值,可以使用print语句。另请注意Python的字符串格式设置。

Example:

例:

for i in xrange(0,5):
    a = 1 + i
    # print the value of a:
    print "the current value of variable 'a':", a

There is no need to flush stdout explicitly, unless you want to enforce to print out lines without a terminating newline:

除非您想强制打印没有终止换行符的行,否则不需要显式刷新stdout:

import sys
import time
for i in xrange(0,5):
    a = 1 + i
    # print the value of a:
    # the trailing comma prevents 'print' from adding a newline
    print "\rthe current value of variable 'a':", a, 
    sys.stdout.flush()
    # short pause for purposes of demonstration
    time.sleep(1)
# finally print a newline
print

To print each statement before it's executed have a look at the trace module.

要在执行之前打印每个语句,请查看跟踪模块。

Example:

例:

y = 0
for xi in range(3):
    y += xi
print y

The output:

输出:

$ python -m trace -t tt.py
 --- modulename: tt, funcname: <module>
tt.py(2): y = 0
tt.py(3): for xi in range(3):
tt.py(4):     y += xi
tt.py(3): for xi in range(3):
tt.py(4):     y += xi
tt.py(3): for xi in range(3):
tt.py(4):     y += xi
tt.py(3): for xi in range(3):
tt.py(5): print y
3
 --- modulename: trace, funcname: _unsettrace
trace.py(80):         sys.settrace(None)

What you are looking for in the first place, might also be a debugger, e.g. pdb. You get an interactive session, where you can step through the code, and have a look at the data interactively.

您首先要查找的内容也可能是调试器,例如PDB。您将获得一个交互式会话,您可以在其中单步执行代码,并以交互方式查看数据。

Example:

例:

$ python -m pdb tt.py
> /home/moooeeeep/tt.py(2)<module>()
-> y = 0
(Pdb) n
> /home/moooeeeep/tt.py(3)<module>()
-> for xi in range(3):
(Pdb) n
> /home/moooeeeep/tt.py(4)<module>()
-> y += xi
(Pdb) n
> /home/moooeeeep/tt.py(3)<module>()
-> for xi in range(3):
(Pdb) n
> /home/moooeeeep/tt.py(4)<module>()
-> y += xi
(Pdb) print y, xi
0 1
(Pdb) 
...

Most Python IDEs (e.g., PyDev) have nicely integrated debugging functionality. So my suggestion: go and get a debugger.

大多数Python IDE(例如PyDev)具有很好的集成调试功能。所以我的建议是:去拿一个调试器。

#2


3  

Have a look at the trace-module

看看跟踪模块

python -m trace --count -C . somefile.py

output is placed in currebt directory:

输出放在currebt目录中:

$ cat somefile.trace
    1: def foo():
    6:     for i in xrange(5):
    5:         a = 1 + i

    1: foo()

-c, --count Produce a set of annotated listing files upon program completion that shows how many times each statement was executed

-c, - count在程序完成时生成一组带注释的列表文件,显示每个语句的执行次数

If using the -t option you get this:

如果使用-t选项,你会得到:

$ python -m trace --count -t tr.py 
 --- modulename: tr, funcname: <module>
tr.py(1): def foo():
tr.py(5): foo()
 --- modulename: tr, funcname: foo
tr.py(2):     for i in xrange(5):
tr.py(3):         a = 1 + i
tr.py(2):     for i in xrange(5):
tr.py(3):         a = 1 + i
tr.py(2):     for i in xrange(5):
tr.py(3):         a = 1 + i
tr.py(2):     for i in xrange(5):
tr.py(3):         a = 1 + i
tr.py(2):     for i in xrange(5):
tr.py(3):         a = 1 + i
tr.py(2):     for i in xrange(5):

#3


1  

You mean something like this?

你的意思是这样的?

def foo():
         for i in xrange(0,5):
                 a = 1 + i
                 print "a = 1 + {0}".format(i)

>>> foo()
a = 1 + 0
a = 1 + 1
a = 1 + 2
a = 1 + 3
a = 1 + 4

#4


0  

def foo():
    for i in xrange(0,5):
        a = 1 + i
        print a

i think this its what you are looking for, i hope this be usefull for you :)

我认为这是你正在寻找的,我希望这对你有用:)

edit:

编辑:

i think i understood what you want:

我想我理解你想要的东西:

def foo():
    for i in xrange(0,5):
        print "a = 1 + i"

#5


0  

I understand what you what to achieve here and I checked that link: it hangs for me too, and only repeats inputted text in the Terminal. Unfortunately I don't know how to print out the script as you've asked: for debugging, I'd advise just using simple print commands to work out which section is being executed.

我明白你在这里要做什么,我检查了那个链接:它也挂了我,只在终端重复输入的文字。不幸的是我不知道如何打印脚本,因为你已经问过:为了调试,我建议只使用简单的打印命令来确定正在执行哪个部分。

def foo():
    for i in xrange(0,5):
        a = 1 + i, print "line is being executed where i = %i " % i

Then just read the printed text output to see what the program is doing. Hope this helps.

然后只需阅读打印的文本输出即可查看程序正在执行的操作。希望这可以帮助。

#1


4  

To make the interpreter print out expression values during runtime you can use the print statement. Also take a note of Python's string formatting facilities.

要使解释器在运行时打印出表达式值,可以使用print语句。另请注意Python的字符串格式设置。

Example:

例:

for i in xrange(0,5):
    a = 1 + i
    # print the value of a:
    print "the current value of variable 'a':", a

There is no need to flush stdout explicitly, unless you want to enforce to print out lines without a terminating newline:

除非您想强制打印没有终止换行符的行,否则不需要显式刷新stdout:

import sys
import time
for i in xrange(0,5):
    a = 1 + i
    # print the value of a:
    # the trailing comma prevents 'print' from adding a newline
    print "\rthe current value of variable 'a':", a, 
    sys.stdout.flush()
    # short pause for purposes of demonstration
    time.sleep(1)
# finally print a newline
print

To print each statement before it's executed have a look at the trace module.

要在执行之前打印每个语句,请查看跟踪模块。

Example:

例:

y = 0
for xi in range(3):
    y += xi
print y

The output:

输出:

$ python -m trace -t tt.py
 --- modulename: tt, funcname: <module>
tt.py(2): y = 0
tt.py(3): for xi in range(3):
tt.py(4):     y += xi
tt.py(3): for xi in range(3):
tt.py(4):     y += xi
tt.py(3): for xi in range(3):
tt.py(4):     y += xi
tt.py(3): for xi in range(3):
tt.py(5): print y
3
 --- modulename: trace, funcname: _unsettrace
trace.py(80):         sys.settrace(None)

What you are looking for in the first place, might also be a debugger, e.g. pdb. You get an interactive session, where you can step through the code, and have a look at the data interactively.

您首先要查找的内容也可能是调试器,例如PDB。您将获得一个交互式会话,您可以在其中单步执行代码,并以交互方式查看数据。

Example:

例:

$ python -m pdb tt.py
> /home/moooeeeep/tt.py(2)<module>()
-> y = 0
(Pdb) n
> /home/moooeeeep/tt.py(3)<module>()
-> for xi in range(3):
(Pdb) n
> /home/moooeeeep/tt.py(4)<module>()
-> y += xi
(Pdb) n
> /home/moooeeeep/tt.py(3)<module>()
-> for xi in range(3):
(Pdb) n
> /home/moooeeeep/tt.py(4)<module>()
-> y += xi
(Pdb) print y, xi
0 1
(Pdb) 
...

Most Python IDEs (e.g., PyDev) have nicely integrated debugging functionality. So my suggestion: go and get a debugger.

大多数Python IDE(例如PyDev)具有很好的集成调试功能。所以我的建议是:去拿一个调试器。

#2


3  

Have a look at the trace-module

看看跟踪模块

python -m trace --count -C . somefile.py

output is placed in currebt directory:

输出放在currebt目录中:

$ cat somefile.trace
    1: def foo():
    6:     for i in xrange(5):
    5:         a = 1 + i

    1: foo()

-c, --count Produce a set of annotated listing files upon program completion that shows how many times each statement was executed

-c, - count在程序完成时生成一组带注释的列表文件,显示每个语句的执行次数

If using the -t option you get this:

如果使用-t选项,你会得到:

$ python -m trace --count -t tr.py 
 --- modulename: tr, funcname: <module>
tr.py(1): def foo():
tr.py(5): foo()
 --- modulename: tr, funcname: foo
tr.py(2):     for i in xrange(5):
tr.py(3):         a = 1 + i
tr.py(2):     for i in xrange(5):
tr.py(3):         a = 1 + i
tr.py(2):     for i in xrange(5):
tr.py(3):         a = 1 + i
tr.py(2):     for i in xrange(5):
tr.py(3):         a = 1 + i
tr.py(2):     for i in xrange(5):
tr.py(3):         a = 1 + i
tr.py(2):     for i in xrange(5):

#3


1  

You mean something like this?

你的意思是这样的?

def foo():
         for i in xrange(0,5):
                 a = 1 + i
                 print "a = 1 + {0}".format(i)

>>> foo()
a = 1 + 0
a = 1 + 1
a = 1 + 2
a = 1 + 3
a = 1 + 4

#4


0  

def foo():
    for i in xrange(0,5):
        a = 1 + i
        print a

i think this its what you are looking for, i hope this be usefull for you :)

我认为这是你正在寻找的,我希望这对你有用:)

edit:

编辑:

i think i understood what you want:

我想我理解你想要的东西:

def foo():
    for i in xrange(0,5):
        print "a = 1 + i"

#5


0  

I understand what you what to achieve here and I checked that link: it hangs for me too, and only repeats inputted text in the Terminal. Unfortunately I don't know how to print out the script as you've asked: for debugging, I'd advise just using simple print commands to work out which section is being executed.

我明白你在这里要做什么,我检查了那个链接:它也挂了我,只在终端重复输入的文字。不幸的是我不知道如何打印脚本,因为你已经问过:为了调试,我建议只使用简单的打印命令来确定正在执行哪个部分。

def foo():
    for i in xrange(0,5):
        a = 1 + i, print "line is being executed where i = %i " % i

Then just read the printed text output to see what the program is doing. Hope this helps.

然后只需阅读打印的文本输出即可查看程序正在执行的操作。希望这可以帮助。