26

I am trying to figure out how to call a Python function from a shell script.

I have a Python file with multiple functions and I need to use the values returned by them in my shell script. Is there a way to do it.

I am doing this in order to read a config file using Python and getting the values in shell. Is there any other better way to achieve this.

test.py contains:

import ConfigParser

config = ConfigParser.ConfigParser()
config.read("test.conf")

def get_foo():
    return config.get("locations", "foo")

def get_bar():
    return config.get("locations", "bar")

I need to store the values returned by the Python functions in a shell variable.

Eric O. Lebigot
  • 91,433
  • 48
  • 218
  • 260
  • 3
    What is the larger problem you are trying to solve? Why not use the script with the config do the work of the the shell script? Alternatively, why not read the config from the shell? I assume the problem is complex enough to break this up into two files. – Pablo Maurin Dec 19 '11 at 21:35
  • what if the test.py path is let say '/root/src/test.py'? – p zankat Jan 16 '22 at 03:18

1 Answers1

43

You can send the result of your functions to the standard output by asking the Python interpreter to print the result:

python -c 'import test; print test.get_foo()'

The -c option simply asks Python to execute some Python commands.

In order to store the result in a variable, you can therefore do:

RESULT_FOO=`python -c 'import test; print test.get_foo()'`

or, equivalently

RESULT=$(python -c 'import test; print test.get_foo()')

since backticks and $(…) evaluate a command and replace it by its output.

PS: Getting the result of each function requires parsing the configuration file each time, with this approach. This can be optimized by returning all the results in one go, with something like:

ALL_RESULTS=$(python -c 'import test; print test.get_foo(), test.get_bar()')

The results can then be split and put in different variables with

RESULT_BAR=$(echo $ALL_RESULTS | cut -d' ' -f2)

which takes the second result and puts it in RESULT_BAR for example (and similarly: -fn for result #n).

PPS: As Pablo Maurin mentioned, it would probably be easier to do everything in a single interpreter (Python, but maybe also the shell), if possible, instead of calculating variables in one program and using them in another one.

Eric O. Lebigot
  • 91,433
  • 48
  • 218
  • 260
  • or try sending output to other python script and run that. – sum2000 Dec 19 '11 at 21:09
  • looks nice.. it did work.. i was wondering. in this case.. to read n parameter from the config file i need to call n times the above line from shell which will end up reading so many times in python using configparser. is thr any other better way i could do? –  Dec 19 '11 at 21:10
  • 1
    @saddy: You could for instance print both results together, and then use Unix utilities to split them: `RESULT=$(import test; print test.get_foo(), test.get_bar())`, and then use `cut`, `sed` (?), or `awk` (?) to extract each part of the result in turn: `RESULT2=$(echo $RESULT | cut -d' ' -f 2)` gives your `get_bar()`, etc. – Eric O. Lebigot Dec 19 '11 at 21:17
  • @EOL ya thats what i was going for.. thnkx. –  Dec 19 '11 at 21:19
  • i would love to define a function within such a scope to print something from list comprehensions. `python -c 'def log(x): print x; end_def; [ log(i) for i in [ 1,2,3,4]]'` kindof – Alexander Oh Mar 19 '14 at 09:30
  • 2
    If the module is python3, you'll need to use the print function. eg. `python3 -c 'import test; print(test.get_foo())'` – John La Rooy Dec 08 '15 at 21:48