Executing Python Script From Command Line is Hiding Print Statements

I know this must be a super basic question, however, I have tried finding a simple answer throughout SO and cannot find one.

So my question is this: How can I execute a python script from the command line such that I can see print statements.

For example, say I have the file test.py:

def hello(): print "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

from the command line and have it print "hello" to the terminal.

How do I do this?


UPDATED: Yes, sorry, my script is actually more like this:

def main(): hello() def hello(): print "hello"

Do I still need to call main(), or is it automatically invoked?

-------------Problems Reply------------

Add at the end of the file:

if __name__ == '__main__':

You have to have the script actually call your method. Generally, you can do this with a if __name__ == "__main__": block.

Alternatively, you could use the -c argument to the interpreter to import and run your module explicitly from the cli, but that would require that the script be on your python path, and also would be bad style as you'd now have executing Python code outside the Python module.

Your print statement is enclosed in a function definition block. You would need to call the function in order for it to execute:

def hello():
print "hello"

if __name__ == '__main__':

Basically this is saying "if this file is the main file (has been called from the command line), then run this code."

As I understand it, your file just has the following lines:

def hello():
print "hello"

The definition is correct, but when do you "call" the function?

Your file should include a call to the hello() function:

def hello():
print "hello"


This way, the function is defined and called in a single file.

This is a very "script-like" approach... it works, but there must be a better way to do it

Category:python Views:0 Time:2013-03-21

Related post

  • How to execute rrdcgi script from command line? 2010-05-28

    I have some rrdcgi scripts with HTML code which are used to show graphs on my webpage. When I try to run this script from webpage its fine and showing all details perfectly but when i execute this script from command line it shows below error and nev

  • Python syntax error when trying to execute a script from command line 2012-03-17

    I've never used python before, but I have to in order to enable push notifications on my android app. When I type python D:\Project\UAirship\clientauth.py I get a syntax error with an error pointing to the drive letter. I've read support articles on

  • How to pass a string as sdtin to python script from command line interface 2011-04-27

    Currently iam using following command to do this python scriptName.py This command uses "<" to stdin the file to script. and it works fine, I can use sys.stdin.read to get the file data. But, what if i want to pass file data as a string, I dont wa

  • How to call a python script with command line args? 2010-10-28

    I have a script, client.py, that reads command line args straight as a script (not wrapped in a function like main()), like so: opts, args = getopt.getopt(sys.argv[1:], "l:r:a:j:b:f:n:u:") and prints a bunch of stuff. I need to call this script from

  • When running a python script from command line, PythonWin also runs. How can I avoid this? 2011-06-08

    I'm trying to run a script from the command line (command prompt). I type: python.exe full_script_path.py parameters And for some reason, PythonWin opens up. I have to close it for execution to finish. Unfortunately, this keeps me from being able to

  • How to execute .csh script with command line arguments from .csh script 2009-09-17

    So I have .csh script generate.csh I want to call another .csh script from within it. I tried ./shell_gen.csh test1 test2.prt But it runs shell_gen.csh but without command line arguments. Anyone? Thanks generate.csh #!/bin/csh ./shell_gen.csh test1 t

  • Problem with Oracle script through command line - How to avoid Prompting for Time etc? 2010-07-21

    I have taken a sql dump as script from Jdeveloper and trying to create database at other place by running the oracle sql script through command line. When executing, at some place in the script, it is prompting for values like "Enter value for time:"

  • Executing AutoIt script from command prompt 2010-10-30

    Is there any way to execute my AutoIt script from command line? Unfortunately, the forum seems to be down and I was not able to gather much information. --------------Solutions------------- You could compile your AutoIt script and run the .exe from t

  • How to test python code on command line 2010-12-26

    While writing and testing a python method, I am currently using the following approach: import foo as f bar = f.bar() bar.runMyMethodAndSeeIfItWorks() If I change something in my method, and I need to retest it, I have to execute the following: f = r

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.144 (s). 11 q(s)