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:

└── yourcode.py

In that file you have a function called beep:

def beep():
    print "beep beep"

How do you call it from the command line without adding a main block? Maybe you run it with the interpreter:

python -c "import project.yourcode; project.yourcode.beep()"
beep beep

Other than that, without changing your code, adding argument parsing and so on, how do you run them? Specially if you want to pass arguments to these functions it quickly becomes dirty to use python -c for that. Now you can just do pip install runp and:

runp project/yourcode.py beep
beep beep

It also works with function arguments, optional arguments, you can check which functions a file contains, get information on what each of them do (with docstrings) and so on.

Check it out on github at the runp repo.