从命令行执行 Python 脚本会隐藏打印语句 [英] Executing Python Script From Command Line is Hiding Print Statements
问题描述
我知道这一定是一个非常基本的问题,但是,我已经尝试在整个 SO 中找到一个简单的答案,但找不到.
I know this must be a super basic question, however, I have tried finding a simple answer throughout SO and cannot find one.
所以我的问题是:如何从命令行执行 python 脚本,以便我可以看到打印语句.
So my question is this: How can I execute a python script from the command line such that I can see print statements.
例如,假设我有文件 test.py:
For example, say I have the file test.py:
def hello():
print "hello"
如果我进入解释器,导入test.py,然后调用test.hello(),一切正常.但是,我希望能够只运行
If I enter the interpreter, import test.py, and then call test.hello(), everything works fine. However, I want to be able to just run
python test.py
从命令行并让它打印hello"到终端.
from the command line and have it print "hello" to the terminal.
我该怎么做?
谢谢!
更新:是的,抱歉,我的脚本实际上更像这样:
UPDATED: Yes, sorry, my script is actually more like this:
def main():
hello()
def hello():
print "hello"
还需要调用main(),还是自动调用?
Do I still need to call main(), or is it automatically invoked?
推荐答案
在文件末尾添加:
if __name__ == '__main__':
hello()
这篇关于从命令行执行 Python 脚本会隐藏打印语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!