Python Tutorial: if __name__ == '__main__'
Why Stack Overflow recommends it
Stack Overflow users link it when answering beginner questions where code runs unexpectedly on import or where a script needs a proper main entry point.
Recommended by 4 people across 4 different months, 2016 to 2022.
What it is
A short Python tutorial from Corey Schafer explaining the `if __name__ == '__main__':` conditional. It shows how the check tells you whether a module is being run directly or imported by another file.
What people said
Recommends Reblochon Masque · 2016-07 · Stack Overflow · on “Extreme newbie - Python code skipping to the end (syntax issue?)”
You need to add if __name__ == '__main__:' , and call main() from there: import random def roll (sides = 6): numberRolled = random.randint(1,sides) return numberRolled def main(): sides = 6 rolling = True while rolling: roll_again = raw_input("Press Enter to roll, or Q to quit.") if roll_again.lower() != "q": numberRolled = roll(sides) print ("You rolled a " + numberRolled) else: rolling = False if __name__ == '__main__': main() print ("Thanks for playing!") For details as to why and how it works, please see: - what-does-if-name-main-do [LINK] - tutorial name == main [this video] - python documentation [LINK]
Recommends Ted Klein Bergman · 2016-08 · Stack Overflow · on “Odd click issues (Python & Pygame)”
Create a file called *Test* that contains `print("Hello")`. Create another file where you import *Test*. Just importing *Test* will print *"Hello"*. Everything under `if __name__ == "__main__":` will not be run when you import that file however. It's hard to explain in a limited space as the comment section but I believe this video will explain somewhat decent: [this video]
Recommends Micael Jarniac · 2020-10 · Stack Overflow · on “Issue when Calling a function from another .py file”
In Python, it's common to see the following: # Defining some function that might be reused later def my_sum(a, b): return a + b # This will only be True if this file is called directly if __name__ == "__main__": print(my_sum(5, 7)) So basically, that allows you to import a certain file to reuse its functions without running parts of it that you might not want to. __name__ is a special variable that contains the name of the current script, but if it's been called directly, it'll be "__main__" , so that's a way of knowing whether that script is being run directly or imported. If it's imported, it's __name__ won't be "__main__" . Corey Schafer has some pretty good Python tutorials on YouTube, a
Recommends Freddy Mcloughlan · 2022-04 · Stack Overflow · on “Python, NameError: name browser is not defined”
Calling this function when __name__ != '__main__' (from another: file, thread or process) will never initialize browser . Example: def f(): global browser browser if __name__ == '__main__': browser = None # Calling f will not raise an error f() def f(): global browser browser if __name__ != '__main__': browser = None # Calling f will will now raise an error f() I think what's happening is you are making a pool and the pool runs parsing_pages() from another process where __name__ != '__main__' . You need to do one of the following: Pass browser into your function as an argument Initialize browser outside of the if statement You should add print(__name__) to check what it equals. It will proba
Cites Raymond C. · 2020-10 · Stack Overflow · on “How to find the minimum, and maximum value within a Binary Tree”
Note - There are a few exceptions to my answer: It may contain errors as OP did not provide the BinaryTree class. Does not take into account any logical issues. It is focused on getting OP's code working. Here are the errors that I spotted: Python is a language which requires proper indentation. Indentation is vital in-order for code to function properly. It appears that the way you have indented your code is incorrect. When you are trying to call a function inside a class, you would need to make the call either via an instance (self) or the class itself (classmethods). Explanation provided here. [LINK] Explanation of if __name__ == "__main__": here [this video] . It is not really required as I a