Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Undefined variable : main" in pydev/python

I made a file named "Main" in pydev and inside it wrote:

if  __name__ =='__Main__':
    main()

def main():
    print("jargon")    

It says my call to main() under the if statement contains an undefined variable and won't compile. Why does it do this?

like image 773
keyert Avatar asked Jul 18 '26 10:07

keyert


1 Answers

Python code is executed top-to-bottom. You need to move your main() definition above the if __name__ == '__main__' block. The way you have it, at the time that you try to call main(), the function does not yet exist.

like image 131
BrenBarn Avatar answered Jul 21 '26 00:07

BrenBarn