My function sits inside a .py file with the very same name as the function:
emp@emp:~$ python ~/Dropbox/emp/Python/stromkosten_pro_jahr.pyempedokles@empedokles:~$ stromkosten_pro_jahr(20,3)
bash: Syntaxfehler beim unerwarteten Wort »20,3«
emp@emp:~$ Where's the error?
33 Answers
You need to cd into the directory where your python file is located and then invoke the python interactive shell to be able to call and run functions interactively.
# cd to the directory
$ cd ~/Dropbox/emp/Python/
# invoke the python interactive shell
$ python # use `python3` to invoke the Python3 shellThe Python interactive shell would look something like:
Python 2.7.6 (default, Mar 22 2014, 22:59:38)
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> Here you may import the module (your *.py file) and run the functions written therein:
>>> from stromkosten_pro_jahr import stromkosten_pro_jahr
>>> stromkosten_pro_jahr(20,3)
[The output of function would be shown right here]For more information, I would suggest to go through The Python Tutorial.
7As I found in this answer, you can run python's function directly from bash like:
$ python -c 'from a import stromkosten_pro_jahr; stromkosten_pro_jahr(20,3)'Where a is your file/module's name (a.py).
But the important thing is to be in the same directory as your file is.
You can not call Python functions directly from the Bash shell. You are getting this particular error because bash is parsing your arguments (20,3) like so:
$ echo (20,3)
bash: syntax error near unexpected token `20,3'To pass the brackets as a string you need to escape them:
$ echo \(20,3\) '(1,2)'
(20,3) (1,2)But this still will not magically run as Python code - you will need to parse the command line arguments in your Python program (save as x.py):
import sys
def fn(a,b): print a+b
eval(sys.argv[1])Then:
$ python x.py 'fn(0,13)'
13 1