jgg
jgg

Reputation: 831

How to execute a function inside a python script?

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
$

Upvotes: 1

Views: 51

Answers (3)

Trent112232
Trent112232

Reputation: 175

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())

Upvotes: 1

BugMaker
BugMaker

Reputation: 61

maybe your Python version is 3.x

On Python3 print should have "()"

you can try

print(foo.hello())

Upvotes: 0

Hossein Heydari
Hossein Heydari

Reputation: 1549

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())'

Upvotes: 1

Related Questions