Packages

Individual Python module files can be placed in directories, or even entire hierarchies of directories. This means you can put multiple module files in a directory, forming a package.

Python modules don’t even have to be written in Python, and packages aren’t necessarily directories.

Technically, a Python package is a special kind of module. Packages are modules that contain a __path__ attribute.

Both packages and modules are also objects.

While directories are separated by a / character (or \ on Windows) when referring to them in a terminal, when you import modules in Python, the slashes become dots.

mystuff/mymodule.py
def greet():
    print("Hello")

def main():
    greet()

if __name__ == "__main__":
    main()
myprog.py
import mystuff.mymodule as mm

mm.greet()
output:
Hello

Leave a Reply

Blog at WordPress.com.

%d