How can I execute the below simple script called foo.py from the (bash) command line?
def hello():
return 'Hi :)'
I tried the following and got an error.
$ python -c 'import foo; print foo.hello()'
File "<string>", line 1
import foo; print foo.hello()
^
SyntaxError: invalid syntax
I'm able to execute the following but just receive no output.
$ python foo.py
$
3 Answers 3
print foo.hello()
Is this Python2?
remove that print before function call:
$ python -c 'import foo; foo.hello()'
Or make it like python3 syntax:
$ python -c 'import foo; print(foo.hello())'
answered Jan 29, 2022 at 13:37
Luke Skywalker
1,54915 silver badges30 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
If you're using python 2.x, use:
import foo; print foo.hello()
However, on python 3.x brackets are required:
import foo; print(foo.hello())
Comments
maybe your Python version is 3.x
On Python3 print should have "()"
you can try
print(foo.hello())
Comments
default
python foo.pythis is correct. You didn't receive an output because you didn't call the function, you just defined it. def hello(): return 'Hi :)' print hello()$ python -c "import foo; print(foo.hello())>