Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running a python method/function directly from a file

Tags:

python

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 'test.py' with a function 'newfunction()'.

----------test.py-----------

def newfunction():
     print 'welcome'

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

  python test.py newfunction

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

I want to be able to use it with django. But an answer that is applicable everywhere would be great.

like image 898
Mukund Gandlur Avatar asked Apr 18 '16 05:04

Mukund Gandlur


People also ask

How do you call a function in Python script?

To use functions in Python, you write the function name (or the variable that points to the function object) followed by parentheses (to call the function). If that function accepts arguments (as most functions do), then you'll pass the arguments inside the parentheses as you call the function.

Can Python functions be external files?

User-defined functions can be called from other files. A function can be called and run in a different file than the file where the function is defined.

How do I run a specific method from the command line in Python?

To run Python scripts with the python command, you need to open a command-line and type in the word python , or python3 if you have both versions, followed by the path to your script, just like this: $ python3 hello.py Hello World!


2 Answers

Try with globals() and arguments (sys.argv):

#coding:utf-8

import sys

def moo():
    print 'yewww! printing from "moo" function'

def foo():
    print 'yeeey! printing from "foo" function'

try:
    function = sys.argv[1]
    globals()[function]()
except IndexError:
    raise Exception("Please provide function name")
except KeyError:
    raise Exception("Function {} hasn't been found".format(function))

Results:

➜ python calling.py foo
yeeey! printing from "foo" function

➜ python calling.py moo
yewww! printing from "moo" function

➜ python calling.py something_else
Traceback (most recent call last):
  File "calling.py", line 18, in <module>
    raise Exception("Function {} hasn't been found".format(function))
Exception: Function something_else hasn't been found

➜ python calling.py
Traceback (most recent call last):
  File "calling.py", line 16, in <module>
    raise Exception("Please provide function name")
Exception: Please provide function name
like image 195
Andrés Pérez-Albela H. Avatar answered Sep 23 '22 06:09

Andrés Pérez-Albela H.


I think you should take a look at:

https://docs.djangoproject.com/en/1.9/howto/custom-management-commands/

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 manage.py. To do this, just add a management/commands directory to the application.

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

like image 35
Ozgur Vatansever Avatar answered Sep 23 '22 06:09

Ozgur Vatansever