Mukund Gandlur Mukund Gandlur - 1 year ago 24
Python Question

Running a python method/function directly from a file

I would like to know if there is a way to directly run a python function directly from a file by just mentioning the filename followed by the function in a single line.

For example, lets say I have a file '' with a function 'newfunction()'.

def newfunction():
print 'welcome'

Can I run the newfunction() doing something similar to this.

python newfunction

I know how to import and to call functions etc.Having seen similar commands in django etc (
python runserver
), I felt there is a way to directly call a function like this. Let me know if something similar is possible.


I think you should take a look at:

All those commands like migrate, runserver or dbshell etc. are implemented like how it was described in that link:

Applications can register their own actions with To do this, just add a management/commands directory to the application.

Django will register a command for each Python module in that directory whose name doesn’t begin with an underscore.