Prev Source
Next Source

Python Packages (And Modules!) Explained

original source

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 called music.py, name would hold music.) However, if you’re running the module directly through thepython command or some other means, __name__ will be __main__ instead. So when you see that if statement, it means that that block of code will only run if this module is being run directly (i.e. not imported.)


Date
May 25, 2024