Run function from the command line
-c (command) argument (assuming your file is named
$ python -c 'import foo; print foo.hello()'
Alternatively, if you don't care about namespace pollution:
$ python -c 'from foo import *; print hello()'
And the middle ground:
$ python -c 'from foo import hello; print hello()'
python -c 'from myfile import hello; hello()' where
myfile must be replaced with the basename of your Python script. (E.g.,
hello() is your "permanent" main entry point in your Python script, then the usual way to do this is as follows:
def hello(): print "Hi :)"if __name__ == "__main__": hello()
This allows you to execute the script simply by running
python myfile.py or
python -m myfile.
Some explanation here:
__name__ is a special Python variable that holds the name of the module currently being executed, except when the module is started from the command line, in which case it becomes