1

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
$
asked Jan 29, 2022 at 13:30
6
  • Use python foo.py this 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() Commented Jan 29, 2022 at 13:35
  • Try python -c "import foo; print(foo.hello())" Commented Jan 29, 2022 at 13:35
  • @AhsanGoheer I get a greater than sign. $ python -c "import foo; print(foo.hello()) > Commented Jan 29, 2022 at 13:38
  • @jgg you get the greater than sign as an output? Commented Jan 29, 2022 at 13:40
  • @AhsanGoheer Yes I got the greater than sign. I think it was because of the double quotes. Once I changed to single quotes it worked. Commented Jan 29, 2022 at 13:43

3 Answers 3

1
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
Sign up to request clarification or add additional context in comments.

Comments

1

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())
answered Jan 29, 2022 at 13:42

Comments

0

maybe your Python version is 3.x

On Python3 print should have "()"

you can try

print(foo.hello())
answered Jan 29, 2022 at 13:38

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.