Python Packages (And Modules!) Explained
If you’ve ever opened a text file and written Python code in it, you’ve created a module! A module is a file with a
.py
extension that can be imported into other modules, run by itself, or both.
Packages are collections of modules that you create and use.
You may have seen
if __name__ == '__main__'
in a few Python modules.__name__
is usually the variable holding the name of the module itself (so if you had a module calledmusic.py
, name would holdmusic
.) However, if you’re running the module directly through thepython
command or some other means,__name__
will be__main__
instead. So when you see thatif
statement, it means that that block of code will only run if this module is being run directly (i.e. not imported.)