Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

$ python foo.py
$

>Solution :

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())'
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading