Run function from the command line

run python function from command line with arguments
python run function in file from command line
call python function with arguments from command line windows
run python script from command line
run python3 from command line
python command line arguments
run a python class from command line
shell script call python function with arguments

I have this code:

def hello():
    return 'Hi :)'

How would I run this directly from the command line?

With the -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()'

How can I run a function from a script in command line?, If the script only defines the functions and does nothing else, you can first execute the script within the context of the current shell using the  Easily run python functions from the command line. 17 November 2015 Tagged: python programming. Let’s say you have a python file and you want to run some functions from it in the command line. You have a folder called: project └── In that file you have a function called beep: def beep (): print "beep beep"

Just put hello() somewhere below the function and it will execute when you do python

For a neater solution you can use this:

if __name__ == '__main__':

That way the function will only be executed if you run the file, not when you import the file.

How to Run Your Python Scripts – Real Python, How do I run a Python method from the command line? Running R scripts from the command line can be a powerful way to: Automate your R scripts Integrate R into production Call R through other tools or systems There are basically two Linux commands that are used. The first is the command, Rscript, and is preferred. The older command is R CMD BATCH.

python -c 'from myfile import hello; hello()' where myfile must be replaced with the basename of your Python script. (E.g., becomes myfile).

However, if 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__":

This allows you to execute the script simply by running python 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 "__main__".

Run Node.js scripts from the command line, , or python3 if you have both versions, followed by the path to your script, just like this: $ python3 Hello World! Work with Azure Functions Core Tools. Azure Functions Core Tools lets you develop and test your functions on your local computer from the command prompt or terminal. Your local functions can connect to live Azure services, and you can debug your functions on your local computer using the full Functions runtime.

I wrote a quick little Python script that is callable from a bash command line. It takes the name of the module, class and method you want to call and the parameters you want to pass. I call it PyRun and left off the .py extension and made it executable with chmod +x PyRun so that I can just call it quickly as follow:

./PyRun PyTest.ClassName.Method1 Param1

Save this in a file called PyRun

#!/usr/bin/env python
#make executable in bash chmod +x PyRun

import sys
import inspect
import importlib
import os

if __name__ == "__main__":
    cmd_folder = os.path.realpath(os.path.abspath(os.path.split(inspect.getfile( inspect.currentframe() ))[0]))
    if cmd_folder not in sys.path:
        sys.path.insert(0, cmd_folder)

    # get the second argument from the command line      
    methodname = sys.argv[1]

    # split this into module, class and function name
    modulename, classname, funcname = methodname.split(".")

    # get pointers to the objects based on the string names
    themodule = importlib.import_module(modulename)
    theclass = getattr(themodule, classname)
    thefunc = getattr(theclass, funcname)

    # pass all the parameters from the third until the end of 
    # what the function needs & ignore the rest
    args = inspect.getargspec(thefunc)
    z = len(args[0]) + 2

Here is a sample module to show how it works. This is saved in a file called

class SomeClass:
 def First():
     print "First"

 def Second(x):
    # for x1 in x:
    #     print x1

 def Third(x, y):
     print x
     print y

class OtherClass:
    def Uno():

Try running these examples:

./PyRun PyTest.SomeClass.First
./PyRun PyTest.SomeClass.Second Hello
./PyRun PyTest.SomeClass.Third Hello World
./PyRun PyTest.OtherClass.Uno
./PyRun PyTest.SomeClass.Second "Hello"
./PyRun PyTest.SomeClass.Second \(Hello, World\)

Note the last example of escaping the parentheses to pass in a tuple as the only parameter to the Second method.

If you pass too few parameters for what the method needs you get an error. If you pass too many, it ignores the extras. The module must be in the current working folder, put PyRun can be anywhere in your path.

How to run Python functions from command line?, How do I run a node script from the command line? run function from command line. Learn more about function, command line, unix

add this snippet to the bottom of your script

def myfunction():

if __name__ == '__main__':

You can now call your function by running

python myfunction

This works because you are passing the command line argument (a string of the function's name) into locals, a dictionary with a current local symbol table. The parantheses at the end will make the function be called.

update: if you would like the function to accept a parameter from the command line, you can pass in sys.argv[2] like this:

def myfunction(mystring):
    print mystring

if __name__ == '__main__':

This way, running python myfunction "hello" will output hello.

Easily run python functions from the command line, , make sure you are in the same directory which contains the app.js file. The following example shows how to launch Access with a command-line argument and then shows how to return the value of this argument by using the Command function. To test this example, click the Windows Start button and click Run. Type the following code in the Run box on a single line. (You must surround the parts of the command line information in quotation marks).

run function from command line - MATLAB Answers, To run this function from the command line we can use the -c (command) argument as follows:$ python -c 'import foobar; print foobar. 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.

How to Write a Function and Run on Command Line in Python 2.7 , Easily run python functions from the command line. 17 November 2015 Tagged: python programming. Let's say you have a python file and you want to run some  Run Functions from the Command Line Pure functions can be defined and executed using data and arguments from the command-line environment, allowing for easy ad-hoc sequential operations. Simple Interaction with Input Streams

How to call bash functions, command line. Learn more about function, command line, unix. But for this the function I call needs to be in the folder where I am. Is there a 

  • Probably you meant print "Hi :)" instead of return 'Hi :)'.
  • I noted that on windows shell, you need a double quote instead of single. $python -c "import foo;foo.hello()"
  • What if the file is not in the local directory or on the PYTHONPATH?
  • On Ubuntu Linux you also have to use double quotes if you run the command from inside a Qt app for example.
  • The second one is a more general answer. I have a script defined multiple customer functions, and only call one depending on my need
  • For some reason, this didn't work for me, while replacing print foo.hello() with print(foo.hello()) did. I don't have the python knowledge to explain why this is, so if someone else could explain what can be going on, that would be greatly appreciated
  • And what if hello() takes arguments that should be supplied by the command line?
  • In that case you can send sys.argv to the method. Or access it from the hello method
  • One difference between this answer and the import foo solution is that import foo allows calling an arbitrary function in foo without modifying foo.
  • That's true, but I wouldn't recommend that solution beyond test purposes
  • @Wolph hey with this structure, how do I execute a seperate function (not included inside hello()) and run it from the command line?
  • What is the difference between python -m foo -c '' and python -c 'import foo;'? I get different behavior where it seems the -c argument is ignored in the first case.
  • It's nice, but it's not really an answer to the question.