# python – Run function from the command line

## The Question :

402 people think this question is useful

I have this code:

def hello():
return 'Hi :)'



How would I run this directly from the command line?

• Probably you meant print "Hi :)" instead of return 'Hi :)'.

618 people think this answer is useful

With the -c (command) argument (assuming your file is named foo.py):

$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:

hi there!

$python test.py goodbye see ya later.  Note: There’s a bug in Python 3 at the moment, but works great with Python 2. Edit: An even better option, in my opinion is the module fire by Google which makes it easy to also pass function arguments. It is installed with pip install fire. From their GitHub: Here’s a simple example. import fire class Calculator(object): """A simple calculator class.""" def double(self, number): return 2 * number if __name__ == '__main__': fire.Fire(Calculator)  Then, from the command line, you can run: python calculator.py double 10 # 20 python calculator.py double --number=15 # 30  ## The Answer 7 6 people think this answer is useful Interestingly enough, if the goal was to print to the command line console or perform some other minute python operation, you can pipe input into the python interpreter like so: echo print("hi:)") | python  as well as pipe files.. python < foo.py  *Note that the extension does not have to be .py for the second to work. **Also note that for bash you may need to escape the characters echo print$$\"hi:$$\"\) | python  ## The Answer 8 5 people think this answer is useful If you install the runp package with pip install runp its a matter of running: runp myfile.py hello You can find the repository at: https://github.com/vascop/runp ## The Answer 9 4 people think this answer is useful I had a requirement of using various python utilities (range, string, etc.) on the command line and had written the tool pyfunc specifically for that. You can use it to enrich you command line usage experience: $ pyfunc -m range -a 1 7 2
1
3
5

$pyfunc -m string.upper -a test TEST$ pyfunc -m string.replace -a 'analyze what' 'what' 'this'
analyze this



1 people think this answer is useful

It is always an option to enter python on the command line with the command python

then import your file so import example_file

then run the command with example_file.hello()

This avoids the weird .pyc copy function that crops up every time you run python -c etc.

Maybe not as convenient as a single-command, but a good quick fix to text a file from the command line, and allows you to use python to call and execute your file.

1 people think this answer is useful

Something like this: call_from_terminal.py

# call_from_terminal.py
# Ex to run from terminal
# ip='"hi"'
# python -c "import call_from_terminal as cft; cft.test_term_fun(${ip})" # or # fun_name='call_from_terminal' # python -c "import${fun_name} as cft; cft.test_term_fun(${ip})" def test_term_fun(ip): print ip  This works in bash. $ ip='"hi"' ; fun_name='call_from_terminal'
$python -c "import${fun_name} as cft; cft.test_term_fun(${ip})" hi  ## The Answer 12 1 people think this answer is useful Below is the Odd_Even_function.py file that has the definition of the function. def OE(n): for a in range(n): if a % 2 == 0: print(a) else: print(a, "ODD")  Now to call the same from Command prompt below are the options worked for me. Options 1 Full path of the exe\python.exe -c “import Odd_Even_function; Odd_Even_function.OE(100)” Option 2 Full path of the exe\python.exe -c “from Odd_Even_function import OE; OE(100)” Thanks. ## The Answer 13 0 people think this answer is useful This function cannot be run from the command line as it returns a value which will go unhanded. You can remove the return and use print instead ## The Answer 14 0 people think this answer is useful We can write something like this. I have used with python-3.7.x import sys def print_fn(): print("Hi") def sum_fn(a, b): print(a + b) if __name__ == "__main__": args = sys.argv # args[0] = current file # args[1] = function name # args[2:] = function args : (*unpacked) globals()[args[1]](*args[2:])  python demo.py print_fn python demo.py sum_fn 5 8  ## The Answer 15 -1 people think this answer is useful Use the python-c tool (pip install python-c) and then simply write: $ python-c foo 'hello()'



or in case you have no function name clashes in your python files:

\$ python-c 'hello()'



Make your life easier, install Spyder. Open your file then run it (click the green arrow). Afterwards your hello() method is defined and known to the IPython Console, so you can call it from the console.