Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set the default subtitles language in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

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 with python -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.

Now you’re ready to go write some awesome Python main() function code!

Download

Sample Code (.zip)

2.2 KB

Download

Course Slides (.pdf)

277.0 KB

Gregory Klassen on March 10, 2020

What does it mean to … ” or with python -m (to execute a package’s main.py file)

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.

Become a Member to join the conversation.