'Explaining what main script only runs if imported means?
I am a little bit confused on what the teacher is asking for here. Can someone please explain what it means when main script should on run if module is executed?
This is what the instructions were:
Main script with automated testing. The main script should only run if the module is executed. It should not run when the module is imported. The main script should print the total number of tests that pass and fail.
In the textbook it gives an example like this:

Not really sure what is going on
Solution 1:[1]
When you execute a python module as a script, it is only slightly different than when you import it as a module. Here's a quick way to illustrate the difference.
Say you have a file module.py with content:
print(__name__)
If you run the script directly it will say:
$ python module.py
__main__
If you instead import it from another script it will output:
$ python -c "import module"
module
You can see here, that if a python file is run as a script, that the __name__ attribute will report "__main__".
So if you want to only run code if the module is executed as a script then you can write:
if __name__ == "__main__":
<code you want to execute as a script here>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
