I have this code:
JavaScript
x
3
1
def hello():
2
return 'Hi :)'
3
How would I run this directly from the command line?
Advertisement
Answer
With the -c
(command) argument (assuming your file is named foo.py
):
JavaScript
1
2
1
$ python -c 'import foo; print foo.hello()'
2
Alternatively, if you don’t care about namespace pollution:
JavaScript
1
2
1
$ python -c 'from foo import *; print hello()'
2
And the middle ground:
JavaScript
1
2
1
$ python -c 'from foo import hello; print hello()'
2