0

I am probably missing something really simple.

So here is my code:

def test():
    return 'Hello!'

if __name__=="__main__":
    print test()

On executing python module_name.py there in no output. When I import the module and execute module_name.test() I get my the expected output. What am I missing here?

phuclv
  • 37,963
  • 15
  • 156
  • 475
DBS
  • 131
  • 5
  • 1
    How did you execute the code without importing? – Matti Virkkunen Feb 24 '16 at 02:27
  • *"On executing the code without importing"* Please show us exactly how you're doing this. Also please fix the excessive whitespace (horizontal and vertical) in your code. – Jonathon Reinhart Feb 24 '16 at 02:28
  • I am using Geany. It has a builtin compile option which executes the command python -m py_compile "module_name.py" – DBS Feb 24 '16 at 02:28
  • 5
    Nope. That's not how you execute a file in python. You want just `python tst.py` Then it's __name__ will be __main__. – Jonathon Reinhart Feb 24 '16 at 02:29
  • 1
    If that is the *actual indentation* (actually looks like it might have been introduced in an edit?) it won't work because the if block is inside the function. – Jason S Feb 24 '16 at 02:35
  • @JasonS Fixed the indentation issue. It was the result of an edit. – DBS Feb 24 '16 at 02:38
  • @JonathonReinhart using python module_name.py isn't giving a different outcome. I have edited the question accordingly. – DBS Feb 24 '16 at 02:51
  • Why are you continuing to use `` tag instead of `backticks`? Only backticks work in comments. And [thanks should be removed](http://meta.stackexchange.com/a/3021/230282). Just an upvote is enough – phuclv Feb 24 '16 at 03:17
  • @DBS see here: http://ibiblio.org/g2swap/byteofpython/read/module-name.html – seralouk Sep 18 '17 at 10:25

0 Answers0