Defining Main Functions in Python: Summary
Congratulations! You now know how to create Python main()
functions.
Here’s what you covered:
-
Knowing the value of the
__name__
variable is important to write code that serves the dual purpose of executable script and importable module. -
__name__
takes on different values depending on how you executed your Python file.__name__
will be equal to:"__main__"
when the file is executed from the command line or withpython -m
(to execute a package’s__main__.py
file)- The name of the module, if the module is being imported
- Python programmers have developed a set of best practices to use when you want to develop reusable code.
To learn more about the concepts covered in this course, check out What Does if name == “main” Do in Python?.
Now you’re ready to go write some awesome Python main()
function code!
Congratulations, you made it to the end of the course! What’s your #1 takeaway or favorite thing you learned? How are you going to put your newfound skills to use? Leave a comment in the discussion section and let us know.
00:00
Let’s just recap what you’ve learned in this tutorial. First of all, you learned that knowing the value of the __name__
variable is very important when you’re writing code that can serve as both an executable script and an importable module. Next, you learned that the __name__
variable takes on different values depending on how you executed your Python file. __name__
will be equal to "__main__"
when the file is executed from the command line or with python -m
, which executes the package’s __main__.py
file, and __name__
will be equal to the name of the module if the module is being imported. Lastly, you learned that Python programmers have developed a set of good practices to use when you want to develop reusable code.
00:41 So, that just about wraps up this tutorial. I hope you enjoyed it, and thank you very much for watching.
Eriberto on March 12, 2020
The switch m means module, so from the command line supply python with the module name to call it.
Lando on March 12, 2020
Is there a design pattern/best practice/examples to convert a script file that uses argparse at the top level with global variables, into a module-based format with independently callable functions?
I’ve inherited a python script like that, but need to also use it as a module.
I’m trying the approach of moving the argparse bits into the main() function to preserve the ability to run it as a script, and for module use I’ve created individual function wrappers to take arguments, and the function wrappers then set the global variables and then call the original function (so I don’t have to touch the original code that much).
Anonymous on March 14, 2020
Very Good
Pygator on March 14, 2020
Good distinction between file, module for importing, and script for command line execution of the files .py extension in general. Made name usage more clear.
markthiele on March 16, 2020
Thanks, very nice.
horacionesman on March 22, 2020
very useful, thanks!
malshishani on March 22, 2020
Thank you a lot, really good :).
pshapard on March 27, 2020
Completed my first course. I new about the main() and name. I’m using this during the lockdown due to the corona virus to brush up on what I already know about python and learn new areas of python.
Alan ODannel on April 14, 2020
Very useful, a good refresher course.
Mario on April 19, 2020
thanks, really interesting course.
mikesult on May 4, 2020
Thanks this give me the direction to clean up some scripts that I’ve written to make them more reusable.
Daniel Faria on July 1, 2020
This was really elegant.
paolotagliente on July 9, 2020
great, very useful and clear, thanks!
Tim-Morgan on July 11, 2020
Thanks for this course! As someone who has only written quick and dirty scripts, I appreciate “basic” topics like this for best practices.
Alain Rouleau on July 22, 2020
Thanks, all very interesting. Learning how the __name__
variable works is kind of important. One way I like to use it is for testing my code through docstrings.
So, for example, you could run doctest.testmod()
only if the module is run as a script and not imported. It’s a quick way to test your code without having to create all sorts of other test files using pytest and so on.
For people interested, check out Python’s doctest documentation for how this works using if __name__ == "__main__":
and docstrings:
docs.python.org/3/library/doctest.html
Just change the argument to doctest.testmod(verbose=True)
if you want to see the result of the doctests. By default, nothing will be displayed if all the tests pass.
Ghani on Oct. 23, 2020
Very interesting course indeed. Many thanks!
Become a Member to join the conversation.
Gregory Klassen on March 10, 2020
What does it mean to … ” or with python -m (to execute a package’s main.py file)