<jupyter_start><jupyter_text>Introduction  **What?**  What does `if __name__ == "__main__"`: do?  The basics  - `__name__` is a global variable.- `__name__` variable points to the namespace wherever the Python interpreter happens to be at the moment. - The global variable, __name__, in the module that is the entry point to your program, is `__main__`.     - If you are not using the construct `__name__ == "__main__" the `__name__` is assigned to the name you import the module by.- So, the code under the if block runs if the module is the entry point to your program.- **In practice?** It allows the code in the module to be importable by other modules, without executing the code block beneath on import.  The problem  - Let's assume we have a script called `important.py`- Now I can call the script as normal with `python important.py` and this works OK.- However, what happens when I import this script from another script called `test_import.py`? The function `do_important()` get executed. - **What if this is a behaviour we do not want?** This is where the `__name__ == "__main__"` construct comes to the rescue.<jupyter_code># Checking if the script is present
!ls
# tunning the script
!python important.py
# As you can see what is inside important.py get executed
!python test_import.py<jupyter_output>I'm doing some important stuff here!
Called from important.py, __name__ has value?: important
Called from test_import.py, __name__ has value?: __main__<jupyter_text>A better way  - `__name__` variable points to the namespace wherever the Python interpreter happens to be at the moment. - Inside an imported module, it's the name of that module/script. - There's a **Pythonic way** to improve on this. All we are trying to say is how to we call the script only when we want to do and how do we protect the script when we import the module but we do not want to runt it unless called specifically?- We are going to rewrite our to scripts. These are indicated by the `_new.py`<jupyter_code>!ls
# The model still behaves as before
!python important_new.py
# The real difference is here. As you can see the function is not extecuted
!python test_import_new.py<jupyter_output>Called from important_new.py, __name__ has value?: important_new
Called from test_import_new.py, __name__ has value?: __main__<jupyter_text>Others<jupyter_code># Importing but not executing it
import important_new
# Executing explicitly the function defined under the main
important_new.main()<jupyter_output>I'm doing some important stuff here!
{'fraction_non_alphanumeric': 0.06556082148499211, 'fraction_numerical': 0.0, 'mean_word_length': 4.756818181818182, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '414108', 'license': 'OLDAP-2.3', 'n_tokens_mistral': 698, 'n_tokens_neox': 686, 'n_words': 367, 'path': "__name__ == '__main__'/GitHub_MD_rendering/What does  __name__ == __main__ do?.ipynb", 'repo_name': 'kyaiooiayk/Python-Programming', 'size': '2532'}