From shell script call a python function of python script located at custom location

My python file: /root/src/foo.py

#/root/src/foo.py

def add(a,b):
    '''add(a,b) --> a+b'''
    return a+b
      

How do I call foo.add(a,b) from a shell script? I got this suggestion from somewhere:

python -c "import foo; print(foo.x(1, 2))"

But it won't work if the shell script and foo.py are in a separate locations.


you need to add the folder to your pythonpath

PYTHONPATH=/path/to/foo python -c "import foo;print(foo.add(5,6))"

should work (in this case it just sets pythonpath, not adds to it)