Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

How to run Python functions from command line?

I have a script foobar.py with this code

# file foobar.py
def sayHello():
    return 'Hello'

How do I run the python function in the script from the command line?


1 Answer
Rajendra Dharmkar

To run this function from the command line we can use the -c (command) argument as follows:

$ python -c 'import foobar; print foobar.sayHello()'

Alternatively, we can also write:

$ python -c 'from foobar import *; print sayHello()'

Or like this

$ python -c 'from foobar import sayHello; print sayHello()'

OUTPUT

Hello
Advertisements

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.