Python Forum
question about if __name__=="__main__" - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: question about if __name__=="__main__" (/thread-38697.html)



question about if __name__=="__main__" - aaander - Nov-13-2022

I am working on a scraper, I've made the class, and written the driver under if __name__ == '__main__': My question is, and sorry if it's basic, I'm newish to programming, why is it when I change that to def main(): , and include an if __name__ == '__main__': followed by main(), the program has errors.

I've done a little searching on the topic to get my own answers, but I think I'm not asking the question correctly. Thank you for any help you can give.


RE: question about if __name__=="__main__" - bowlofred - Nov-13-2022

What are the errors? Can you show the code?


RE: question about if __name__=="__main__" - Larz60+ - Nov-13-2022

Please read: https://docs.python.org/3/tutorial/modules.html#executing-modules-as-scripts
and also: https://docs.python.org/3/library/__main__.html

Getting into the technical background of how __name__ is implemented gets deep into the 'import' statement, but all you need to know Unless you're really curious, is that it is used to uniquely identify the module in the import system. it will be equal to "main" if the module is run using python modulename.py from the terminal (command line).
However, If the module is imported into another module, __name__ will not be = __main__, and the code below the if statement will not be run.

That's all you really need to know about __name__:

If you would like to spend some time getting into the import statement, it is fascinating and there is an 'oldie but goodie' from Brett Cannon, who re-wrote import, from pycon 2012 https://www.youtube.com/watch?v=Nsg886UOahw
Many changes have been made since then, but ths is a good place to start.


RE: question about if __name__=="__main__" - jefsummers - Nov-13-2022

Another way to think of it is that you can change the behavior of the .py file depending on whether it is run directly or imported. I use it then to run a demo of a module if run directly, but not if imported.

As @bowlofred asked, what errors or bad behavior are you having, and can you post the code?


RE: question about if __name__=="__main__" - deanhystad - Nov-13-2022

I'll wager on the error being a NameError caused by moving an assignment from global to function scope. Your program used to look like this:
def func():
    print(a)

if __name__ == "__main__":
    a = 5
    func()
And you changed it to this:
def func():
    print(a)

def main():
    a = 5
    func()

if __name__ == "__main__":
    main()
Error:
NameError: name 'a' is not defined
Am I close?


RE: question about if __name__=="__main__" - aaander - Nov-13-2022

(Nov-13-2022, 04:17 PM)deanhystad Wrote: I'll wager on the error being a NameError caused by moving an assignment from global to function scope. Your program used to look like this:
def func():
    print(a)

if __name__ == "__main__":
    a = 5
    func()
And you changed it to this:
def func():
    print(a)

def main():
    a = 5
    func()

if __name__ == "__main__":
    main()
Error:
NameError: name 'a' is not defined
Am I close?

Actually yes, what ended up happening is my class variables became undefined, which threw a wrench in everything that worked previous to the swap. We ended up not needing to change anything in my script, but I was left with the question of why it didnt work.


RE: question about if __name__=="__main__" - deanhystad - Nov-13-2022

There is no way that class variables can become undefined by moving a block of code protected by if __name__ into a function. It is unlikely this would affect instance variables either. I'm pretty sure the problem is with global variables.

You really need to post your code if you want an answer.